3

This question might be a little bit out of the ordinary for this site, but I want to better understand how vim works under the hood. I know that if I enter the hex value 0x01 vim will display it as ^A. Does that mean when I hit <ctrl-a> on my keyboard, vim will receive my keystroke as 0x01? Or does it receive it as something totally unrelated, like two separate keys?

Also, this question is somewhat related, but are there any characters (including the extended ascii codes 128-255) that vim does not accept as a valid normal mode keystroke? Which ones, or could you point me to a list of all of them somewhere?

  • Is this just for curiosity? Are you intending to apply it to some task? (XY Problem) – mMontu Feb 1 '16 at 16:33
  • @mMontu Both. I was going to try writing a program that creates a hidden process of vim and then sends keystrokes to it. Just as a hobby project to see if I can. I was going to end up looking in the source code, but I thought I could also ask here. – DJMcMayhem Feb 1 '16 at 16:53
  • Well, that is not usual. Are you sure that vim is the right tool for your automation? Because vim is an interactive editor it will use resources in order to provide an user interface that is likely to only trouble your program. For some specific cases you can pass a command with -c in order to have vim filtering a file. – mMontu Feb 1 '16 at 17:00
  • @mMontu It's not for an automation. More of an experiment. And yeah, I'm sure I want to use vim for it. Thanks for your help! – DJMcMayhem Feb 2 '16 at 0:12
4

The question 20.5 of the Vim FAQ seems to indicate that vim will receive a single character:

20.5. Why does mapping the <C-...> key not work?

The only Ctrl-printable-key chords which Vim can reliably detect (because they
are defined in the ASCII standard) are the following: >

        Ctrl-@                 0x00            NUL
        Ctrl-A to Ctrl-Z       0x01 to 0x1A
        Ctrl-a to Ctrl-z       0x01 to 0x1A
        Ctrl-[                 0x1B            ESC
        Ctrl-\                 0x1C
        Ctrl-]                 0x1D
        Ctrl-^                 0x1E
        Ctrl-_                 0x1F
        Ctrl-?                 0x7F            DEL

But the keyboard input system was subject to several discussion on the vim-dev mailing list:

So you will probably need to read the source code to have a better understanding of this mechanism.

For your second question you could check question 20.4, as any valid normal mode keystroke can be used for mappings:

20.4. I am not able to create a mapping for the <xxx> key. What is wrong?

1) First make sure, the key is passed correctly to Vim. To determine if
   this is the case, put Vim in Insert mode and then hit Ctrl-V (or
   Ctrl-Q if your Ctrl-V is remapped to the paste operation (e.g. on
   Windows if you are using the mswin.vim script file) followed by your
   key.

   If nothing appears in the buffer (and assuming that you have
   'showcmd' on, ^V remains displayed near the bottom right of the Vim
   screen), then Vim doesn't get your key correctly and there is nothing
   to be done, other than selecting a different key for your mapping or
   using GVim, which should recognise the key correctly.

(...)

For more information, read 

    |map-keys-fails|
    |:map-special-keys|
    |key-codes|

Edit:

If your purpose is to pass commands to vim from external tools, you might consider avoid understanding the details on how <ctrl-a> is received by vim, providing the alternative format explained in :help key-codes (e.g.: <C-a> standing for <ctrl-a>), and then using :execute + normal commands, as explained in :help :execute:

    :execute "normal ixxx\<Esc>"

(possibly by inserting such strings on the clipboard and executing it from vim with :@+ or writing to a file and then using :source on it)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.