是否有任何手册来获取bash快捷键列表?


21

bash命令行交互时,我使用了许多快捷方式,以使工作更轻松,更快捷。

喜欢:

  • ctrl+ L:清除屏幕
  • ctrl+ a/ ctrl+ e:向移动开始/行尾
  • ctrl+ r:仅写几个字符即可搜索命令的历史记录
  • ctrl+ u/ ctrl+ y:到剪切/粘贴线。

还有很多我想知道的知识,对学习绝对有帮助。

我想从哪里知道在Ubuntu中这些快捷方式的列表?是否有列出这些快捷方式的手册?

注意:

我想将快捷方式及其操作的列表放在一个位置。在很短的时间内学习其中的许多内容确实会有所帮助。那么有什么办法可以得到这样的清单呢?虽然感谢您在这里给出的答案。

Answers:


22

默认值在中man bash,以及每个命令的详细信息。如果更改了键绑定,请参见BroSlow的答案。

   Commands for Moving
       beginning-of-line (C-a)
              Move to the start of the current line.
       end-of-line (C-e)
              Move to the end of the line.
       forward-char (C-f)
              Move forward a character.
       backward-char (C-b)
              Move back a character.
       forward-word (M-f)
              Move forward to the end of the next word.  Words are composed of alphanumeric characters (letters and digits).
       backward-word (M-b)
              Move back to the start of the current or previous word.  Words are composed of alphanumeric characters (letters and digits).
       shell-forward-word
              Move forward to the end of the next word.  Words are delimited by non-quoted shell metacharacters.
       shell-backward-word
              Move back to the start of the current or previous word.  Words are delimited by non-quoted shell metacharacters.
       clear-screen (C-l)
              Clear the screen leaving the current line at the top of the screen.  With an argument, refresh the current line without clearing the screen.

...

       reverse-search-history (C-r)
              Search backward starting at the current line and moving `up' through the history as necessary.  This is an incremental search.

...

       unix-line-discard (C-u)
              Kill backward from point to the beginning of the line.  The killed text is saved on the kill-ring.

...

       yank (C-y)
          Yank the top of the kill ring into the buffer at point.

编辑

这些命令都在本手册的连续部分中,因此您可以从中进行浏览Commands for Moving。或者,您可以将整个部分保存到带有

man bash | awk '/^   Commands for Moving$/{print_this=1} /^   Programmable Completion$/{print_this=0} print_this==1{sub(/^   /,""); print}' > bash_commands.txt

(注意,这将打印整个部分,包括没有默认键盘快捷键的命令。)

AWK代码说明

  • 在(仅)出现时Commands for Moving,将变量设置print_this为1。
  • Programmable Completion以下部分(仅发生)上,将变量设置为0。
  • 如果变量为1,则除去前导空格(三个空格),然后打印该行。

1
这些是默认快捷方式,不一定是OP实际系统上的快捷方式。bind -P会更精确。

@BroSlow好评论。不过,我仍然认为我的回答有好处,因为它对命令的作用更为冗长。如果您将其写为答案,我会+1。
Sparhawk

@Sparhawk:您的答案+1。我在一个地方寻找捷径清单。因此,如果bash手册可以告诉我所有快捷方式,那么像您在这里回答的那样,我如何将其组合在一起!您是怎么得到这样的清单的?有什么方法可以解析带有操作的快捷方式并将其保存在另一个文件中?我会等着你的答复..
Saurav库马尔

如果在bash手册中搜索Readline Command Names,则会在此部分看到所有命令。我将编写一个简短的脚本将其提取到文本文件中,但现在无法执行(我将在几个小时内有时间)。
Sparhawk

@Sparhawk:您的回答帮助我编写了一个简单的grep过滤器命令。在这里检查,希望您喜欢。谢谢你的帮助。虽然我正在等待您的剧本.. :)
Saurav Kumar 2014年

20

您可以通过调用bind带有-P选项的内置bash来列出当前bash shell中的所有快捷方式。

例如

bind -P | grep clear
clear-screen can be found on "\C-l".

要更改它们,您可以执行以下操作

 bind '\C-p:clear-screen'

并将其放在init文件中以使其永久(请注意,一次只能将一个键组合绑定到一个事物,因此它将失去以前的任何绑定)。


通过名称搜索来获取快捷键对我来说并不罕见,而且过程很漫长。有没有一种简单的方法可以将快捷方式列表放在一个地方?希望你能理解。+1,虽然这种方法..
Saurav库马尔

2
@SauravKumar bind -P应该为您提供所有快捷方式。如果您是想忽略那些没有绑定/阅读线功能映射的对象,则可以执行以下操作:bind -P | grep -v "not bound"

7

下面的命令提供了一个很好的列输出,显示了用法和快捷方式。

bind -P | grep "can be found" | sort | awk '{printf "%-40s", $1} {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}'

这给出了一个输出,看起来像

abort                                   "\C-g", "\C-x\C-g", "\e\C-g". 
accept-line                             "\C-j", "\C-m". 
backward-char                           "\C-b", "\eOD", "\e[D". 
backward-delete-char                    "\C-h", "\C-?". 
backward-kill-line                      "\C-x\C-?". 
backward-kill-word                      "\e\C-h", "\e\C-?". 
backward-word                           "\e\e[D", "\e[1;5D", "\e[5D", "\eb". 
beginning-of-history                    "\e<". 
beginning-of-line                       "\C-a", "\eOH", "\e[1~", "\e[H". 
call-last-kbd-macro                     "\C-xe". 
capitalize-word                         "\ec". 
character-search-backward               "\e\C-]". 
character-search                        "\C-]". 
clear-screen                            "\C-l". 
complete                                "\C-i", "\e\e". 
...

使用以下命令将此输出转换为文本文件

bind -P|grep "can be found"|sort | awk '{printf "%-40s", $1} {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}' > ~/shortcuts

该文件在$ HOME目录中创建。

说明

  • 获取所有快捷方式。

    bind -P
  • 删除所有未分配的快捷方式

    grep "can be found"
  • 对输出进行排序

    sort
  • 打印第一列(即函数)并对齐文本

    awk '{printf "%-40s", $1}
  • 这是上一个命令的一部分。它打印6+列(即快捷方式)。

    {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}'
  • 将输出放入主目录中名为快捷方式的漂亮文本文件中

    > shortcuts

通过运行以下命令,您可以了解该命令的工作原理。

bind -P
bind -P | grep "can be found"
bind -P | grep "can be found" | sort

@SauravKumar您想要我在答案中添加任何内容吗?
注册用户

2
+1可以使您的答案更有意义。.::)我错过,它改变了句子的全部含义;)不,您不必添加任何东西。你做你最好的..
Saurav库马尔

+1好命令!我将其扩展为包括未设置的快捷方式:bind -P | tail -n +2 | sort | awk '{printf "%-40s", $1} {if ($6 == "any") {printf"\n"} else {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}}'
wjandrea

1

好的,我有一种方法可以通过过滤bash手册来获取快捷方式列表。它还将说明每个快捷方式的确切功能。感谢Sparhawk启发我找到解决方案。我需要的是学习正则表达式的用法,尽管我仍然不太擅长:)

所以这是一行命令:

man bash | grep "(.-.*)$" -A1

这是输出的一小部分提取:

   beginning-of-line (C-a)
          Move to the start of the current line.
   end-of-line (C-e)
          Move to the end of the line.
   forward-char (C-f)
          Move forward a character.
   backward-char (C-b)
          Move back a character.
   forward-word (M-f)
          Move forward to the end of the next word.  Words are composed of alphanumeric characters (letters and digits).
   backward-word (M-b)
          Move back to the start of the current or previous word.  Words are composed of alphanumeric characters (letters and digits).
   clear-screen (C-l)
          Clear the screen leaving the current line at the top of the screen.  With an argument, refresh the current line without clearing the
   previous-history (C-p)
          Fetch the previous command from the history list, moving back in the list.
   next-history (C-n)
          Fetch the next command from the history list, moving forward in the list.
   beginning-of-history (M-<)
          Move to the first line in the history.
   end-of-history (M->)
          Move to the end of the input history, i.e., the line currently being entered.
   reverse-search-history (C-r)
          Search backward starting at the current line and moving `up' through the history as necessary.  This is an incremental search.
   forward-search-history (C-s)
          Search forward starting at the current line and moving `down' through the history as necessary.  This is an incremental search.

现在将快捷方式保存到文件中:

man bash | grep "(.-.*)$" -A1 > bash_shortcuts

这就是我所需要的。我只想知道分配给bash的快捷键,并且没有按照BroSlow的要求重新配置任何键。

再次感谢大家的贡献。

注意事项

如果有人想改善这一点,他/她是最受欢迎的。我仅提到了列出由某些键分配的那些快捷方式的方法。因此,如果有人知道如何使用这种方式列出未分配给描述的动作,那将是非常受欢迎的:)


非常好。但是,唯一的问题是,这只会打印多行描述的第一行。此外,它可能会跳过标头和命令而没有默认的击键(例如dump-macros),尽管这可能是您想要的。
Sparhawk

1

只要未对bash手册进行某种修改(以使该命令不正确(不太可能)),以下命令将显示的所有默认快捷方式bash

man bash | grep -A294 'Commands for Moving'

给出的输出如下所示:

 Commands for Moving
   beginning-of-line (C-a)
          Move to the start of the current line.
   end-of-line (C-e)
          Move to the end of the line.
   forward-char (C-f)
          Move forward a character.
   backward-char (C-b)
          Move back a character.
   forward-word (M-f)
          Move forward to the end of the next word.  Words are composed of alphanumeric characters (letters and digits).
   backward-word (M-b)
          Move back to the start of the current or previous word.  Words are composed of alphanumeric characters (letters  and
          digits).
   shell-forward-word
          Move forward to the end of the next word.  Words are delimited by non-quoted shell metacharacters.
   shell-backward-word
          Move back to the start of the current or previous word.  Words are delimited by non-quoted shell metacharacters.
   clear-screen (C-l)
          Clear  the  screen  leaving  the  current line at the top of the screen.  With an argument, refresh the current line
          without clearing the screen.
   redraw-current-line
          Refresh the current line.

Commands for Manipulating the History
   accept-line (Newline, Return)
          Accept the line regardless of where the cursor is.  If this line is non-empty, add it to the history list  according
          to  the state of the HISTCONTROL variable.  If the line is a modified history line, then restore the history line to
          its original state.
   previous-history (C-p)
          Fetch the previous command from the history list, moving back in the list.
   next-history (C-n)
...

如果修改了bash手册,则可以轻松更改此命令以适应需要。


做得好Patil!我为什么不这样考虑呢.. :)
Saurav Kumar 2014年

@Patil我也考虑过对行数进行硬编码,但是我发现bash手册更有可能改变了命令节中的行数,而不是改变了节的顺序。我同意,虽然两者都不大可能。
Sparhawk
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.