Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to figure out how to get vim and tmux play together.

My problem is: When I use xterm-256color I get vim to work nicely, but the background doesn't render properly.

I fixed this by using screen-256color. This looks nice but breaks the Ctrl + arrow keys (they go in insert mode and delete 4 lines). I want to use these mappings instead to switch tabs in vim.

How do I get this to work?

share|improve this question

4 Answers 4

up vote 8 down vote accepted

Run cat within tmux and press the keys in question to find out the escape codes they generate. For me this gives ^[[D and ^[[C. Then just map those escape codes directly.

nnoremap ^[[D gT
nnoremap ^[[C gt

where the ^[ is an escape character, that you can insert by pressing <CTRL-v><ESC> in vim.

share|improve this answer
    
That didn't change the behaviour of my vim. –  outsmartin Jan 26 '12 at 21:02
    
What's printed when you start cat within tmux and press the keys? –  Mika Fischer Jan 26 '12 at 22:38
    
^[[1;5C and ^[[1;5D. Good idea, I didn't know how to get those. Just tried it. It works! Thank you. –  outsmartin Jan 27 '12 at 9:14
    
Great. I edited my ansewer to incorporate the method to get the right escape codes. –  Mika Fischer Jan 27 '12 at 12:19

There are several levels of configuration that need to be set up correctly for the best functionality.

  1. Configure tmux to recognize the sequences. Before launching tmux, set a TERM value that is appropriate for your terminal emulator (e.g. xterm-256color). The terminfo database entry identified by the TERM environment variable tells tmux how to recognize the sequences for the modified arrow keys (the user-defined capabilities {kUP,kDN,kRIT,kLFT}{,3..7}).
  2. Configure tmux to generate the sequences. Set the xterm-keys session option to on so that tmux will generate its own xterm-style sequence when it receives a sequence that its TERM identifies. In your ~/.tmux.conf:

    set-option -g xterm-keys on
    
  3. Configure the programs running inside tmux to use/expect the majority of its sequences. Make sure you are using a screen-based TERM inside tmux (e.g. screen-256color). This tells programs running inside tmux how to control tmux’s terminal (re)emulation and what responses they should expect from tmux.
  4. Unfortunately, screen-based terminfo database entries often do not define any sequences for the modified arrow keys (i.e. they may not have kUP, et cetera), so the programs running inside tmux may not know to expect such sequences. You will need to manually configure any programs that you want to recognize these keys. In Vim, you can put this in your ~/.vimrc:

    if &term =~ '^screen'
        " tmux will send xterm-style keys when xterm-keys is on
        execute "set <xUp>=\e[1;*A"
        execute "set <xDown>=\e[1;*B"
        execute "set <xRight>=\e[1;*C"
        execute "set <xLeft>=\e[1;*D"
    endif
    

    You may want to use the variants without x (i.e. <Up> instead of <xUp>, et cetera) if you find that using the x-variant causes your normal arrow keys to stop working.

    The advantage of defining the keys like this is that you do not have to map each of the modifier combinations separately (Shift, Control, Contol+Shift, Meta, Meta+Shift, Meta+Control, Meta+Control+Shift); the * gets special interpretation to handle all the related sequences (see :help xterm-modifier-keys).

  5. Then you can map the keys instead of their hard-coded sequences:

    nnoremap <C-Right> gt
    nnoremap <C-Left> gT
    
share|improve this answer
    
Hey, this worked perfectly! –  pablox Nov 1 '13 at 4:59
    
perfect. thank you sir –  keyvan Mar 30 at 9:50

Does setting set t_Co=256 in your ~/.vimrc help at all? This is just a colour setting but may help clear some problems.

You could re-map the ctrl+Arrow keys in your ~/.vimrc as well...

Edit: did you try ctrl+Arrow in a GNU screen session? I think this maybe your problem.

share|improve this answer
    
it looks and works fine in screen with xterm-256color –  outsmartin Jan 24 '12 at 16:41
    
set t_Co=256 is done aswell –  outsmartin Jan 24 '12 at 16:47
    
Did any of it help? –  Sardathrion Jan 24 '12 at 16:50
    
unfortunately no. What do you exactly mean with remapign ctrl+arrow? I use them already remapped to switch tabs in vim. –  outsmartin Jan 24 '12 at 16:53
    
its mapped like this: map <C-Right> gt –  outsmartin Jan 24 '12 at 16:55

As explained here, disable Background Color Erase (BCE) by clearing the t_ut terminal option (run :set t_ut= in Vim and then press Control+L to refresh the terminal's display) so that color schemes work properly when Vim is used inside tmux and GNU screen.

This way, you can keep your TERM value as xterm-256color for proper key detection while also getting proper Vim color scheme rendering too! :-)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.