Bash Shortcuts

2 minute read

Published:

I tend to spend quite a bit of my time on a command line, so I am always looking for little ways to make my life easier. Because of that, this post will be a bit of a living document that I plan to occasionally update as I discover more handy tricks. This is not a comprehensive list, nor is it intended to be one. These are shortcuts that allow me to have a smoother workflow.

Note: The ^ is shorthand notation for Ctrl, or the Control Key. If a capital letter is specified that means you need to hold down Shift as well. For example, the hotkey ‘^C’ means you would hold down Ctrl + Shift + c.

Bash Hotkeys

^CCopy
^VPaste
^aGo to the beginning of the line.
^eGo to the end of the line.
^lClear the Screen.
^wDelete the word before the cursor.
^uClears the line before the cursor position.
^kClear the line after the cursor.
^dExit the current shell.
^zSuspend current process; fg restores it.
^cKill current process (sends the SIGINT signal).
^tSwap the last two characters before the cursor.
Esc + tSwap the last two words before the cursor.
Alt + fMove cursor forward one word on the current line.
Alt + bMove cursor backward one word on the current line.
TabAuto-complete files,folder names, and command names.

Bash History

!!Run the last command again; this can be useful when combined with command substitution, e.g. for i in $(!!); do STUFF; done
!keywordReruns the last command starting with keyword. For example, if the last command I ran starting with ‘ser’ was service sshd restart, !ser would run that command.
!#Where # is the number that corresponds to that command in history.
!$Final argument of the previous command.
!^First argument of the previous command.
Esc + .Final argument of the previous command; hitting ‘Esc + .’ multiple times will cycle through the last argument history.
^rSearch through previously used commands.

Special Bash Variables

$$PID of the current shell.
$!PID of the last job run in the background.
$_Final argument of the previous command.
$?Exit status of the previous command.

SSH Sessions

~.Terminate SSH session; ideal for hung sessions.
~^zSuspend the SSH session; type fg to resume it.
~?Display the supported escape sequences.

Note: SSH escape sequences are only valid after a new line.