26

I am using Putty -> Suse box -> vim 7.2 combo for editing and want to remap Ctrl + arrows combo to a particular task. But for some reason, Vim ignores the shortcut and goes into insert mode and inserts character "D" (for left) of "C" (for right).

Which part of my keyboard/terminal configuration is to blame and how to fix it?

  • PuTTY doesn't send usefully-distinct sequences for the control-arrows. None of the suggested answers are correct (or useful). – Thomas Dickey Jan 13 '18 at 12:38
27

Figure out exactly what escape sequence your terminal sends for Ctrl+arrow by typing Ctrl+V, Ctrl+arrow in insert mode: this will insert the leading ESC character (shown as ^[ in vim) literally, followed by the rest of the escape sequence. Then tell vim about these escape sequences with something like

map <ESC>[5D <C-Left>
map <ESC>[5C <C-Right>
map! <ESC>[5D <C-Left>
map! <ESC>[5C <C-Right>

I seem to recall that Putty has a default setting for Application Cursor Keys mode that's inconvenient (I forget why), you might want to toggle this setting first.

Note that although escape sequences vary between terminals, conflicts (i.e. an escape sequence that corresponds to different keys in different terminals) are rare, so there's no particular need to try to apply the mappings only on a particular terminal type.

  • How do I tell what escape sequence is sent? – Alex B Sep 7 '10 at 22:37
  • @Alex: I've tried to clarify my explanation, complain if you still don't understand my first sentence. – Gilles 'SO- stop being evil' Sep 7 '10 at 22:43
  • You can also run od -a or od -c if you dig octal and then type the keys in question. See "added" in my answer for an example. – msw Sep 8 '10 at 0:14
  • Sorry for getting back to this question so late, but I've figured out that PuTTY still sends application cursor keys to the terminal, even after I turn it off completely. I am at a loss what else should I tweak to make it go away. – Alex B Jan 18 '11 at 23:25
  • @Alex: You don't need to make it go away, you can tell your applications about them (which I've found to be the path of least resistance). Or you can replace PuTTY by one of the alternatives such as mintty plus Cygwin ssh (but that's getting off-topic for this site). – Gilles 'SO- stop being evil' Jan 18 '11 at 23:30
7

Your best bet is probably to look at PuTTY's Application Cursor Keys mode configuration.

The default sequences send ESC as a prefix and [ followed by Append or Change or other things throwing you into insert mode.

added, following Gilles

A slightly more explicit version of the ^V escape can be seen with od(1). Here is me typing ^Up, ^Down, ^Right, ^Left at my terminal:

$ od -a
0000000 esc   [   1   ;   5   A esc   [   1   ;   5   B esc   [   1   ;
0000020   5   C esc   [   1   ;   5   D

So my terminal sends ^[[1;5A when I press Ctrl +

  • Turning off Application Cursor Keys mode doesn't seem to help. – Alex B Sep 7 '10 at 22:37
2

I found a better solution here: http://vim.wikia.com/wiki/Fix_arrow_keys_that_display_A_B_C_D_on_remote_shell

Just put this string in your .vimrc file:

:set term=cons25

Update

Copy this file to your /home, renaming it .vimrc:

/usr/share/vim/vim_VERSION_/vimrc_example.vim
  • This creates some weird char behaviour. – Thibault Jul 12 '17 at 13:58
0

for my this and other vim keyboard problems I simply do

vim ~/.vimrc
set nocompatible

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.