Handy Bash Shortcuts

In this post, we'll show you some handy bash shortcuts. When you've become comfortable with using these (or at least some of them) your productivity at the bash shell will improve considerably!

Line Editing

By default, line editing commands are similar to those of emacs so we'll only discuss those for now. (If you wish to change to vi style editing commands, use set -o vi (to change back use set -o emacs)).
When you use the Esc key in the short cuts below, you just need to press it then release it before pressing the next key(s).

The easiest shortcut and probably the most useful is using Tab to autocomplete files and folders. The rest will take a little more effort to remember. Below is a table of shortcuts, after that we present them sorted by category.

ShortcutDescription of what it does
Ctrl+ago to beginning of line
Ctrl+ego to end of line
Esc-bmove cursor backward one word
Esc-fmove cursor forward one word
Ctrl+cinterrupt the command your running
Ctrl+dif no characters on line, exit shell (EOF) otherwise just delete the next character after cursor
Esc-rrevert line to its initial state (could be several undo's)
Ctrl+_undo last edit
Ctrl+tswap order of two letters
Esc-tswap order of two words
Ctrl+udeletes the line before the cursor (keeping it in buffer if required again)
Ctrl+kdeletes the line after the cursor (keeping it in buffer if required again)
Ctrl+wdeletes the word before the cursor (keeping it in buffer if required again)
Ctrl+ypaste whatever is stored in the buffer
Esc-ccapitalise the first letter of the current or following word
Esc-uchange the rest of the current word or the following word to uppercase
Esc-lchange the rest of the current word or the following word to lowercase
Esc-#insert comment (comment out the line and return, comment available in history)
Ctrl+lclears the screen (as does the "clear" command)
Ctrl+rsearch through history for last command of particular type
Ctrl+zsuspends whatever process your running and puts it to the background (fg restores it)
Ctrl+x+vdisplays the version of bash that you're using
Esc-.last word of last command
Tabautocomplete existing file, folder, and command names to save you typing them out
!!run the last command again
!nrun the command from nth position in history
!stringrun last command starting with string
!?stringrun the last command containing string
!$last word of last command just resolves the "word" before you hit return (so you can see it)
!!:pJust display the last command
!string:pJust display the last command containing string

Auto-completion Examples

You can use TAB to show what file/folder options are available from your current directory. For example, say you have a directory with the following files/folders:

file1.txt  file2.txt  firefox_notes.txt ftp_stuff notes.txt
If you want to look at the contents of notes.txt, you'd just need to type:
less n
followed by the TAB key, which will auto complete the line to:
less notes.txt
Similarly, if you want to see what files you can look at starting with f, type:
less f
Followed by TAB TAB, and you'll see the options displayed:
file1.txt          file2.txt          firefox_notes.txt  ftp_stuff
If you then add the letter "i":
less fi
followed by TAB TAB, you'll see the options reduced to those starting with "fi":
file1.txt          file2.txt          firefox_notes.txt
Next, if you add the letter "l", followed by TAB, you'll see it autocompletes to:
less file
Another TAB now will give the options:
file1.txt  file2.txt
Now adding a "2" followed by a TAB will autocomplete to:
less file2.txt
So if you've forgotten the exact files and folders in a directory, TAB can help you navigate your way around pretty easily!

As well as that, autocomplete is clever enough to know some commands can only use certain filetypes for example, if we had used "cd" followed by TAB (to see the available options) it will autocomplete to use the directory automatically
cd ftp_stuff/
You can also use TAB see what commands or utilities are available. For example type a letter followed by TAB TAB and you'll be given the option to see all available options. Try it with "ma" followed by TAB TAB and you'll see all command/utility options. These will include mail, make, man and a whole load of others.


The Shortcuts Grouped by Category:

Moving the Cursor

In addition to using the arrow keys, you can use these:

ShortcutDescription of what it does
Ctrl+ago to beginning of line
Ctrl+ego to end of line
Esc-b move cursor backward one word (use Esc as meta key)
Esc-fmove cursor forward one word (use Esc as meta key)

Interrupt/Exit(EOF)

ShortcutDescription of what it does
Ctrl+cinterrupt the command your running
Ctrl+dif no characters on line, exit shell (EOF) otherwise just delete the next character after cursor

Undo

ShortcutDescription of what it does
Esc-rrevert line to its initial state (could be several undo's)
Ctrl+_undo last edit

Flip/Swap

ShortcutDescription of what it does
Ctrl+tswap order of two letters
Esc-tswap order of two words # must use Esc as meta key

Cut/Copy Paste

ShortcutDescription of what it does
Ctrl+udeletes the line before the cursor (keeping it in buffer if required again)
Ctrl+kdeletes the line after the cursor (keeping it in buffer if required again)
Ctrl+wdeletes the word before the cursor (keeping it in buffer if required again)
Ctrl+ypaste whatever is stored in the buffer

Uppercase/Lowercase/Capitalisation

ShortcutDescription of what it does
Esc-ccapitalise the current or following word
Esc-uchange the rest of the current word or the following word to uppercase
Esc-lchange the rest of the current word or the following word to lowercase
Esc-#insert comment (comment out the line and return, comment available in history)

Clear the Screen

ShortcutDescription of what it does
Ctrl+lclears the screen (as does the command "clear")

Search History

ShortcutDescription of what it does
Ctrl+rsearch through history for last command of particular type

Suspend a Foreground Process

ShortcutDescription of what it does
Ctrl+zsuspends whatever process your running and puts it to the background (fg restores it)

Event designators

ShortcutDescription of what it does
!!run the last command again
!nrun a command wich in nth position in history
!stringrun last command starting with string
!?stringrun last command containing string
^x^yreplace string x with string y in the last command entered
!$last word of last command just resolves the "word" before you hit return (so you can see it)
!!:pjust print/display the last command
!string:pjust print/display the last command starting with string

comments powered by Disqus