macOS Catalina introduced zsh as the default interactive shell. zsh is compatible with Bourne shell (sh) to a large degree and introduces many valuable extensions that make everyday productivity more pleasant. In this post, I list some shortcuts for the zsh line editor (also known as the command prompt) in its default configuration.
Keyboard Shortcuts
When using zsh as an interactive shell, there are a variety of keyboard shortcuts that you can use to edit text more quickly, and mostly without having to blindly reach for the arrow keys or press backspace too many times to delete words. The keyboard shortcuts are modeled after EMACS and readline. Zsh also has a vi emulation mode, which I will not cover in this post, but you can read here for more definitely there,
If you are on a Mac, the CTRL
key will usually be called control
on your
keyboard if you use an Apple keyboard. Other vendors usually call it Ctrl,
or
if you use a German QWERTZ keyboard layout perhaps it is called Strg.
The bindkey
name indicates the zsh internal name of the shortcut.
Key | Meaning | bindkey name |
---|---|---|
CTRL + H |
Delete character left of cursor | backward-delete-char |
CTRL + W |
Delete character left of cursor | backward-kill-word |
CTRL + U |
Delete whole line | kill-whole-line |
CTRL + K |
Delete everything left of cursor | kill-line |
CTRL + M |
Execute command | accept-line |
CTRL + L |
Clear screen | clear-screen |
CTRL + I |
Auto-complete | expand-or-complete |
CTRL + A |
Go to beginning of line | beginning-of-line |
CTRL + E |
Go to end of line | end-of-line |
CTRL + P |
Go up or recall previous command | up-line-or-history |
CTRL + N |
Go down or recall next command | down-line-or-history |
CTRL + B |
Go backward one character | backward-char |
CTRL + F |
Go forward one character | forward-char |
CTRL + Y |
Paste deleted text | yank |
CTRL + _ |
Undo | undo |
CTRL + R |
Search backward in history | history-incremental-search-backward |
CTRL + S |
Search forward in history | history-incremental-search-forward |
Reviewing configured shortcuts
Do you have any issues using the shortcuts above? Running bindkey
in an
interactive zsh shell will reveal the currently configured shortcuts. This is
useful if some of the shortcuts you find online don’t work, and you would like
to find out whether they are configured. If your key configuration is messed
up, you can at least temporarily reset it to the Emacs keymap by running
bindkey -e
You might want to review your zsh configuration, in case the keymap is incorrectly configured there.
Bonus round: Re-run the last command
Here are two ways you can re-execute the last command, in case you need to
change one or two things about it. For this example, let’s say your last
command was file /bin/sh
, then
sudo !!
will re-rerun the command as sudo file /bin/sh
, and
^sh^bash
will re-run the command as file /bin/bash
. Handy!
Alternatives to zsh
You may also want to give fish a spin. I have been using it for the last 8 years, and I am a happy user.