1

If I want to bind a key-mapping to a function or widget in zsh I have learnt that I first have to hit Ctrl+v - at a prompt, then enter the key sequence I want to use, then use the output in my key-binding command.
So for example if I want to map Ctrl+xCtrl+v to the action of opening the current command line contents in an editor, I need to

  • hit Ctrl+v - to enter "dump key mode"
  • hit Ctrl+xCtrl+v
    • In my case this produces ^X^E
  • take the ^X^E and use it in my keybinding command, e.g.
bindkey "^X^E" edit-command-line

Why is this necessary and what is actually happening "behind the scenes" when I do this?

1 Answer 1

5

When you press Ctrl-V, the shell will start by ignoring keyboard interrupts and simply take the pressed key combination as the input character. This is easily possible as ASCII is designed to hold all control characters.

Of course, on display it has to cheat a bit and show the ^ followed by the corresponding key or otherwise it would output control characters instead of what you need to see.

Note that the bindkey documentation shows that it supports two notations for control characters: (examples refer to Ctrl-X)

  • caret notation which is to explicitly write the caret (^) followed by the corresponding control character textually (not needing the Ctrl-V method in this question); example: ^X
  • C- followed by the control character; example: C-x. This causes some key combinations to require escaping (even if you don't use it). You should probably read the whole screen and bindkey manual.
3
  • great answer, thanks. Im am still fuzzy on why the whole Ctrl +v thing is needed. If I understand you correctly and ASCII has defined encodings for these characters couldnt zsh just use these standard values? Commented May 20, 2016 at 4:51
  • It does use them but those are not visible and you obviously want to see what you type in. When you use them in an input string for example, they will act as the actual control character, not their visual representation. Commented May 20, 2016 at 4:56
  • oh I think I see now! so the caret and the <C- > notations are simply to represent non-printable character sequences. I was thinking maybe you had to go through the ctrl v thing because different terminals produced different sequences that needed to be resolved through termcap or terminfo or something! thanks for explaining. Commented May 20, 2016 at 5:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.