Last updated:

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

  1. ctrl+a : move your cursor to the beginning of the line
  2. ctrl+e : move your cursor to the end of the line
  3. ctrl+k : delete any characters from your cursor to the end of the line
  4. ctrl+u : delete any characters from your cursor to the beginning of the line
  5. ctrl+w : delete the previous word
  6. ctrl+t : transpose two previous characters
  7. ctrl+y : yank/recover the last deletion
  8. ctrl+d : delete one character at the cursor position
  9. ctrl+h : delete one character before the cursor
  10. ctrl+f : move forward (or use the right arrow)
  11. ctrl+b : move backward (or use the left arrow)
  12. ctrl+r : find character sequence in history (completion mode)
  13. ctrl+g : escape from completion mode
  14. ctrl+v : Literal next (LNEXT)

👉 LNEXT interprets the next character as a string. eg: to symbolize a CR+LF you must use the key combination ctrl+v return which will print the special character ^M.

Meta Keys combinations

  1. Meta+d : delete from the cursor position to the end of the word
  2. Meta+f : move forward one word
  3. Meta+b : move backward one word
  4. 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

  1. Use up/down arrows to move through the bash command history
  2. Use left/right arrows to move on the current line
  3. When shell auto-completion is enabled, use the tabulation key tab to auto-complete a command name, hostname, or filename
  4. 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