Learning some of the Bash shortcuts is essential to be effective at typing command-lines on a terminal. A Bash shell interactive session provides ways to manipulate text as you type it in. Those shortcuts allow us to fix typos without having to retype a full command line. By default, the bash shortcuts use the GNU readline library and are configured with Emacs-like keybindings. Those shortcuts can be easily customized.
Default Emacs-like shortcuts
Control Keys combinations
- ctrl+a : move your cursor to the beginning of the line
- ctrl+e : move your cursor to the end of the line
- ctrl+k : delete any characters from your cursor to the end of the line
- ctrl+u : delete any characters from your cursor to the beginning of the line
- ctrl+w : delete the previous word
- ctrl+t : transpose two previous characters
- ctrl+y : yank/recover the last deletion
- ctrl+d : delete one character at the cursor position
- ctrl+h : delete one character before the cursor
- ctrl+f : move forward (or use the right arrow)
- ctrl+b : move backward (or use the left arrow)
- ctrl+r : find character sequence in history (completion mode)
- ctrl+g : escape from completion mode
- ctrl+v : Literal next (LNEXT)
👉
LNEXT
interprets the next character as a string. eg: to symbolize aCR+LF
you must use the key combination ctrl+v return which will print the special character^M
.
Meta Keys combinations
- Meta+d : delete from the cursor position to the end of the word
- Meta+f : move forward one word
- Meta+b : move backward one word
- Meta+t : transpose two adjacent words
On Mac, the default meta key may be the esc key. You can change this to the more convenient option key (alt key), see my post How To Use Option As Meta Key In MacOS Terminal?
Other common key actions
- Use up/down arrows to move through the bash command history
- Use left/right arrows to move on the current line
- When shell auto-completion is enabled, use the tabulation key tab to auto-complete a command name, hostname, or filename
- When bash history is enabled, use the exclamation key + command name to repeat the last similar command (ex. :
!vi
will recall the last vi command)
Custom Key Binding with the Readline Variables: INPUTRC, READLINE_LINE, and READLINE_POINT
The GNU readline library, in an interactive shell session, is used to manipulate text as you type it in. The default Emacs-like keybindings in Bash can be changed by using the default ~/.inputrc
readline configuration file or with a different file defined by the $INPUTRC
shell variable. On a Linux system, you will generally find the system-wide readline settings in the /etc/inputrc
configuration file, you must make sure to include this file in your ~/.inputrc
file if you want to preserve system-wide settings.
The readline editing will be disabled if the bash shell is started with the --noediting
option. It can also be enabled or disabled during an active session by using the set builtin
with set -o emacs
for the Emacs-like bindings and set -o vi
for the Vi-like bindings.
👉 The readline library is also used in Bash with the
read
builtin command when using the read-e
option.
The $READLINE_LINE and $READLINE_POINT can be used when configuring new keybinding with the bind -x
builtin. The $READLINE_LINE
variable contains the Readline line buffer and the $READLINE_POINT
variable holds the position of the insertion point in the Readline line buffer.
Below is a simple example of a readline custom .inputrc
file. You can find the full list of the readline option used in the
GNU Readline manual.
# Preserve system wide settings
$include /etc/inputrc
# Ensure all binding keeps the default Emacs behavior
# Most common in a Bash shell environment, even for vi users
set editing-mode emacs
# do not bell on tab-completion (use the visible bell)
set bell-style visible
# Indicate file types with different colors when
# doing auto-completion. Use the LS_COLORS variable.
set colored-stats On
# ignore case when doing auto-completion
set completion-ignore-case on
# Set the search commands backward and forward
# respectivalye to ctrl-p and ctrl-n
"\C-p":history-search-backward
"\C-n":history-search-forward