Bash CLI Shortcuts and Tricks
Shortcuts
ctrl-c # Kill a running command
ctrl-d # Exit shell session
tab or tab-tab # Tab Completion
ctrl-L, clear # Clearing your terminal
# Cursor Movement
ctrl-a, ctrl-e # Move to beginning or end of line
alt-f, alt-b # Move forward or back one word
# Search hitory
ctrl-r # Searching through shell history. Repeat ctrl-r for next
ctrl-g # Return back where you're typing
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tricks
sudo !! # Re-run previous command with 'sudo' prepended
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Killing and Yanking text
ctrl-k # Kill (cut) to the end of line
ctrl-y # Yank the text back i.e. what you've just cut
ctrl-u # Kill to the beginning of line
ctrl-w # Kill backwards word-by-word
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A Practical example
# You forgot to type sudo
apt update
# instead of ctrl-a and type 'sudo'
ctrl-u
sudo
ctrl-y
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Replace ‘tail’ with ‘less’
tail -f /var/log/syslog
less +F /var/log/syslog
ctrl-c # detach
shift-f # attach (to the end)
ctrl-c q # exit
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Edit the Current Command in an Editor (ctrl-x-e)
HOSTS="8.8.8.8 4.4.4.4 8.8.4.4"
for host in $HOSTS; do
ctrl-x-e # open editor with what you've typed
# use with vi or emacs
echo $EDITOR
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Paste the Argument of the Previous Command (alt-.)
ping 8.8.8.8
ctrl-c
mtr --curses alt-.
# curses is a text user interface (TUI)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Unbork the Terminal
# You don't want to close the terminal
reset
Index