1

I normally use Vim on Windows and have had no problems with key-mapping, but now that I'm using a virtual machine running Ubuntu, accessed through MobaXterm, things don't quite work as they should. That includes basic things like <c-s> for save and <F5> for run, which I can't live without. I've done my research and read materials that seem to address this problem (here and here), but they went well over my head. Frankly, having to press <c-v> before a function key is downright weird. And it didn't even work when I tried. Can somebody explain, please? As a test case, if I want to map <F2> to :pwd, what do I have to do?

To my delight, I've found that my key-mappings do work as intended on Ubuntu's gVim, so I'll be using that. I still would like the answer, though, because I actually prefer the quick and easy feel of plain old vim on the terminal.

  • I'm not familiar with MobaXterm. It looks like a packaged version of some open-source things like X and xterm; but I wonder if you might have more luck just running X in the VM and running xterm (or one of the million other terminals) in it. – echristopherson Nov 6 '14 at 23:13
1

You'll have to live without ctrlS because that is the terminal command to stop output (ctrlQ undoes that).

Other function keys should be mappable without any problem, just enter :map , then hit the function key you want to map which should show e.g.<F5> for F5, then space, then what you want the key to map to.

You can put that line (without the leading colon) in your ~/.vimrc file to enable the mapping for every vim session.

If you've tried that then please edit your question to show what exactly you tried and what the result was (and how that differed from your expectations).

EDIT: If you want to map sequences that aren't defined in the terminal definition, then you can manually map the sequence.

First you need to find out what bytes/characters it sent when you press e.g. ctrlshiftF2. I always use od -c for this; start the command, press the key sequence, hit ctrld to send an end-of-file to the command, which then prints the decoded version:

$ od -c
^[[24^
0000000 033   [   2   4   ^  \n
0000006

So this is escape, [, 2, 4, ^ (the newline is what I entered after the sequence and should be ignored; you can also hit ctrld twice but then the output is started after the input and looks messed up).

Now we know the sequence, and can add the mapping to .vimrc. Add a line such as the following:

map <C-[>[24^ :whateveryouwant

The <C-[> sequence is the vim representation of escape, which is the same as ctrl[. After that the characters aren't special so they can be entered as-is.

Now ctrlshiftF2 will be mapped to the right hand side you entered when you start vim.

  • 1
    Actually, you can make the terminal ignore ^S/^Q for flow control by running stty -ixon (and preferably putting it in your shell startup file). – echristopherson Nov 6 '14 at 23:12
  • Thanks for addressing the <c-s> limitation, but your suggestion is what I have already. Of course, I put my usual mappings in my .vimrc, the problem is that some of them don't work on terminal Vim. They do work in gVim (Ubuntu), though. The second link in the original post says "In the GUI version of Vim (gvim), the mapping of keys seem to work for the most part... for terminal versions of Vim, mapping something like Ctrl-Shift-F2 needs some extra work.", so it's a known issue. I'm just curious why. – bongbang Nov 7 '14 at 4:10
  • Many thanks for the detailed addition. Should I take it, then, that the function keys aren't in my terminal definition, and that's why my straightforward mapping for <F5> (no shift, no ctrl) wasn't working? – bongbang Nov 10 '14 at 7:51
  • You can use the infocmp command to generate a readable version of your terminal's definition. The kfXX entries are the keyboard function definitions, <F5> is kf5. \E is the representation of the escape character. Compare that to what od -c displays. – wurtel Nov 10 '14 at 9:08

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.