如果您仍然愿意破解源代码......

如果您仍然愿意破解源代码......

在 Vim 中,您可以点击Shift-k并打开光标下字符串的手册。

是否也可以以这种方式配置 Bash(使用时set -o vi)?

例如:

# '|' represents the position of a cursor.
$ |
# Write a command.
$ grep things *|
# Hit 'esc' to enter normal mode.
# Hit '3b' to move to 'grep'.
$ |grep things *
# Now I would like to hit 'Shift-k' to open 'man grep'.

答案1

您可以将 bash 函数绑定到一个键bind -x。在此函数中,您可以通过变量访问输入缓冲区的当前内容READLINE_LINEREADLINE_POINT

run_man () {
  declare prefix="${READLINE_LINE:0:$READLINE_POINT}" suffix="${READLINE_LINE:$READLINE_POINT}"
  declare word="${prefix##*[!-+.0-9A-Z_a-z]}${suffix%%[!-+.0-9A-Z_a-z]*}"
  man "$word"
}
bind -m vi -x '"K": run_man'

打开命令位置单词的手册页可能比打开光标下单词的手册页更有用,但这需要更复杂的解析。 bash 完成代码可能会有所帮助。或者您可能会选择该行的第一个单词,这比获取当前单词需要更少的解析。

要检测 bash 内置函数并显示 bash 文档而不是手册页,请参阅通用 help/man 命令:帮助内置部分匹配

聚苯乙烯

如果能够看到 man 而无需从提示符中删除整个命令,那就太好了。

我经常在 zsh 中这样做。我希望在 bash 中也可以,但设置起来更复杂。

答案2

只需使用历史扩展引用最后给出的命令的命令名称。

$ grep something
$ man !:0

由于历史扩展已完成别名扩展,如果你想使用别名,你应该这样做:

alias k='man "$(history -p \!:0)"'

然后只需键入k即可查看最后执行的命令的手册页。

答案3

太长了;博士

这不是标准操作,但您可以很容易地添加它。看这个答案(链接)

如果您仍然愿意破解源代码......

我读了的源代码readline看起来添加所需的功能是相当可行的。readline已经支持v允许您进入编辑模式并打开$EDITOR.如果分析如何readline打开的逻辑,那么用光标下的单词作为参数$EDITOR打开应该很容易。man

这里有一些有趣的 grep:

  • grep -RI EDITOR *

    doc/hsuser.texi:is used: @code{$@{FCEDIT:-$@{EDITOR:-vi@}@}}.  This says to use the
    doc/hsuser.texi:@env{EDITOR} variable if that is set, or @code{vi} if neither is set.
    doc/rluser.texi:@code{$VISUAL}, @code{$EDITOR}, and @code{emacs}
    examples/rlfe/ChangeLog:    * line options; use EDITOR/VISUAL to set vi/emacs preference.
    examples/rlfe/README:but if the the environment variable EDITOR is set to "vi" that
    examples/rlfe/rlfe.c: * line options; use EDITOR/VISUAL to set vi/emacs preference.
    examples/rlfe/rlfe.c:      if (getenv ("EDITOR") != 0)
    examples/rlfe/rlfe.c:   vi |= strcmp (getenv ("EDITOR"), "vi") == 0;
    
  • grep -RI -C 5 editing-mode *.c

    bind.c-  { "bell-style",        V_STRING,       sv_bell_style },
    bind.c-  { "comment-begin",     V_STRING,       sv_combegin },
    bind.c-  { "completion-display-width", V_INT,   sv_compwidth },
    bind.c-  { "completion-prefix-display-length", V_INT,   sv_dispprefix },
    bind.c-  { "completion-query-items", V_INT,     sv_compquery },
    bind.c:  { "editing-mode",      V_STRING,       sv_editmode },
    bind.c-  { "emacs-mode-string", V_STRING,       sv_emacs_modestr },  
    bind.c-  { "history-size",      V_INT,          sv_histsize },
    bind.c-  { "isearch-terminators", V_STRING,     sv_isrchterm },
    bind.c-  { "keymap",            V_STRING,       sv_keymap },
    bind.c-  { "keyseq-timeout",    V_INT,          sv_seqtimeout },
    --
    --
    bind.c-  else if (_rl_stricmp (name, "completion-query-items") == 0)
    bind.c-    {
    bind.c-      sprintf (numbuf, "%d", rl_completion_query_items);
    bind.c-      return (numbuf);
    bind.c-    }
    bind.c:  else if (_rl_stricmp (name, "editing-mode") == 0)
    bind.c-    return (rl_get_keymap_name_from_edit_mode ());
    bind.c-  else if (_rl_stricmp (name, "history-size") == 0)
    bind.c-    {
    bind.c-      sprintf (numbuf, "%d", history_is_stifled() ? history_max_entries : 0);
    bind.c-      return (numbuf);
    --
    --
    funmap.c-  { "do-lowercase-version", rl_do_lowercase_version },
    funmap.c-  { "downcase-word", rl_downcase_word },
    funmap.c-  { "dump-functions", rl_dump_functions },
    funmap.c-  { "dump-macros", rl_dump_macros },
    funmap.c-  { "dump-variables", rl_dump_variables },
    funmap.c:  { "emacs-editing-mode", rl_emacs_editing_mode },
    funmap.c-  { "end-kbd-macro", rl_end_kbd_macro },
    funmap.c-  { "end-of-history", rl_end_of_history },
    funmap.c-  { "end-of-line", rl_end_of_line },
    funmap.c-  { "exchange-point-and-mark", rl_exchange_point_and_mark },
    funmap.c-  { "forward-backward-delete-char", rl_rubout_or_delete },
    --
    --
    funmap.c-  { "vi-column", rl_vi_column },
    funmap.c-  { "vi-complete", rl_vi_complete },
    funmap.c-  { "vi-delete", rl_vi_delete },
    funmap.c-  { "vi-delete-to", rl_vi_delete_to },
    funmap.c-  { "vi-eWord", rl_vi_eWord },
    funmap.c:  { "vi-editing-mode", rl_vi_editing_mode },
    funmap.c-  { "vi-end-bigword", rl_vi_eWord },
    funmap.c-  { "vi-end-word", rl_vi_end_word },
    funmap.c-  { "vi-eof-maybe", rl_vi_eof_maybe },
    funmap.c-  { "vi-eword", rl_vi_eword },
    funmap.c-  { "vi-fWord", rl_vi_fWord },
    

相关内容