bind

Display current readline key and function bindings, bind a key sequence to a readline function or macro, or set a readline variable.

Syntax
      bind [-m keymap] [-lpsvPSV] 
      bind [-m keymap] [-q function] [-u function] [-r keyseq] 
      bind [-m keymap] -f filename 
      bind [-m keymap] -x keyseq:shell-command 
      bind [-m keymap] keyseq:function-name or readline-command

Each non-option argument is a command as it would appear in .inputrc, but each binding or command must be passed as a separate argument; e.g., '"\C-x\C-r": re-read-init-file'.

Key

   -m keymap    Use keymap as the keymap to be affected by the subsequent bindings.
                Acceptable keymap names are emacs, emacs-standard, emacs-meta, emacs-ctlx, vi,
                vi-move, vi-command, and vi-insert. vi is equivalent to vi-command;
                emacs is equivalent to emacs-standard.

   -l           List the names of all readline functions.

   -p           Display readline function names and bindings in such a way that they can be re-read.

   -P           List current readline function names and bindings.

   -s           Display readline key sequences bound to macros and the strings they output in such a way that
                they can be re-read.

   -S           Display readline key sequences bound to macros and the strings they output.

   -v           Display readline variable names and values in such a way that they can be re-read.

   -V           List current readline variable names and values.

   -f filename  Read key bindings from filename.

   -q function  Query about which keys invoke the named function.

   -u function  Unbind all keys bound to the named function.

   -r keyseq    Remove any current binding for keyseq.

   -x keyseq:shell-command   Cause shell-command to be executed whenever keyseq is entered.
                When shell-command is executed, the shell sets the READLINE_LINE variable to the
                contents of the readline line buffer and the READLINE_POINT variable to the current
                location of the insertion point. If the executed command changes the value of
                READLINE_LINE or READLINE_POINT, those new values will be reflected in the editing state.

The return value is 0 unless an unrecognized option is given or an error occurred.

You can determine the character sequence emitted by a key by pressing Ctrl-v at the command line, then pressing the key you're interested in. Alternatively run the read command and then press the key(s) of interest, Control-c will return from the read builtin command.

The \e is the escape character, displayed as ^[ in the shell output.

bind key mappings can be abbreviated by ommiting the bind command and the outer set of single quotes.

bind is an internal command; but can also be installed manually from ISC.org or using homebrew: brew install bind, a homebrew installation will be placed in /usr/local/sbin/ [example of config].

Examples

Setup the shell to perform a history search when the Up and Down arrow keys are used.
This allows typing part of a command and then using the arrows to select matching commands from history.
(by default Up and Down will just select previous commands without any matching against history.)

bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'
bind '"\e[C": forward-char'
bind '"\e[D": backward-char'

The last two bindings ensure that the left and right keys continue to work correctly.

“No cord or cable can draw so forcibly, or bind so fast, as love can do with a single thread” ~ Robert Burton

Related macOS commands

Startup profile - Set bind commands at every login.
Bindable Readline Commands
Readline keyboard shortcuts - GitHub.


 
Copyright © 1999-2024 SS64.com
Some rights reserved