ubuntu, updated

Terminal shortcuts and history

The keyboard shortcuts, history tricks and small habits that make the Bash terminal fast.

Bash, sh and the terminal

Keyboard shortcuts

KeysDoes
Tab / Tab TabComplete a command or path / show all options
Ctrl+RSearch your history; press again for older matches
Ctrl+A / Ctrl+EJump to start / end of line
Alt+B / Alt+FJump one word back / forward
Ctrl+WDelete the word before the cursor
Ctrl+U / Ctrl+KDelete to start / to end of line
Ctrl+YPaste what you just deleted
Alt+.Insert the last argument of the previous command
Ctrl+LClear the screen
Ctrl+CCancel the running command
Ctrl+DLog out (end of input)
Ctrl+ZPause the running command (fg resumes it)

History tricks

TypeMeans
!!The last command (sudo !! is the classic)
!$The last argument of the last command
!123Run command number 123 from history
history | grep netplanFind an old command
cd -Go back to the previous folder

Clearing history and keeping secrets out of it: see Shell history.

Small habits that pay off

mkdir -p /srv/app/{logs,data,conf}   # create several folders at once
cp nginx.conf{,.bak}                 # quick backup: nginx.conf -> nginx.conf.bak
watch -n 2 df -h                     # rerun a command every 2 seconds
tail -f /var/log/syslog              # follow a log live (Ctrl+C to stop)
less /var/log/syslog                 # read a file: /search, G = end, q = quit

Aliases

Put your own shortcuts at the end of ~/.bashrc, then run source ~/.bashrc:

alias ll='ls -alFh'
alias ports='sudo ss -tulpn'
alias update='sudo apt update && sudo apt upgrade'

Don’t lose work when SSH drops

Long jobs (upgrades, big copies) die when your SSH connection breaks. Run them inside tmux (installed on Ubuntu Server by default):

WhatCommand
Start a sessiontmux new -s work
Detach (leave it running)Ctrl+B, then D
List sessionstmux ls
Re-attachtmux attach -t work

More in the tmux cheat sheet.

§ home
~/knubertus/home/
1,1 Top