Bash Keyboard Shortcuts: Readline Keys and Examples
Bash keyboard shortcuts let you edit the current command line, move through text, search command history, and control a running job without reaching for the mouse. Most are provided by Readline, the line-editing library Bash uses; the exact key bindings can vary with your configuration and terminal.
How Bash shortcuts work
At an interactive Bash prompt, Readline interprets keystrokes while you type. Its default editing mode is Emacs-style, even if you do not use the Emacs editor. In this mode, many shortcuts use Ctrl or Alt together with another key. In shortcut notation, Ctrl+A means hold Ctrl and press A; Alt+F means hold Alt and press F.
Some terminals send the same control character for keys that look different, and Alt may be handled as an Escape prefix. For example, pressing Esc and then F can perform the same Readline command as Alt+F. Press the keys in sequence rather than holding them together when using this Escape form.
These shortcuts edit the command you are entering; they do not change the command’s meaning after it runs. They are useful at a Bash prompt and in some programs that also use Readline, but not every terminal application supports them.
Essential line-editing shortcuts
These default Emacs-mode bindings cover common edits. “Word” boundaries can depend on the characters in the command and Readline’s configuration.
Ctrl+A: move to the beginning of the line.Ctrl+E: move to the end of the line.Ctrl+BandCtrl+F: move back or forward one character.Alt+BandAlt+F: move back or forward one word.Ctrl+D: delete the character under the cursor. On an empty prompt, it may signal end-of-file and close the shell.Backspace: delete the character before the cursor.Ctrl+K: cut from the cursor to the end of the line.Ctrl+U: cut from the cursor to the beginning of the line.Alt+D: cut forward to the end of the next word.Ctrl+W: cut backward over the preceding word.Ctrl+Y: paste (yank) the most recently cut text.
For example, if the line is grep -r error /var/log and the cursor is at the end, Ctrl+W removes the preceding word, while Ctrl+U clears everything from the cursor to the start. Read the result before pressing Enter, especially after a large deletion.
History, completion, and corrections
History shortcuts help reuse commands without retyping. The up and down arrow keys move through recent commands. Ctrl+P and Ctrl+N move to the previous and next history entries, respectively. Ctrl+R starts a reverse search through history: type part of a previous command to find a match, press Ctrl+R again to look farther back, and press Enter to run the selected command. Press an arrow key or another editing key to bring a match onto the prompt for editing instead.
Tab: complete a filename, directory, command, or other available word. Press it again when you want Bash to show possible matches.Alt+.: insert the final argument from the previous history entry. Press repeatedly to cycle through earlier entries’ final arguments.Ctrl+L: clear the visible terminal area and redraw the prompt; it does not erase command history.Ctrl+_(often Ctrl+Shift+-): undo the last editing change. The key combination may vary by keyboard layout.
History search is convenient, but a recalled command is not automatically safe to rerun. Check paths, options, and arguments first; a command that was appropriate in one directory or situation may have different effects now.
Shortcuts for running commands
Some familiar control keys act on the running foreground program rather than merely editing the prompt. Their effect depends on the program and terminal settings, but these are common defaults:
Ctrl+C: send an interrupt signal to the foreground process. Many commands stop, but a program can handle or ignore the signal.Ctrl+Z: send a suspend signal to the foreground job. Bash usually returns to a prompt; usefgto resume that job in the foreground orbgto continue it in the background.Ctrl+D: at an empty shell prompt, signal end-of-file, which normally exits that interactive shell. It is not the same as interrupting a running command.
Use Ctrl+C when you intend to interrupt rather than suspend a command. Do not assume that pressing it reverses work already completed: a process may have changed files or performed other actions before it stops.
Emacs mode, Vi mode, and customization
Readline supports two main editing styles. Emacs mode is the default in a typical Bash setup. To use Vi-style command-line editing for the current shell, run set -o vi; to return to Emacs mode, run set -o emacs. Vi mode separates insertion and command-like editing states, so its keystrokes differ from the Emacs shortcuts listed above.
Bindings can also be customized through Readline settings, commonly placed in ~/.inputrc. Bash-specific startup configuration can select a mode or adjust related behavior. A shortcut that differs from the defaults may therefore reflect a personal configuration, a different shell, or a terminal program with its own key handling.
To inspect Readline’s active bindings, use bind -P in Bash. For help on an available editing function, bind -P output lists function names and assigned keys; the bind -p form prints bindings in a format suitable for an input configuration file. Avoid changing a global configuration just to fix one application until you have checked whether the application uses Readline at all.
When shortcuts do not work
If a key combination produces an unexpected character, does nothing, or triggers a terminal action, check the layer handling the keystroke. A terminal emulator may reserve shortcuts, a remote connection may alter key transmission, or the current application may use its own bindings. Try the Escape-prefix form for an Alt shortcut, and test the key at a plain Bash prompt.
- Confirm that the shell is Bash rather than another shell with different defaults.
- Check the active editing mode with
set -o; look foremacsorvi. - Inspect bindings with
bind -Pand review~/.inputrcif you have customized Readline. - Check the terminal emulator’s shortcut settings if a key is intercepted before Bash receives it.
- Remember that
Ctrl+CandCtrl+Zaffect a foreground job, not the text-editing cursor.
Also distinguish Bash shortcuts from shell syntax. For instance, Ctrl+R searches command history, while typing !! is a history expansion feature. They are related ways to reuse commands, but they are not the same mechanism.
Frequently Asked Questions
What is the shortcut to clear the Bash command line?Ctrl+U removes text from the cursor back to the start of the line. If the cursor is at the end, it clears the whole line. Ctrl+L clears the terminal display but leaves the command line and history conceptually separate.
How do I search Bash command history?
Press Ctrl+R, type a distinctive part of the command, and press Ctrl+R again to search older matches. Press Enter to run the match, or edit it first after bringing it back to the prompt.
How do I move to the beginning or end of a command?
Use Ctrl+A to go to the beginning of the current line and Ctrl+E to go to its end. These are Readline editing commands, not commands entered into Bash.
How do I stop a Bash command?
Usually, press Ctrl+C to interrupt the foreground process. Ctrl+Z suspends a job instead; it does not necessarily terminate it.
Why does Ctrl+D close Bash?
At an empty prompt, Ctrl+D sends an end-of-file indication, which normally exits an interactive shell. With text on the line, its default Readline action is to delete the character under the cursor.
Can I use Ctrl+W and Ctrl+U in Vi mode?
They are familiar Emacs-mode Readline bindings and may not behave the same way in Vi mode. Check the active mode and its bindings, or switch with set -o emacs if you want the default Emacs-style editing behavior.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.