14. Bindings

Each of the editor commands we have seen was actually a function bound by default to a certain key. The real names of the commands are:

Example 14.1: Default editor key bindings
expand-or-complete   «Tab»
push-line            «Esc+Q»
run-help             «Esc+H»
accept-and-hold      «Esc+A»
quote-line           «Esc+'»

These bindings are arbitrary; you could change them if you want. For example, to bind accept-line to Ctrl+Z:

Example 14.2: bindkey for Ctrl+Z
% bindkey '^Z' accept-line

Another idea would be to bind the delete key to delete-char; this might be convenient if you use Ctrl+H for backspace.

Example 14.3: bindkey delete-char
% bindkey '^?' delete-char

Or, you could bind Ctrl+XCtrl+H to run-help:

Example 14.4: bindkey Ctrl+X H run-help
% bindkey '^X^H' run-help

Other examples:

Example 14.5: Miscellaneous bindkey examples
% bindkey '^X^Z' universal-argument
% bindkey ' ' magic-space
% bindkey -s '^T' 'uptime
> '
% bindkey '^Q' push-line-or-edit

universal-argument multiplies the next command by 4. Thus Ctrl+XCtrl+ZCtrl+W might delete the last four words on the line. If you bind space to magic-space, then csh-style history expansion is done on the line whenever you press the space bar.

Something that often happens is that I am typing a multiline command and discover an error in one of the previous lines. In this case, push-line-or-edit will put the entire multiline construct into the editor buffer. If there is only a single line, it is equivalent to push-line.

The -s flag to bindkey specifies that you are binding the key to a string, not a command. Thus bindkey -s '^T' 'uptime\n' lets you VMS lovers get the load average whenever you press Ctrl+T.

If you have a NeXT keyboard, the one with the | and \ keys very inconveniently placed, the following bindings may come in handy:

Example 14.6: NeXT keyboard Alt bindings
% bindkey -s '\e/' '\\'
% bindkey -s '\e=' '|'

Now you can type Alt+/ to get a backslash, and Alt+= to get a vertical bar. This only works inside zsh, of course; bindkey has no effect on the key mappings inside talk or mail, etc.

Some people like to bind Ctrl+S and Ctrl+Q to editor commands. Just binding these has no effect, as the terminal will catch them and use them for flow control. You could unset them as stop and start characters, but most people like to use these for external commands. The solution is to set the NO_FLOW_CONTROL option. This will allow you to bind the start and stop characters to editor commands, while retaining their normal use for external commands.