Getting 256 colors to work in tmux

I have 256 colors working just fine in konsole,. I thought I’d give tmux a try because, unlike screen, it seems to support vi mode. However I find that the colors of my prompt show up and this is most likely because I have a 256 color mode prompt. What do I need to do to get tmux to recognize all 256 colors?

Asked By: xenoterracide

||

Try setting 256 colors explicitly in your bashrc or zshrc:

export TERM=xterm-256color

or

export TERM=screen-256color

If you have problems with tmux not clearing the background colors correctly when using the screen term setting, you can try:

export TERM=screen-256color-bce
Answered By: redacted

The Tmux FAQ explicitly advises against setting TERM to anything other than screen or screen-256color or tmux or tmux-256color in your shell init file, so don’t do it!

Here’s what I use:

~$ which tmux
tmux: aliased to TERM=xterm-256color tmux

and in in my .tmux.conf:

set -g default-terminal "screen-256color"

Aliasing tmux to "tmux -2" should also do the trick.

And don’t forget to restart your tmux server: (see @mast3r answer)

tmux kill-server && tmux
Answered By: 0xYUANTI

As per the tmux FAQ, add this to your ~/.tmux.conf:

set -g default-terminal "screen-256color"

Then add this alias for tmux:

alias tmux='tmux -2'

No need to override the TERM variable in your profile or when starting tmux.

More information: http://vim.wikia.com/wiki/256_colors_setup_for_console_Vim

Answered By: user1338062

This is my solution….
I edit my .bashrc file and adding this

if [ "$TERM" != "xterm-256color" ]; then
      export TERM=xterm-256color
    fi

Hopefully it works in yours

Answered By: Wira Bhakti

The newest version of ncurses ships with a tmux-256color terminfo entry (the FAQ does mention this). As an example, a benefit of using tmux-256color over screen-256color is that italics is properly rendered (screen doesn’t support italics).

So if you are using tmux 2.6 or above, and have the latest ncurses package, the following will work as well:

set -g default-terminal "tmux-256color"
Answered By: user252672

Really important note here if you’re running an Ubuntu older than Bionic (18.04). If you’re running tmux 2.1 or older, and you probably are, basically no advice you read online about tmux will work..for anything.

tmux -V is an easy test for this. If you see 2.1 or older, you can build tmux from source, or you can add an unofficial repository. I updated my tmux with:

sudo add-apt-repository ppa:/hnakamur/tmux
sudo apt update
sudo apt upgrade

Personally, after upgrading to 2.6, 1) my keys are working better, 2) online advice actually works, 3) my colors are not insane. Big improvement.

(Here’s a link to the repository page, btw):
https://launchpad.net/~hnakamur

Answered By: zzxyz

I could not explain why this works but it solved the problem for me.

~/.tmux.conf

set-option -g default-command bash
Answered By: John Snow

For people who have latest tmux, the .tmux.conf option from the accepted answer should work.

I only want to add that you will probably need to restart tmux for the new configuration to take effect:

tmux kill-server && tmux

Interestingly, this isn’t mentioned anywhere in the answers and took me a while to figure out.

Answered By: mast3r

Following lines in ~/.tmux.conf worked for me —

set -g default-terminal "xterm-256color"
set-option -ga terminal-overrides ",xterm-256color:Tc"

Tested on

  • Tmux 2.7, Ubuntu 16.04,18.04
  • Tmux 2.9, CentOS
  • Tmux 3.0a, CentOS7
Answered By: user3496912

Feeling grateful for all other answers here giving me hints, but then it seems unnecessary to modify global TERM setting AND ~/.tmux.conf.

I ended up using an alias to set the TERM only when I’m about to use tmux. Just add this one-liner at the end of your ~/.bashrc (or whatever shell configuration file you use).

alias tmux='TERM=xterm-256color tmux'

That’s it. Interestingly, after tmux launchs, the TERM would automatically be reset to “screen”, but the colors work anyway. So I guess tmux would also be happy (for not messing up with its TERM environment?).

Answered By: RayLuo
Categories: Answers Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.