How to fix Ctrl + arrows in Vim?

23,409

Solution 1

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.

Solution 2

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 +

Solution 3

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
Share:
23,409
Alex B
Author by

Alex B

Updated on September 17, 2022

Comments

  • Alex B
    Alex B over 1 year

    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?

    • Marius
      Marius over 6 years
      PuTTY doesn't send usefully-distinct sequences for the control-arrows. None of the suggested answers are correct (or useful).
  • Alex B
    Alex B over 13 years
    Turning off Application Cursor Keys mode doesn't seem to help.
  • Alex B
    Alex B over 13 years
    How do I tell what escape sequence is sent?
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 13 years
    @Alex: I've tried to clarify my explanation, complain if you still don't understand my first sentence.
  • msw
    msw over 13 years
    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.
  • Alex B
    Alex B over 13 years
    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.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 13 years
    @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).
  • Alex B
    Alex B over 13 years
    @Gilles Finally fixed it. Just one typo in map commands: should be map <ESC>5D, rather than <ESC>[5D, since ESC is already ^[.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 13 years
    @Alex: On the terminal I chose as an example, C-Left sends ESC [ 5 D (4 characters). Another common sequence if ESC O 5 D. I don't think any terminal sends ESC 5 D for any key, these sequences usually start with ESC [ or ESC O.
  • Alex B
    Alex B over 13 years
    @Gilles ah OK, got it.
  • rev
    rev about 12 years
    @Gilles thanks for posting this answer. on debian, i get ESC[1;5A whereas on mac i get ESC[5A. your answer helped me solve my vim mapping problem.
  • Yashar
    Yashar almost 7 years
    This creates some weird char behaviour.