Vim with big projects — vim & tmux.

David
4 min readSep 20, 2020

If you have ever used vim or vi, You know how much quicker you can write code. Furthermore, with the additional plugins, vim has no limits. The only issue is that when you deal with big or medium projects, your original workflow gets in bit slow.

What would be the solution, you may ask? use visual code or any other IDE? No, Tmux will do the job. Although tmux will be of great help to you, this program is much more power and can be used in variety of cases ( servers with sessions, etc).

Setup vim and tmux.

I trust you will be able to install programs, if you already use vim :).
So, let’s jump right into it. Apart from any other plugin that you use in your .vimrc config file, we will install another plugin vim-tmux-navigator that will greatly improve navigation in tmux. By default, tmux’s keyboard navigation is a bit awkward. For instance, if you want to switch between to panels, you would need to type in CTRL+b and then o. This is what vim-tmux-navigator will fix.

After the installing.

By now you should probably try using tmux to get a feel of how everything works and most importantly how bad are the keyboard shortcuts.

To add a horizontal panel: CTRL + b and %.

To add a vertical panel: CTRL + b and then “.

To switch between them: CTRL + b and then o.

Let’s fix that shall we.

Setting up this plugin is straightforward. You only have to add this line in your vimrc file, if you have a plugin manager. Otherwise,checkout this tutorial.

After that, you will have to install the plugin by typing :PluginInstall in vim.

Now for the tmux side of things. By default you probably do not have a .tmux.conf, you will need to create one and paste this piece of code in it.

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
| grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"
bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'
tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'
if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
"bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\' 'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
"bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\' 'select-pane -l'"

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R
bind-key -T copy-mode-vi 'C-\' select-pane -l

Results:

If everything is setup correctly, you should be able to navigate between panels with CTRL+h, j, k, or l without the use of CTRL + b.

Troubleshooting

You may have noticed it if your vim configuration has some theming, but tmux seems to discard any colorscheme. To fix this issue, you have to add some lines in your shell config file ( .bashrc or .zshrc ) as well as your .tmux.conf file.

In your shell config file, add this line and restart your shell so that the changes are loaded.

In your tmux config file, add this line. Be sure to restart and kill any tmux session running (tmux kill-server) so that the changes take effect.

.tmux.conf file.

--

--