This is a list of shell shortcuts I’ve found useful, all of which (except the last) are enabled by default on macOS.

While some of these shortcuts are specific to zsh, and a few specific to the macOS Terminal, most should work on a decently configured shell on any platform.

Tested on macOS 12.1 with the default shell of zsh 5.8, using the option key as meta.

Not all shortcuts are included. To list all the shell key bindings and the widgets they call, run bindkey. Widgets are documented in the zshcontrib man page.

The “Emacs” shortcuts

These shortcuts are particularly worth learning because they work in most text boxes on macOS and on the command line for most shells (including zsh, bash, sh, csh, and tcsh) in the Terminal.

shortcut action
ctrla move to beginning of line
ctrle move to end of line
ctrlb move back ()
ctrlf move forward ()
ctrld forward delete
ctrlh backward delete (delete)
ctrlk delete to end of line
ctrlt transpose chars before and after insertion point

The option key modifies the arrow and delete keys to work on words in most text boxes and shells.

shortcut action
opt move to prev word
opt move to next word
optdelete backward delete word

Some of the control key shortcuts above that operate on characters have corresponding meta (option) shortcuts that operate on words.

shortcut action
optb move back by word
optf move forward by word
optd delete next word
ctlopth delete previous word
optt transpose words

Signals

shortcut action
ctlz move job into the background (use fg to restore)
ctlc kill current program
ctld end of file (exit shell)
ctll or optl clear screen
ctls pause display
ctlq unpause display

Shortcuts for the entire line

shortcut action
tab expand or complete
ctlu delete entire line
ctlg send break
optq push line
optg get line
ctl_ undo changes to line

Tab completion and expansion: The tab key can be used to list and select file and directory names to complete the command being entered.

For example, type “ls “ followed by tab. The shell will list the files and directories in your home directory. Pressing tab again allows you to move among the possible selections and pressing return accepts the selected completion. Use ctlg to cancel the completion selection. (The precise behavior of tab completion/expansion depends on configuration options.)


Send break (ctlg) clears the command line and generates an error (a non-zero exit status).


Push line, a great shortcut, temporarily pushes (saves) the text on the command line until you run another command (or send a break).

For example, type a command without pressing enter (like touch foo), then press optq and enter another command (like ls). When you press return the command is executed, then the previous, unexecuted command is restored to the command line. Use optg to restore the pushed line if you decide not to run another command.

History shortcuts

Like the tab completion feature, anyone who has used a terminal in the twenty-first century has probably discovered the basic history mechanism: the up and down keys scroll through a history of previously entered commands.

shortcut action
or ctlp previous history
or ctln next history
ctlr reverse history search
optp previous command match
optn next command match
opt< beginning of history
opt> end of history
ctlo accept history line and go to next

See my previous post about zsh history options that control what is saved to history.

ctlr initiates a reverse (backwards) search through the command line history. Pressing the key again will find an older match.

Note: ctls, to cycle forward through the search, does not work because it’s used to send a signal to the Terminal to pause output. There are ways to change this behavior, but the shortcut is not important enough to me to bother.


optp searches history for prior occurrences of whatever command you’ve typed.

For example, type cd and press optp to list the previous time you used that command. Press again to move back through the list of matches and optn to move forward.


opt< and opt> jump to the first and last line of saved history.


ctlo accepts (executes) the selected line from history and advances to the next command in history. Very useful for replaying a series of commands.

Copy/Paste

The system-wide copy and paste commands work in the Terminal and are listed under the Edit menu, along with other paste options. There are also pbcopy and pbpaste commands to use from the command line.

There are emacs rough equivalents of copy and paste, “kill” and “yank,” which I’ve never mastered, perhaps because of the unfortunate names. The shortcuts exist but will not be covered here.

Helpers

shortcut action
opth run help for command
opt? run which for command
optx execute named command
optz execute last named command

See my previous post about how helpful the run help command is and how to configure it.


opt? runs which for the command entered on the line.

For example, type ls and then press opt?. The which command will be run, showing /bin/ls (assuming you haven’t aliased ls), and then the ls command will be restored to the line.


Press optx and type the name of a zle widget to execute. I used this a lot to run describe-key-briefly to check key bindings while writing this. Press optz to run the named command again.

Edit command line

There’s one shortcut not enabled by default that is too essential to leave off this list: Edit Command Line.

Assuming you’ve set the environmental $VISUAL variable, you can use a shortcut (opte in this example) to edit the command line with your preferred text editor by adding these lines to your .zshrc configuration:

autoload -Uz edit-command-line
zle -N edit-command-line
bindkey "^[e" edit-command-line

Tags: ,

Categories:

Published: