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.
Shortcut | Description |
---|---|
Ctrl+a | go to beginning of line |
Ctrl+e | go to end of line |
Esc-b | move cursor backward one word |
ESC-f | move cursor forward one word |
Ctrl+c | interrupt the command your running |
Ctrl+d | if no characters on line, exit shell (EOF) otherwise just delete the next character after cursor |
Esc-r | revert line to its initial state (could be several undo's) |
Ctrl+\+_ | undo last edit |
Ctrl+t | swap order of two letters |
Esc-t | swap order of two words |
Ctrl+u | deletes the line before the cursor (keeping it in buffer if required again) |
Ctrl+k | deletes the line after the cursor (keeping it in buffer if required again) |
Ctrl+w | deletes the word before the cursor (keeping it in buffer if required again) |
Ctrl+y | paste whatever is stored in the buffer |
Esc-c | capitalise the first letter of the current or following word |
Esc-u | change the rest of the current word or following word to uppercase |
Esc-l | change to 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+l | clears the screen (as does clear command) |
Ctrl+r | search through history for last command of particular type |
Ctrl+z | suspends whatever process your running and puts it to the background (fg restores it) |
Ctrl+x+v | displays the version of bash that you are using |
Esc-. | last word of last command |
Tab | autocomplete existing file, folder, and command names to save you typing them out |
!! | run the last command again |
!n | run the command from the nth position in history |
!string | run the last command starting with string |
!?string | run the last command containing string |
!\$ | last word of last command just resolves the "word" before you hit enter (so you can see it) |
!!:p | just display the last command |
!string:p | just 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 file-types 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:
Shortcut | Description |
---|---|
Ctrl+a | go to beginning of line |
Ctrl+e | go to end of line |
Esc-b | move cursor backward one word (use Esc as meta key) |
Esc-f | move cursor forward one word (use Esc as meta key) |
Interrupt / Exit (EOF)
Shortcut | Description |
---|---|
Ctrl+c | interrupt the command your running |
Ctrl+d | if no characters on line, exit shell (EOF) otherwise just delete the next character after cursor |
Undo
Shortcut | Description |
---|---|
Esc-r | revert line to its initial state (could be several undo's) |
Ctrl+\+_ | undo last edit |
Flip / Swap
Shortcut | Description |
---|---|
Ctrl+t | swap order of two letters |
Esc-t | swap order of two words # must use Esc as meta key |
Cut, copy and paste
Shortcut | Description |
---|---|
Ctrl+u | deletes the line before the cursor (keeping it in buffer if required again) |
Ctrl+k | deletes the line after the cursor (keeping it in buffer if required again) |
Ctrl+w | deletes the word before the cursor (keeping it in buffer if required again) |
Ctrl+y | paste whatever is stored in the buffer |
Uppercase, lowercase and capitalisation
Shortcut | Description |
---|---|
Esc-c | capitalise the current or following word |
Esc-u | change the rest of the current word or the following word to uppercase |
Esc-l | change 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
Shortcut | Description |
---|---|
Ctrl+l | clears the screen (as does the command clear) |
Search history
Shortcut | Description |
---|---|
Ctrl+r | search through history for last command of particular type |
Suspend a foreground process
Shortcut | Description |
---|---|
Ctrl+z | suspends whatever process your running and puts it to the background (fg restores it) |
Event designators
Shortcut | Description |
---|---|
!! | run the last command again |
!n | run the command from the nth position in history |
!string | run the last command starting with string |
!?string | run the last command containing string |
^x^y | replace 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) |
!!:p | just print/display the last command |
!string:p | just print/display the last command starting with string |