necrux - Blog

Bash Shortcuts

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

^C Copy
^V Paste
^a Go to the beginning of the line.
^e Go to the end of the line.
^l Clear the Screen.
^w Delete the word before the cursor.
^u Clears the line before the cursor position.
^k Clear the line after the cursor.
^d Exit the current shell.
^z Suspend current process; fg restores it.
^c Kill current process (sends the SIGINT signal).
^t Swap the last two characters before the cursor.
Esc + t Swap the last two words before the cursor.
Alt + f Move cursor forward one word on the current line.
Alt + b Move cursor backward one word on the current line.
Tab Auto-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
!keyword Reruns 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.
^r Search 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.
~^z Suspend the SSH session; type fg to resume it.
~? Display the supported escape sequences.

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