In vim, control-left and control-right are back and forward whitespace-separated word (i.e. synonyms for B and W), the same as web textareas and bash.

But when running screen, these keys stop working. When pressed, vim instead switches to its command line and enters 5C or 5D there.

Here is encoding I have for these keys:

$ cat >/dev/null    # pressing control-left, then control-right:

BTW: I tried remapping these keys in .screenrc, like this:

bindkey ^[[1;5D stuff B
bindkey ^[[1;5C stuff W

This works for vim, but now those keys don't work at the bash command line (instead, just outputting B or W).

I'm guessing this is some termcap mysteriousness, which I've never understood... thanks for any enlightenment!

  • $ echo $TERM changing screen's $TERM to screen-256color (if it's not already) may help. May I suggest you to get accustomed to b and w, instead? – romainl Jan 11 '13 at 8:12
  • Thanks. It wasn't, I set it with term screen-256color in .screenrc, and restarting; echo $TERM confirmed the change. But it didn't solve the problem. I actually use b/w all the time - I was very surprised to find myself using ^left/^right. I blame the web. – 13ren Jan 11 '13 at 8:29
  • Bad bad bad web! – romainl Jan 11 '13 at 8:55
  • @romainl :-) it's also the bash commandline - because I'm often switching between bash and vim, that's probably more of a factor than the web. Bad bash. – 13ren Jan 12 '13 at 4:29

There are a couple of ways to fix this. One is to note the value of $TERM before you start screen (probably xterm), then after you start vim within screen, set the 'term' option to that value. For example,

:set term=xterm

Another way is to define mappings for those character sequences:

:map <esc>[1;5D <C-Left>
:map <esc>[1;5C <C-Right>

You could execute either of those solutions in your ~/.vimrc conditionally on using screen, like this:

if &term == "screen"
    map <esc>[1;5D <C-Left>
    map <esc>[1;5C <C-Right>
  • 1
    Brilliant! Both work. I found it also works if I set it in .screenrc (term xterm), But I guess least disruptive is wisest, so I've used your conditional map answer. BTW the binding is swapped, with 5D for left, 5C for right (the opposite one would expect). So I used: map <esc>[1;5D <C-Left> and map <esc>[1;5C <C-Right> – 13ren Jan 11 '13 at 9:20

My .vimrc in Ubuntu to handle ctrl arrow keys

map  <Esc>[1;5A <C-Up>
map  <Esc>[1;5B <C-Down>
map  <Esc>[1;5D <C-Left>
map  <Esc>[1;5C <C-Right>
cmap <Esc>[1;5A <C-Up>
cmap <Esc>[1;5B <C-Down>
cmap <Esc>[1;5D <C-Left>
cmap <Esc>[1;5C <C-Right>

map  <Esc>[1;2D <S-Left>
map  <Esc>[1;2C <S-Right>
cmap <Esc>[1;2D <S-Left>
cmap <Esc>[1;2C <S-Right>

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.