21

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

22

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.
  • 1
    These are default shortcuts, not necessarily the shortcuts on OP's actual system. bind -P would be more precise. – BroSlow Apr 8 '14 at 0:54
  • @BroSlow Good comment. I still think there is merit in my answer, though, since it's more verbose about what the commands do. If you write it as an answer, I'll +1. – Sparhawk Apr 8 '14 at 0:56
  • @Sparhawk: +1 for your answer. I'm looking for the list of shortcuts at one place. So if bash manual can tell me about all the shortcuts, how will I get it together, like you answered here!! How did you get the list like this? Is there any way to parse the shortcuts with the action and save it in another file? I'll be waiting for your reply.. – Saurav Kumar Apr 8 '14 at 6:49
  • If you search the bash manual for Readline Command Names, you'll see all the commands in this section. I'll write a short script to extract it to a text file, but I can't do it right now (I'll have time in a few hours). – Sparhawk Apr 8 '14 at 7:18
  • @Sparhawk: Your answer helped me to write a simple grep filter command. Check it here hope you'd like it. Thanks for your help. Though I'm waiting for your script.. :) – Saurav Kumar Apr 8 '14 at 10:43
20

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

  • Getting the shortcut keys by searching it by name is little unusual to me and also a long process. Is there any simple way to get the list of shortcuts at one place? Hope you understand. +1 though for this approach.. – Saurav Kumar Apr 8 '14 at 6:45
  • 2
    @SauravKumar bind -P should give you all the shortcuts. If you mean ignoring those that don't have mappings for bind/readline functions you could do something like bind -P | grep -v "not bound" – BroSlow Apr 8 '14 at 6:51
8

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
  • @SauravKumar You want me to add anything to the answer? – Registered User Apr 8 '14 at 8:17
  • 2
    +1 for your answer and to make it more meaningful.. :) I missed to and it changed the whole meaning of the sentence ;) No you don't have to add any thing. You've done your best.. – Saurav Kumar Apr 8 '14 at 8:20
  • +1 Nice command! I've expanded it to include unset shortcuts: 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 Dec 1 '17 at 6:29
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 :)

  • Very nice. However, the only problem is that this will only print the first line of multi-line descriptions. Also, it skips headers and commands without default keystrokes (e.g. dump-macros), although that might be what you want. – Sparhawk Apr 8 '14 at 11:02
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.

  • Well done Patil!! Why didn't I think about this way.. :) – Saurav Kumar Apr 24 '14 at 4:35
  • @Patil I thought about hardcoding the number of lines too, but I figured it was more likely that the bash manual had the number of lines in the command section changed, rather than have the order of its sections altered. I agree that either is probably unlikely though. – Sparhawk May 4 '14 at 1:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.