23

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

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
Saurav Kumar
  • 15,174

5 Answers5

27

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.
Sparhawk
  • 6,969
22

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

10

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
1

Okay I've got a way to get the list of shortcuts by filtering the bash manual. It will also give the description what exactly each shortcut does. Thanks to Sparhawk who enlightened me to find the solution. What I needed was to learn the use of regular expressions although I'm still not good in it :)

So here is the one line command:

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

Here is a small extraction of the output:

   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.

Now to save the shortcuts to a file:

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

That's all I needed. I just wanted to know the shortcut keys assigned to bash and I've not reconfigured any keys as BroSlow asked me.

Once again thanks to all for their contributions.

Note:

If someone wants to enhance this, he/she is most welcomed. I've only mentioned the way to list those shortcuts which have been assigned by some keys. So if someone knows how to list those actions which have not been assigned with the description using this way, is most welcomed :)

Saurav Kumar
  • 15,174
1

As long as the bash manual is not modified in a way to make this command improper(which is not very likely), the following command will show all default shortcuts for bash.

man bash | grep -A294 'Commands for Moving'

This gives an output which looks like:

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

If the bash manual is modified ,this command can easily be changed to fit the needs.

Saurav Kumar
  • 15,174