Useful zsh Shortcuts

Published: March 10, 2023, updated: January 4, 2025

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 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 don’t cover in this post. You can this document here for more information about the vi emulation mode in zsh.

If you are using an Apple keyboard on a Mac, you may see a control key instead of a CTRL key. Other vendors call it Ctrl, or if you use a German QWERTZ keyboard layout it’s likely 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 these shortcuts? Running bindkey in an interactive zsh shell reveals 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 !!

re-reruns the command as sudo file /bin/sh, and

^sh^bash

re-runs the command as file /bin/bash. This is 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.

Tags

I would be thrilled to hear from you! Please share your thoughts and ideas with me via email.

Back to Index