Economy of Effort

Twitter LinkedIn GitHub Mail RSS

Zsh, Tmux, Vim, And 256 Color Madness

tl;dr: You want $TERM to be screen-256color when tmux is running, and you want it to be xterm-256color when tmux is not running. Also, launch tmux with -2 argument.

I love tmux. It is the primary reason why I switched from using gVim to console vim. I love having a fully terminal-based workflow. It beats switching between a GUI editor app and terminal window any day.

This switch, however, was not without some issues. Here are the solutions to two that I encountered.

Weirdness with zsh, tmux, and vim

Problems:

  • when $TERM is screen-256color but tmux is not running, zsh will echo your command into the output when you hit Enter:

Notice how the output of the “ls” and “echo” commands repeat themselves in the output stream as soon as I switched to screen-256color.

  • when $TERM is xterm-256color while tmux is running, colors will not display properly in Vim:

vim /etc/default/grub while TERM=screen-256color:

vim /etc/default/grub while TERM=xterm-256color:

Solution:

In my zsh config (~/.zshrc), I set xterm-256color to be the default TERM, but right after that, added a command that would re-export TERM as screen-256color if tmux is running:

export TERM=xterm-256color
[ -n "$TMUX" ] && export TERM=screen-256color

No Vim colorschemes when tmux is launched by terminal app in place of shell

Problem:

I ran into a specific set of circumstances where my Vim colorscheme would not display.

Terminal applications usually launch a shell by default, but some (like gnome-terminal) have the option of defining a command to be run rather instead of the shell.

If I set this command to tmux, tmux would indeed launch. However, if I then ran Vim, the colorscheme would not display correctly.

However, if I allowed gnome-terminal to launch a shell, and then ran tmux myself from that shell, Vim would display properly within that tmux session.

Solution:

I got my clue from this StackOverflow post. Basically, what is happening is that, when running tmux from within my shell, which is configured for 256 colors, tmux would launch in 256 color mode. But when I had gnome-terminal launch tmux directly, it would not.

The easy way around this was to use the “-2” argument for tmux, making the command tmux -2.

With that command in place, tmux launches whenever firing up gnome-terminal, and does so in 256 color mode.

Comments