← Back to Upcase

Tmux Part 4: Vim Integration

(Anthony) #20

I installed Vundle and then added

Bundle 'christoomey/vim-tmux-navigator'

to my ./vimrc file

I added the new bindings to my .tmux.conf file:

# Smart pane switching with awareness of vim splits
is_vim='echo "#{pane_current_command}" | grep -iqE "(^|\/)g?(view|n?vim?)(diff)?$"'
bind -n C-h if-shell "$is_vim" "send-keys C-h" "select-pane -L"
bind -n C-j if-shell "$is_vim" "send-keys C-j" "select-pane -D"
bind -n C-k if-shell "$is_vim" "send-keys C-k" "select-pane -U"
bind -n C-l if-shell "$is_vim" "send-keys C-l" "select-pane -R"
bind -n C-\ if-shell "$is_vim" "send-keys C-\\" "select-pane -l"

but when I source the .tmux.conf file, I get this error:

/Users/acandael/.tmux.conf:8: invalid or unknown command: is_vim='echo "#{pane[0/0]│   1 set nocompatible              " be iMproved, required

/Users/acandael/.tmux.conf:9: invalid or unknown command: "(^|/)g?(view|n?vim?)(d’

how can I fix this?



(Ben Orenstein) #21

@acandael Can you please open an issue on the vim-tmux-navigator repo rather than posting in this thread?

(Anthony) #22


when I run run Rspec from within Vim, my spec file is replaced with a new window that runs the spec file. When I hit return I return to the spec file. However I would prefer that the spec is run in a new pane, like in the tutorial video.

I installed the .dotfiles with the Thoughtbot Laptop script.

In my .vimrc file the following lines specify the vim-rspec configuration:

" vim-rspec mappings                                                                                  
nnoremap <Leader>t :call RunCurrentSpecFile()<CR>                                                     
nnoremap <Leader>s :call RunNearestSpec()<CR>                                                         
nnoremap <Leader>l :call RunLastSpec()<CR>  

am I missing something?

Thanks for your help,


(Ben Orenstein) #23

Likely. dispatch.vim.

(Anthony) #24

Thanks Ben. Yesterday I found this blog post on dispatch.vim:



(Captain Zoom) #25

When I use Vim from within tmux, the delete key (or Ctrl-H) sends a ^? character instead of a backspace, i.e.:

YOU WILL BE DELETED^?^?^?^?^?^?^?^? 

This doesn’t happen elsewhere in tmux, not does it happen in Vim outside tmux. This is surely related to my other problem with Ctrl-H failing to switch panes. As was the case there, toggling the Delete Sends Ctrl-H setting has no effect here. Any idea what the ^? is or what’s wrong with my Ctrl-H?

(Chris Toomey) #26

@captain_zoom not sure what the specific issue is, but if you are only seeing this behavior in Vim, you can check what the mapping in Vim is by running: nmap <C-h>.

Other things to look at would be comparing the behavior in and out of tmux, and looking into your tmux mappings if you’re using Vim-Tmux-Navigator.

(Captain Zoom) #27

Thanks @christoomey – I just removed all the <c-h> mappings in my .vimrc, and the problem persists. I now have:

:imap <c-h>
no mapping found
:nmap <c-h>
no mapping found
:map <c-h>
s  <C-H>       * <C-G>c

I can remove that last mapping by typing unmap <c-h>, but that doesn’t change anything. (Interestingly, unmap <c-h> doesn’t work when it’s the last line in my .vimrc. Somehow that mapping is getting set after .vimrc loads?)

Regardless, I don’t think a mapping is the problem, because the problem is unique to Vim inside tmux. I have no issues (with or without my normal C-h mappings) in gui MacVim, terminal MacVim, or the default Vim, as long as they are run outside of tmux. Not using Vim-Tmux-Navigator at the moment.

(geraldspreer) #28

I have applied some the tmux and vim configuration in this course. But now when I use vim inside iTerm but outside of tmux, every time I leave insert mode the string tmux; is inserted in the current buffer.
This only happens inside the terminal NOT in Macvim.

Any clues?

(Chris Toomey) #29

Hello @geraldspreer, Not sure what the cause of that would be, although you can likely rule out the tmux.conf file as you said this is happening outside of tmux. I would try disabling parts of the new configuration to try to lock down what line is causing it.

(geraldspreer) #30

Found the solution:
Updated my .vimrc to:

    if exists('$TMUX')
      let &t_SI = "\<Esc>[3 q"
      let &t_EI = "\<Esc>[0 q"
      " these two lines were outside the if statement 
      " putting them here solves this issue although the cursor shape stays 
      " the same in any mode outside tmux - but that's OK. 
      let &t_SI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=1\x7\<Esc>\\"
      let &t_EI = "\<Esc>Ptmux;\<Esc>\<Esc>]50;CursorShape=0\x7\<Esc>\\"

Thank you for pointing me towards this.

(Geoff Harcourt) #31

Hi @geraldspreer, are you using Neovim? Neovim had an issue until very recent builds, but even if you aren’t using Neovim there’s some interesting info about terminal types and emulation. Here’s the issue report:

I had this issue only in tmux for a while. I mapped <C-h> to be the backspace key in normal mode to work around it until the recent fix.

(geraldspreer) #32

Thanks for the hint.

(Matthew Davis) #34

When I try to run the specs in the current file, vim-rspec seems to be getting the path wrong. It assumes the path to be


and not


This is even if I cd to the spec file’s subdirectory and launch vim with the spec as a command line argument. At first I thought it was because I was finding the file with Ctrl-P, but that’s not it.

EDIT: Actually, I’m wrong. If I launch vim in the subdirectory containing the spec file, when Vim-Rspec passes off that filename, RSpec has no problem loading it. BUT in this case, Rspec can’t load ‘rails_helper’ which is in the require line of that spec file. So if I’m in project root, vim-rspec isn’t telling Rspec where the spec file is located – it’s just letting RSpec assume that it’s the same directory from which vim was launched. And if launched from the subdirectory, Rspec doesn’t know where to look for the helper files. Either way, the lack of awareness of where the file is located is fatal.

Note that running “All Specs” works just fine. It’s just the commands that require a specific filename that are getting broken.

(Chris Toomey) #35

@MatthewMDavis, have you tried running from the root?

For reference, rspec.vim uses @% to determine the file path. You can confirm the output by running :echo @% in Vim. This should match the path you would need to run in vim.

One other setting to look at is autochdir. You can determine the value by running :verbose set autochdir?. You’ll either see noautochdir, or the last file that set it. If set, that could be interfering.

(Matthew Davis) #36

I had just found out that a directory switch within vim was causing my problem right before I saw your message, and now I know what .vimrc setting was doing it to me: yes, vim was autochdir’ing to the spec file’s directory, and vim in turn was spawning a tmux runner pane, in a subdirectory where rspec didn’t know how to locate rails_helper.

Having turned off autochdir, things are working as expected (as long as I’m working in the Rails project root).

That was a bear of a morning. Thanks for your help. Nice course, too. I don’t know if I’ll spend 100% of my time in Tmux, but the number is definitely going way up.

(Chris Toomey) #37

Happy to help! If you’re anything like me then tmux’s addictive efficiency will soon take you over. :slight_smile:

(Christophe Robillard) #39

Great job @christoomey ! Really !
But i’ve got a problem with vim-tmux-runner.
When I try to launch RunNearestSpec, I’ve got an error in the runner pane :

^Lrspec spec/models/

There is a ^L before the command !
How can I fix it ?
(FYI, when i’m using :VtrSendLinesToRunner, there is no problem at all, it works)

Thanks for your answer

(Chris Toomey) #40

Hello @krichtof, thanks for the kind words!

The ^L is purposeful, it is meant to clear the terminal before running the command but it seems like your terminal is not interpreting the ^L as a clear.

Not sure why this is, but you can change the keys used to clear by changing the VtrClearSequence or disable this entirely with the VtrClearBeforeSend setting.

(Christophe Robillard) #41

Thanks for your answer. When I disable the VtrClear, it works :slight_smile: