33

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?

Steven D
  • 45,310
  • 13
  • 119
  • 114
Alex B
  • 4,458
  • 5
  • 38
  • 41
  • 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

5 Answers5

34

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.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • How do I tell what escape sequence is sent? – Alex B Sep 07 '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 07 '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 08 '10 at 00: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
  • @Gilles Finally fixed it. Just one typo in `map` commands: should be `map 5D`, rather than `[5D`, since `ESC` is already `^[`. – Alex B Jan 18 '11 at 23:41
  • @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`. – Gilles 'SO- stop being evil' Jan 18 '11 at 23:47
  • @Gilles ah OK, got it. – Alex B Jan 19 '11 at 00:15
  • @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. – rev Apr 01 '12 at 19:23
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 +

msw
  • 10,503
  • 1
  • 31
  • 43
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
jasonwryan
  • 71,734
  • 34
  • 193
  • 226
trigal
  • 21
  • 1
0

for my this and other vim keyboard problems I simply do

vim ~/.vimrc
set nocompatible
zainengineer
  • 289
  • 2
  • 3
0

I've implemented this in my ~/.vimrc to fix the issues with the escape sequences generated by my terminal emulator for the Ctrl+Arrow, Shift+Arrow, and Ctrl+Shift+Arrow keypresses; all these keypresses create issues on their own, so it's usually best to fix them in one fell swoop:

" Just a temporary shorthand
function! NoRemapNXIC(lhs, rhs_nxi, rhs_c = v:null)
  execute "nnoremap " .. a:lhs .. " " .. a:rhs_nxi
  execute "xnoremap " .. a:lhs .. " " .. a:rhs_nxi
  execute "inoremap " .. a:lhs .. " <C-O>" .. a:rhs_nxi
  if a:rhs_c isnot v:null
    execute "cnoremap " .. a:lhs .. " " .. a:rhs_c
  endif
endfunction

" Make navigating through long lines easy
call NoRemapNXIC("<Up>",   "gk")
call NoRemapNXIC("<Down>", "gj")

call NoRemapNXIC("<C-Left>",  "b")
call NoRemapNXIC("<C-Right>", "e")
call NoRemapNXIC("<S-Left>",  "B")
call NoRemapNXIC("<S-Right>", "E")

" Handle <Ctrl> + <Arrow> escape sequences
call NoRemapNXIC("<ESC>[1;5A", "gk", "<Nop>")
call NoRemapNXIC("<ESC>[1;5B", "gj", "<Nop>")
call NoRemapNXIC("<ESC>[1;5D", "b",  "<C-Left>")
call NoRemapNXIC("<ESC>[1;5C", "e",  "<C-Right>")

" Handle <Shift> + <Arrow> escape sequences
call NoRemapNXIC("<ESC>[1;2A", "<PageUp>",   "<Nop>")
call NoRemapNXIC("<ESC>[1;2B", "<PageDown>", "<Nop>")
call NoRemapNXIC("<ESC>[1;2D", "B",          "<S-Left>")
call NoRemapNXIC("<ESC>[1;2C", "E",          "<S-Right>")

" Handle <Ctrl> + <Shift> + <Arrow> escape sequences
call NoRemapNXIC("<ESC>[1;6D", "ge", "<C-Left>")
call NoRemapNXIC("<ESC>[1;6C", "w",  "<C-Right>")

delfunction NoRemapNXIC

Of course, you'll need to check what escape sequences for those keypresses are actually generated by your particular terminal emulator, and adjust this code appropriately. It's already described in other answers how to check that.

As you can see, I also remapped the actual cursor movement commands a bit, to use b, e, B, E, ge and w, which makes them more suitable to my own preferences. Of course, you can tailor those to your own needs.

Please note that remapping <Up> and <Down> to gk and gj, respectively, and remapping to gk and gj commands in other places isn't directly related to the OP's question, but I included those here as well for the sake of completeness, and simply because my ~/.vimrc does it that way. Having those remaps as well might be a good idea, see this question for more details.

See also :h i_CTRL-O.

dsimic
  • 1
  • 1