Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Customizing Command-Line Editing

The bind command enables you to change the editing keys, editing options, as well as create keyboard macros. Changes affect only the current editing mode.

The -p or -P switches display the various command-line editing functions—the key combinations that will activate them. The -P switch displays the information in an easy-to-read format.

$ shopt -o emacs
emacs      on
$ bind -P | head -5

abort can be found on "\C-g", "\C-x\C-g", "\e\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys
$ shopt -s -o vi
$ bind -P | head -5

abort is not bound to any keys
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys

A particular edit mode can be chosen with the –m (keymap) switch.

$ bind -P -m vi | head -5

abort can be found on "\C-g".
accept-line can be found on "\C-j", "\C-m".
alias-expand-line is not bound to any keys
arrow-key-prefix is not bound to any keys

Special keys are indicated by backslash codes. These codes are based on the ones used by the emacs text editor.

  • \\—A backslash

  • \"—A double quote

  • \'—A single quote

  • \a—An alert (bell)

  • \b—A backspace

  • \C—The Control key

  • \d—Delete

  • \e—The Escape key

  • \f—A form feed

  • \M—The emacs meta key

  • \n—A new line (line feed)

  • \r—A carriage return

  • \t—A horizontal tab

  • \v—A vertical tab

  • \nnn—ASCII code in octal format

  • \xnnn—ASCII code in the hexadecimal format

For example, control-g is represented by the sequence \C-g.

The -l (list) switch lists all possible keyboard functions.

$ bind -l | head -5
abort
accept-line
alias-expand-line
arrow-key-prefix
backward-char

To view a particular function, use the -q (query) switch.

$ shopt -s -o emacs
$ bind -q abort
abort can be invoked via "\C-g", "\C-x\C-g", "\e\C-g".

A binding can be removed using -u (unbind). If there is more than one binding, the first one is removed.

$ bind -u abort
$ bind -q abort
abort can be invoked via "\C-x\C-g", "\e\C-g"

Alternatively, the -r (remove) removes a binding by its key sequence.

$ bind -r "\e\C-g"
$ bind -q abort
abort can be invoked via "\C-x\C-g".

New bindings can be added using the key sequence, a colon, and the name of the editing function. For example, the backward-kill-line function erases everything from the beginning of the line to the cursor position. In vi mode, backward-kill-line isn't normally bound to any keys so it can't be used.

$ shopt -s -o vi
$ bind -q backward-kill-line
backward-kill-line is not bound to any keys.

However, bind can assign backward-kill-line to a new key combination.

$ bind "\C-w:backward-kill-line"
$ bind -q backward-kill-line
backward-kill-line can be invoked via "\C-w".

Now control-w erases to the start of the line.

Besides keys that activate specific edit functions, there are also a number of options (or variables). The -v or -V switches show the keyboard options. -V is easier to read.

$ bind -v | head -5
set blink-matching-paren on
set completion-ignore-case off
set convert-meta on
set disable-completion off
set enable-keypad off
$ bind -V | head -5
blink-matching-paren is set to 'on'
completion-ignore-case is set to 'off'
convert-meta is set to 'on'
disable-completion is set to 'off'
enable-keypad is set to 'off'

These options can be turned on and off.

$ bind "set enable-keypad on"
$ bind -V | head -5
blink-matching-paren is set to 'on'
completion-ignore-case is set to 'off'
convert-meta is set to 'on'
disable-completion is set to 'off'
enable-keypad is set to 'on'

A complete listing of all functions and options is available on the Readline manual page.

The bind command can also define keyboard macros. These are short sequences of keys that are automatically expanded to a longer, common sequence to reduce the amount of typing. The -s or -S switches list all currently defined macros. The -V switch is more "human readable."

The format for creating a macro is the same as the one used to assign functions, except that \" must appear around the expanded text.

$ bind -S
$ bind "\C-w:\" >/dev/null\""
$ bind -S
\C-w outputs >/dev/null

Control-w will now insert >dev/null into the current line.

  • + Share This
  • 🔖 Save To Your Account