Pages

Tuesday, November 30, 2010

Set key bindings for C shell

To bind a sequence of keys to a particular shell command (for example use Ctrl+R, to search the history backwards, as in bash, or other useful functions) add the following to ~/.cshrc :

# Nice key bindings
bindkey -b ^r history-search-backward
bindkey -b ^w backward-delete-word
bindkey -b ^u backward-kill-line
bindkey -b ^b backward-word     # Ctrl + b
bindkey -b ^f forward-word      # Ctrl + f
bindkey '^[OD' backward-word    # Ctrl + Left
bindkey '^[OC' forward-word     # Ctrl + Right
bindkey '\e[1;5D' backward-word # ~ for xterm
bindkey '\e[1;5C' forward-word  # ~

How to obtain the key sequence from a function key, or combination of keys: use the read command. 
Example for pressing Ctrl + Left sequence:
$ read
^[[1;5D
Example for pressing Esc sequence:
$ read
^[
Make sure you write the key sequence as \e[1;5D  rather than ^[[1;5D. The ^[ sequence is equivalent to the [Esc] key, which is represented by \e in the shell. So, for instance, if the key sequence was ^[[OP the resulting bind code to use would be \e[OP.
The same key sequence for a function key or combination can be obtained by pressing Ctrl+V and then the key or sequence.

1 comment: