Command Line Text Navigation Shortcuts
In this article
Keyboard shortcuts for text navigation and manipulation in the command line let you move the cursor, edit commands, and recall history without touching the mouse. This article covers the essential shortcuts for Bash and Zsh, the two most common Unix-like shells, and explains how they work so you can use them effectively.
Why Learn Command Line Shortcuts?
Working in a terminal often means typing long commands, fixing typos, and re-running previous commands. Using keyboard shortcuts instead of arrow keys or a mouse saves time and reduces errors. Once you memorize a few key combinations, you can edit commands as fast as you think them. These shortcuts are especially valuable for developers, system administrators, and anyone who spends hours in a terminal.
Core Navigation Shortcuts
The most fundamental shortcuts move the cursor without deleting anything. In Bash and Zsh, these are based on the GNU Readline library, which provides a consistent set of key bindings.
- Ctrl + A – Move cursor to the beginning of the line.
- Ctrl + E – Move cursor to the end of the line.
- Ctrl + F – Move forward one character (same as right arrow).
- Ctrl + B – Move backward one character (same as left arrow).
- Alt + F – Move forward one word.
- Alt + B – Move backward one word.
These shortcuts work in most shells and in many command-line applications that use Readline, such as the Python REPL and MySQL client. The Alt key may be replaced by Esc on some systems; for example, pressing Esc then F is equivalent to Alt+F.
Editing and Manipulation Shortcuts
Editing shortcuts let you delete, transpose, and change case without moving your hands to the mouse. They are crucial for correcting typos quickly.
- Ctrl + D – Delete the character under the cursor (if the line is empty, it exits the shell).
- Ctrl + H – Delete the character before the cursor (same as backspace).
- Alt + D – Delete from the cursor to the end of the current word.
- Alt + Backspace – Delete from the cursor to the beginning of the current word.
- Ctrl + W – Delete from the cursor to the beginning of the previous word (in Bash, it deletes only up to the previous space).
- Ctrl + U – Delete from the cursor to the beginning of the line.
- Ctrl + K – Delete from the cursor to the end of the line.
- Ctrl + Y – Paste (yank) the last deleted text at the cursor.
- Ctrl + T – Swap the character under the cursor with the one before it.
- Alt + T – Swap the current word with the previous word.
- Alt + U – Uppercase the current word (from cursor to end of word).
- Alt + L – Lowercase the current word.
- Alt + C – Capitalize the current word (first letter uppercase, rest lowercase).
Note that Ctrl+W in Bash deletes only the previous word (separated by spaces), while in Zsh it deletes up to the previous slash or other punctuation. This difference can be surprising when moving between shells.
History Navigation and Search
Reusing previous commands is a huge time-saver. The shell keeps a history of commands you've typed, and you can navigate it with these shortcuts.
- Up arrow or Ctrl + P – Go to the previous command.
- Down arrow or Ctrl + N – Go to the next command.
- Ctrl + R – Reverse incremental search. Start typing a fragment of a previous command, and the shell shows the most recent match. Press Ctrl+R again to cycle through earlier matches.
- Ctrl + G – Cancel the reverse search.
- Ctrl + S – Forward incremental search (may not work if terminal flow control is enabled; often disabled by default).
- Alt + . (period) – Insert the last argument of the previous command. Repeating it cycles through older commands' last arguments.
To see your full history, type history and press Enter. You can then re-run a command by typing ! (e.g., !42) or !! to run the last command.
How These Shortcuts Work
Most of these shortcuts are provided by the GNU Readline library, which is used by Bash, Zsh (with the zle system), and many other line-editing tools. Readline has two editing modes: emacs mode (default) and vi mode. The shortcuts listed above are from emacs mode. If you prefer vi key bindings, you can switch with set -o vi in Bash or bindkey -v in Zsh. In vi mode, you start in insert mode, and pressing Esc puts you in normal mode where you can use h, j, k, l to move and other vi commands to edit.
You can customize these shortcuts in your ~/.inputrc file for Bash or with bindkey in Zsh. For example, to make Ctrl+Left move back a word in Bash, add:
"\e[1;5C": forward-word
"\e[1;5D": backward-wordThis kind of customization is powerful but requires knowing the escape sequences your terminal sends for special keys.
Common Problems and Misconceptions
One common issue is that some shortcuts don't work in certain terminals or over SSH. For instance, Ctrl+S is often intercepted by terminal flow control (XON/XOFF), which pauses output. To use it for forward search, you may need to disable flow control with stty -ixon.
Another misconception is that these shortcuts work in every command-line program. They work only if the program uses Readline or implements similar bindings. Many custom CLIs (like some interactive tools) use their own key handling, so shortcuts may differ.
Also, Alt+key combinations can be problematic on macOS Terminal because the Option key is often configured to send special characters. You can change this in Terminal's preferences under Keyboard to use Option as Meta key, or use Esc as a prefix instead.
Comparing Bash and Zsh Shortcuts
Bash and Zsh share most shortcuts because both use Readline (Zsh uses its own zle but emulates Readline). However, there are differences:
- In Zsh,
Ctrl+Wdeletes up to the previous slash or punctuation, while Bash deletes up to the previous space. You can change this in Zsh withautoload -U select-word-styleandselect-word-style bash. - Zsh has a
Ctrl+LeftandCtrl+Rightbinding for word movement by default, while Bash requires configuration. - Zsh's history search (
Ctrl+R) is more advanced, allowing multiline commands and better handling of special characters.
If you switch between shells, be aware of these subtle differences. You can make them more consistent by customizing your .inputrc or .zshrc.
Alternatives and Tools
If you find the default shortcuts limiting, consider using a more advanced shell or a terminal multiplexer that adds features:
- Zsh with Oh My Zsh – Adds plugins for syntax highlighting, autosuggestions, and improved history search.
- Fish shell – Has a different set of shortcuts (e.g., Ctrl+F for autocomplete) and is more user-friendly for beginners.
- tmux – A terminal multiplexer that allows you to manage multiple sessions and panes, and it has its own copy-paste shortcuts.
- fzf – A fuzzy finder that can be integrated with Ctrl+R to search history interactively.
These tools build on the basic shortcuts and can greatly enhance productivity, but they require additional setup.
Frequently Asked Questions
How do I undo a deletion in the command line?
There is no built-in undo for deletions, but you can use Ctrl+Y to paste the last deleted text. Readline keeps a kill ring that stores multiple deletions; pressing Alt+Y cycles through them after using Ctrl+Y.
Why doesn't Ctrl+Left work in my terminal?
Ctrl+Left is not a standard Readline shortcut. It is a terminal-specific key sequence that many terminals send as ESC [ 1 ; 5 D. You need to bind that sequence in your .inputrc or .zshrc. Alternatively, use Alt+B and Alt+F which are standard.
Can I use these shortcuts in Windows Command Prompt or PowerShell?
No, these shortcuts are for Unix-like shells. Windows PowerShell has its own shortcuts, such as Ctrl+Left/Right to move by word, but they differ. If you use WSL (Windows Subsystem for Linux), the shortcuts work as in Linux.
How do I make Ctrl+S work for forward search?
Run stty -ixon to disable terminal flow control. This allows Ctrl+S to be sent to the shell, enabling forward incremental search. To make it permanent, add that command to your .bashrc or .zshrc.
What is the difference between Ctrl+W and Alt+Backspace?
Both delete a word to the left of the cursor, but they use different word definitions. Ctrl+W deletes up to the previous space (in Bash), while Alt+Backspace deletes up to the beginning of the current word, which may include punctuation. In Zsh, the behavior may differ as described above.
Are these shortcuts the same in the macOS Terminal?
Yes, they work in macOS Terminal, but you may need to enable the Option key as Meta for Alt+ shortcuts. Go to Terminal > Preferences > Profiles > Keyboard and check “Use Option as Meta key.” Alternatively, use Esc as a prefix (press Esc, then the key).