Is there any manual to get the list of bash shortcut keys?

There are many shortcuts that I use while interacting with bash command line to make the work easier and faster.

Like:

  • ctrl+L: to clear the screen
  • ctrl+a/ctrl+e: to move start/end of the line
  • ctrl+r: to search the history of command just writing few of chars
  • ctrl+u/ctrl+y: to cut/paste the line.

and many many more, that I want to know and which will definitely useful to learn.

I want to know from where can I get the list of these shortcuts in Ubuntu? Is there any manual which lists these shortcuts?

NOTE:

I want to get the list of shortcuts and their actions at one place. It will really help to learn many of them in a small duration of time. So is there way we can get the list like this? Though thanks for answer given here..


Solution 1:

The defaults are in man bash, along with details as to what each command does. See BroSlow's answer if you have changed your key bindings.

   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.

EDIT

These commands are all in a contiguous section of the manual, so you can browse it from Commands for Moving. Alternatively, you can save this entire section to a text file with

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

(N.B. this prints the whole section, including commands with no default keyboard shortcut.)

Explanation of awk code

  • On the (only) occurrence of Commands for Moving, set the variable print_this to 1.
  • On the (only) occurrence of Programmable Completion, which is the following section, set the variable to 0.
  • If the variable is 1, then get rid of the leading whitespace (three spaces), and print the line.

Solution 2:

You can list all shortcuts in your current bash shell by calling the bash builtin bind with the -P option.

e.g.

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

To change them, you can do something like

 bind '\C-p:clear-screen'

And put it in an init file to make it permanent (note you can only have a key combination bound to one thing at a time, so it will lose any binding it had previously).

Solution 3:

The following command gives a nice columnar output showing the use and shortcuts.

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

This gives an output, which looks like

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". 
...

Get this output into a text file using following command

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

The file is created in your $HOME directory.

Explanation

  • gets all the shortcuts.

     bind -P
    
  • removes all non-assigned shortcuts

     grep "can be found"
    
  • sorts the output

     sort
    
  • prints the first column (i.e. function) and justifies text

     awk '{printf "%-40s", $1}
    
  • This is part of the previous command. It prints columns 6+ (i.e. shortcuts).

     {for(i=6;i<=NF;i++){printf "%s ", $i}{printf"\n"}}'
    
  • Puts the output into a nice text file in home dir named shortcuts

     > shortcuts
    

You can get the idea of how the command works by running the following commands.

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