Useful terminal shortcuts

 · 2 min · torgeir

Useful default terminal shortcuts I can't live without.

Terminal Iterm

The GitHub Engineering blog gave a nice list of CLI tricks every developer should know the other day. Here’s a quick recap:

  • Ctrl-c: Cancel the current command
  • Ctrl-a: Move the cursor to the beginning of the line
  • Ctrl-e: Move the cursor to the end of the line
  • Ctrl-l: Clear the terminal screen
  • Ctrl-d: Exit shell session
  • TAB: Auto-complete commands, file names, and paths
  • history: Type this in the terminal to access the history
  • !!: Execute the last command
  • Ctrl-r: Reverse search for a previously executed command
  • Ctrl-n: Move to the next command in the history
  • Ctrl-p: Move to the previous command in the history
  • Ctrl-g: Abort search
  • Ctrl-_: Undo the last edit

Here’s a couple left out, that I personally find really useful.

  • Ctrl-u: Delete to start of line. Can also be undone with Ctrl-_.
  • Ctrl-k: Delete from cursor to end of line. Can also be undone with Ctrl-_.
  • Ctrl-y: Yank the deleted text back in place (aka paste). Can also be undone with Ctrl-_.
  • Ctrl-z: Move running interactive application to the background. Bring it back by typing fg.

And a couple using Meta (or Alt/Option).

  • Meta-b: Move back, a word at a time
  • Meta-f: Move forward, a word at a time
  • Ctrl-w: Delete backward, a word at a time. These can also be yanked back with Ctrl-Y or undone with Ctrl-_
  • Meta-d: Delete forward, a word at a time. These can also be yanked back with Ctrl-Y or undone with Ctrl-_

In iTerm.app you need to set the left or right option key to Meta or Esc+ in the Profiles -> Keys -> General menu to bring back these:

  • Meta-t: Transpose words, i.e. swap a word with the previous one. Can be undone with Ctrl-_. Will respect word separators, so that one=two followed by Meta-t will result in two=one if = is considered a word separator.

In zsh you can adjust the exported WORDCHARS environment variable to tune what is considered word separators.

Btw, most if not all of these work by default in Emacs as well 😉


Edit: Lowercased all commands <2023-05-06 Sat>