Using True Color in Vim with Tmux

Posted 03.03.2016 in Technology by Jakob Lundberg

 

Color schemes in the terminal

At Deductive Labs we love working with the terminal. But setting the color scheme for the terminal has always been kind of tricky. There has for a long time only been 2 options, either use a 16 color palette or a 256 color palette.

At first glance it would appear simple, just go with 256 colors and you will have more than enough for usage in the terminal. But not so fast. The 256 color palette is a fixed one. Thus it is not possible to adapt it to fit a certain color scheme. This means that using any color scheme with the 256 color palette will result in an approximation. And therefore often ruin the color balance and contrast.

So the other option is to use a 16 color palette. The good news here is that we now freely can choose which the 16 colors will be. This gives a true representation of the chosen color scheme in the terminal. Is then all fine and perfect? Unfortunately not. The problem now is that we only have one active palette. This can cause conflict when different applications are affected by the palette. In my case I run a terminal, Tmux and Vim. All of which have need to accept the same palette. To make this work well there has to be some coordination taking place. And this means that switching color scheme becomes something of a project.

True Color

But now there is finally a third option to solve this. We can use True Color. With this we get the best of both worlds. We can have (almost) any color we want to perfectly match the color scheme. And the color choice in one application does not make changes to another application. The trick here is of course that all of the applications need to have True Color support. But the situation is improving.

The terminal emulator

The first step is to ensure that the terminal emulator supports True Color. There are now several terminals that does this. Examples are Gnome Terminal, st, iterm2, Terminator(GTK+3 version) and my favorite, ROXTerm. You can run this scrip to test if your terminal has support. If the colors smoothly blend, then you know that you have True Color support.

https://gist.github.com/jakob-lundberg/65bed87d584df7bd5ed7

If not, you might have to consider switching to another terminal emulator or check if there is a newer version that does have support.

Tmux

When I work in the terminal I am always using Tmux to handle multiple sessions at once. Unfortunately True Color support has been lacking in Tmux. This has long been the reason that I was not able to use True Color. But now there is a patch that gives support. And this patch was recently merged into the code master branch.

At the time of writing this there has not been a release that includes this merge however. So in order to take advantage of it right away we need to compile the code directly from the master branch.

Dependencies

First we need to install some dependencies for the build process. This differs a bit depending on the system you are running. On Ubuntu we need to run the following command

sudo apt-get install libevent-dev libncurses-dev build-essential

The Tmux code

The we download the code from the Git repository

git clone https://github.com/tmux/tmux.git

Compile

To compile the code

cd tmux
sh autogen.sh
./configure && make

If this was successful we can now copy the new binary to /usr/local/bin

sudo cp tmux /usr/local/bin

Terminal Capability

We finally have to enable the Tc terminal capability for the outer terminal.
First check what $TERM value our outer terminal have.

tmux detach
echo $TERM
xterm-256color

Then we enter Tmux and use the terminal override option.

tmux attach
tmux set-option -ga terminal-overrides ",xterm-256color:Tc"

To get the option working we detach and attach back again

tmux detach
tmux attach

Tmux should now support True Color and we can run the test script from within Tmux to verify.

Vim

To get True Color support in Vim we have to use the new fork/rewrite of Vim called Neovim. This is a rewrite of Vim using modern C++.

Installation on Ubuntu is done by

sudo add-apt-repository ppa:neovim-ppa/unstable
sudo apt-get update
sudo apt-get install neovim

And True Color is configured in Neovim by adding the following line to the ~/.config/nvim/init.vim configuration file.

if has(“termguicolors”)
set termguicolors
endif

Conclusion

We can now startup Neovim inside Tmux inside our terminal emulator and enjoy True Color.

TrueColorDemo
ROXTerm with the Solarized color scheme. Running Tmux, showing the color test and Neovim with the Gruvbox scheme.

It is still a bit of a hassle to get it set up. But as the new versions of the applications become stable and included as default in distributions it will become a lot easier. But following this guide we can get some cool bleeding edge features.

Written by Jakob Lundberg

Jakob is a tech focused system designer. With a background in network management, system administration and development he has an interest in the entire stack.

Always looking for new and improved solutions. Never accepting that things have to be complicated.

Related articles

Kritisk sårbarhet i Microsoft Remote Desktop(RDP)

En kritisk sårbarhet har hittats i Microsofts Remote Desktop Services, eller RDP.  Remote Desktop Services(RDP) - tidigare känd som Terminal Services, används för fjärrstyrning av Microsoft Windows operativsystem. Sårbarheten är…

Master Asynchronous JavaScript in a few simple steps

  Introduction One of the powerful features of JavaScript is its asynchronous nature. And it is also one of the most difficult to master. But asynchronous JavaScript does not have…

I Encrypt, therefore I Am

In this age of global surveillance and spying, encrypting communications has become the standard. Companies, big and small, are finally taking the necessary steps to encrypt all their traffic traversing…