Bash Tricks

Bash Keyboard Shortcuts

readline shortcuts for moving around a line, cutting and yanking text, and reverse-searching your history. they work in bash and mostly in zsh too. you don’t need to memorize all of them — just pick a few and they’ll become muscle memory in like a week.

moving the cursor: Ctrl+A, Ctrl+E, Alt+F, Alt+B

  • Ctrl+A — jump to start of line
  • Ctrl+E — jump to end
  • Alt+F / Alt+B — move forward/back one word

I remapped caps lock to ctrl years ago and it made all of these way more comfortable. highly recommend.

cutting and pasting: Ctrl+W, Ctrl+U, Ctrl+K, Ctrl+Y

  • Ctrl+W — delete word before cursor
  • Ctrl+U — nuke everything before cursor
  • Ctrl+K — nuke everything after cursor
  • Ctrl+Y — paste back what you just nuked
Ctrl+ACtrl+E./deploy.sh productionCtrl+U nuke beforeCtrl+K nuke afterCtrl+Y pastes back what you nuked

the nice thing is these are actually cut operations, not delete. so Ctrl+U then Ctrl+Y is basically cut and paste. I use this when I start typing a command, realize I need to do something else first, Ctrl+U to stash it, do the other thing, then Ctrl+Y to bring it back.

Ctrl+R — reverse-searching your history

reverse search through your history. just start typing and it finds matching commands:

(reverse-i-search)`deploy': ./deploy.sh production

press Ctrl+R again to go further back. Enter to run it, Ctrl+G to bail out.

this replaces like 90% of what you’d use Bash History Expansion for honestly.

Alt+. — inserting the last argument of the previous command

inserts the last argument of the previous command. same as !$ but interactive — and if you keep pressing it, it cycles through older commands’ last args. super handy.

Ctrl+C, Ctrl+Z, Ctrl+D, Ctrl+L — controlling a running command

  • Ctrl+C — kill it
  • Ctrl+Z — suspend it (then bg or fg, see Bash Process Tricks)
  • Ctrl+D — EOF / close shell
  • Ctrl+L — clear screen

see also: Bash History Expansion, Bash Process Tricks, Bash Quick Loops

more shortcuts than you’ll ever need: catonmat readline cheat sheet