40+ Commonly Used Keyboard Shortcuts for Bash



Hi everyone,

In this post, I share the shortcuts that I use while interacting with bash command line to make the scripting easier and faster. Although, most of these shortcuts are available on the internet, I wanted to list as many shortcuts as possible and their actions at one place. Hopefully this post helps you to learn many of them all at once.


What's Bash?

Bash is the shell for most major Linux distributions. If you're running Ubuntu, Fedora, Slackware, Mandriva, then when you are at a console or have a terminal open, you're working with Bash. 

Bash was written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as it is a default shell on the major Linux distributions and OS X. Bash (Bourne Again Shell) has a very rich array of convenient shortcuts that significantly reduces the difficulty of working with the command line.

Command Editing Shortcuts

Ctrl + A  go to the start of the command line
Ctrl + E  go to the end of the command line
Ctrl + K  delete from cursor to the end of the command line
Ctrl + U  delete from cursor to the start of the command line
Ctrl + W  delete from cursor to start of word (i.e. delete backwards one word)
Ctrl + Y  paste word or text that was cut using one of the deletion shortcuts (such as the one above) after the cursor
Ctrl + XX move between start of command line and current cursor position (and back again)
Alt + B  move backward one word (or go to start of word the cursor is currently on)
Alt + F  move forward one word (or go to end of word the cursor is currently on)
Alt + D  delete to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + C capitalize to end of word starting at cursor (whole word if cursor is at the beginning of word)
Alt + U  make uppercase from cursor to end of word
Alt + L  make lowercase from cursor to end of word
Alt + T  swap current word with previous
Ctrl + F  move forward one character
Ctrl + B  move backward one character
Ctrl + D  delete character under the cursor
Ctrl + H  delete character before the cursor
Ctrl + T swap character under cursor with the previous one

Command Recall Shortcuts

Ctrl + R  search the history backwards
Ctrl + G  escape from history searching mode
Ctrl + P  previous command in history (i.e. walk back through the command history)
Ctrl + N  next command in history (i.e. walk forward through the command history)
Alt + .  use the last word of the previous command

Command Control Shortcuts

Ctrl + L  clear the screen
Ctrl + S  stops the output to the screen (for long running verbose command)
Ctrl + Q  allow output to the screen (if previously stopped using command above)
Ctrl + C  terminate the command
Ctrl + Z  suspend/stop the command

Bash Bang (!) Commands

Bash also has some handy features that use the ! (command) to allow you to do some extra stuff with bash commands.

!!  run last command
!command  run the most recent command that starts with ‘command’ (e.g. !ls)
!command:p  print out the command that !command would run (also adds it as the latest command in the command history)
!$  the last word of the previous command (same as Alt + .)
!$:p  print out the word that !$ would substitute
!*  the previous command except for the last word (e.g. if you type ‘find some_file.txt /‘, then !* would give you ‘find some_file.txt‘)
!*:p  print out what !* would substitute

In bash, you can also get this list along with many other commands. However, if we want to take only the ones we need, we can use the following command:

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


bind -P : gives all the shortcuts.

grep "can be found": removes all non assigned shortcuts

sort: sorts out the output

awk '{printf "%-40s", $1} :prints the first column(i.e use) and justifies text

{for(i=6;i<=NF;i++)
   {printf "%s ", $i}{printf"\n"}}'
:This is part of the previous command, it removes first 5 character from second column and prints the remaining stuff(i.e shortcuts)

There is a lot more than these when it comes to shortcuts with bash. But, the shortcuts above will get you 90% of the way towards maximum bash productivity. If you think that I have missed out on an essential bash shortcut that you can’t live without, then please let me know and I’ll update the post. Also, here is a list of Useful Linux Commands if you would like to check it out.

Thanks for Sharing and Happy coding!
Author:

Software Developer, Codemio Admin


Disqus Comments Loading..