Bash shortcuts
Here are some useful shortcuts when you’re working in Bash. Shortcuts are categorised by use.
Conventions
Ctrl
-a
: Hold the Control key and press ‘a’.Alt
-a
: Hold the Alt key and press ‘a’. The Alt key is sometimes called the Meta key. If the Alt key doesn’t work, try using the Escape key instead. (SoEsc
-a
instead. You might need to let go of the Escape key before pressing ‘a’.)key1
key2
: Presskey1
, let go, then presskey2
.- This page is case sensitive so
a
is different fromA
.A
is the same asShift
-a
. - A smallword is any string of characters that contain only characters from the set
{a-z,A-Z,0-9}
. Smallwords are delimited by non-alphanumeric characters (whitespace, full stop, caret, etc.). This is similar to a lowercase ‘w’ in the Vi editor. - A bigword is any string of characters not containing a whitespace. Bigwords are delimited by whitespace characters (space, tab, etc.). This is similar to an uppercase ‘W’ in the Vi editor.
Movement
Ctrl
-a
orHome
: Jump to the beginning of the line.Ctrl
-e
orEnd
: Jump to the end of the line.Ctrl
-xx
: Jump between saved locations. Pressing this will toggle between the current location and the beginning of the line. If you move the cursor after you have jumped to the beginning, then press this again, Bash will remember the location. Press this again to come back to the location you just saved.Ctrl
-f
orRight Arrow Key
: Go forward one character.Alt
-f
: Go forward one smallword. This does not work on the Gnome Terminal becauseAlt
-f
is bound to the “file” menu. However,Esc
f
works.Ctrl
-b
orLeft Arrow Key
: Go back one character.Alt
-b
: Go back one smallword.Ctrl
-]
a
: Move to the next occurrence of ‘a’.Alt
-Ctrl
-]
a
: Move to the previous occurrence of ‘a’.
Editing
Ctrl
-x
Ctrl
-u
orCtrl
-/
orCtrl
-_
: Undo the last operation.Alt
-r
: Revert all changes to the current line. For example, if you edited a command from your history, this would revert to the original.Ctrl
-h
orBackspace
: Delete the character to the left of the cursor.Alt
-Backspace
: Cut to the beginning of the current smallword. (From left side of the cursor to the beginning of the smallword. Does not cut leading whitespace.)Ctrl
-d
: Delete the character under the cursor. If the line is empty, exit the terminal. This command fails if the cursor is at the end of a non-empty line.Alt
-d
: Cut from under the cursor to the end of the current smallword.Ctrl
-y
: Paste the most recently cut text.Alt
-y
: Rotate between the most recently cut text. (Only works if the last action was eitherCtrl
-y
orAlt
-y
.)Ctrl
-u
: Cut from the left side of the cursor to the beginning of the line.Ctrl
-k
: Cut from under the cursor to the end of the line.Ctrl
-l
: Clear the screen leaving the current line untouched. Same as the commandclear
, as far as I can tell.Ctrl
-w
: Cut one bigword to the left.Ctrl
-t
: Swap the character under the cursor with the character to the left of the cursor. If this is used at the end of the line, swap the last two characters before the cursor.Alt
-t
: Swap the current smallword with the previous smallword.Ctrl
-v
: Literally insert the next typed character. For example,Ctrl
-v
Tab
will insert a literal Tab character instead of completing the command.Ctrl
-x
Ctrl
-e
: Open the default editor and execute the edited command. (This can be set with the$EDITOR
variable, for example enteringEDITOR=vim
on a separate line will set the editor to Vim.)Alt
-c
: Capitalise the character under the cursor and move to the end of the current smallword.Alt
-u
: Make the current smallword uppercase. (From under the cursor to the end of the current sequence.)Alt
-l
: Make the current smallword all lowercase (from under the cursor to the end of the sequence) and move the cursor to the end the sequence.Alt
-.
orAlt
-_
: Paste the last argument of the previous command.Alt
-Ctrl
-y
: Paste the second bigword of the previous command.Alt
-Ctrl
-e
: Expand the current line. For example,~
Alt
-Ctrl
-e
will expand to/home/$USER
.
Processes
Ctrl
-c
: Terminate the command.Ctrl
-z
: Stop the current process. Typingbg
will restart it in the background andfg
will restore it to the foreground.Ctrl
-s
: Freeze the terminal.Ctrl
-q
: Restore a frozen terminal.Ctrl
-m
orEnter
: Enter the command.Ctrl
-o
: Accept the current line, then fetch the exact same line and print it on the new line.
History
Ctrl
-p
orUp Arrow Key
: Show the previous command in the history.Ctrl
-n
orDown Arrow Key
: Show the next command in the history.Alt
-<
: Move to the first item in the history.Alt
->
: Move to the last item in the history (the current command, not the last executed command).Ctrl
-r
: Reverse-search the history incrementally. For example, pressingCtrl
-r
, then typing ‘string’, then pressingCtrl
-r
,Ctrl
-r
, …, will scroll through all commands in the history containing ‘string’. PressCtrl
-g
to break out of the search.Alt
-p
: Reverse search the history non-incrementally.
The following commands are useful if you want to execute similar commands from your history.
!!
: An alias for the previous command. (Useful forsudo !!
, for example.)!string
: Execute the most recent command in the history beginning with ‘string’.!string:p
: Print the most recent command in the history beginning with ‘string’, so you see what would have been run if you typed!string
.!n
: Execute the nth command in the history, where n is a positive integer. Typehistory
to get a list.!$
: An alias for the last bigword of the previous command.!^
: An alias for the first bigword of the previous command.!*
: An alias for the previous command without the last bigword. (So if the previous command wasls -A /home/$USER
, then!*
would give justls -A
.)!n:p
: Print the nth command in the history.!$:p
,!^:p
, …, also work.^string1^string2
: In the previous command, replace the first occurrence of “string1” with “string2”, then execute.
Completions
Ctrl
-x
~
: Print username completions.Alt
-~
: Attempt username completion.Ctrl
-x
!
: Print command name completions.Alt
-!
: Attempt command name completion.Ctrl
-x
/
: Print filename completions.Alt
-/
: Attempt filename completion.Ctrl
-x
$
: Print shell variable completions.Alt
-$
: Attempt shell variable completion.Ctrl
-x
@
: Show host name completions.Alt
-@
: Attempt hostname completion.Alt
-&
: Perform tilde expansion.Alt
-*
: Insert all possible completions.Alt
-?
: Show the current completion list.Alt
-{
: Insert all filename completions within braces.
Scrolling
Shift
-Page Up
: Scroll up one screen. (This may not work for some terminals. Use your mouse instead if you have one.)Shift
-Page Down
: Scroll down one screen.
The content on this page is in the public domain.