jasonwryan.com

Miscellaneous ephemera…

Vim Colours in the Console

image

I have spent the last couple of days trying to understand why I couldn’t get Vim to display the correct 16 colours in the console. Not in a terminal emulator in X, like xterm or rxvt-unicode, but in the consoles, TTY1-6.

After a lot of searching, more reading into the arcane documentation of Linux terminals than I was either prepared for or keen on and some experimentation, I have it working. With one caveat; I wasn’t prepared to set the TERM environment in my .vimrc. Apart from being cautioned against, as I do set it for tmux, I was anxious to avoid any clashes when running Vim in tmux (which I do most of the time).

From the top, then. Logging into a console and checking the $TERM and colours, gave me the default:

1
2
3
$ echo $TERM && tput colors
linux
8

Which, under my current configuration of Vim, rendered colours like so:

image

Hardly satisfactory.

The first step was to remove the line in my .vimrc that was forcing 256 colours:

1
set t_Co=256

Then I had to rewrite my colourscheme to change all of the references to the terminal colour numbers to colour names, as apparently that is what 8-colour terms accept. So, my miromiro.vim, went from looking like:

1
2
3
4
hi Normal          ctermfg=15
hi Ignore          ctermfg=8
hi Comment         ctermfg=7
hi LineNr          ctermfg=8

…to the more literal:

1
2
3
4
hi Normal         ctermfg=white    cterm=bold
hi Ignore         ctermfg=black    cterm=bold
hi Comment        ctermfg=grey
hi LineNr         ctermfg=black    cterm=bold

Once I had made the necessary changes, I saved this new colourscheme as miro8.vim1. And, yes, I know I could have included it in an if condition in my original colourscheme2, but I was intent on keeping things compartmentalized until I had it working…

Running Vim in the console was now starting to look promising:

image

The final piece of the puzzle was to pass my X colours to the console. Fortunately, Aaron Griffin’s 2006 post made this a trivial exercise. With the following lines in my .bashrc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Linux console colors (jwr dark) 
if [ "$TERM" = "linux" ]; then
   echo -en "\e]P0000000" #black
   echo -en "\e]P83d3d3d" #darkgrey
   echo -en "\e]P18c4665" #darkred
   echo -en "\e]P9bf4d80" #red
   echo -en "\e]P2287373" #darkgreen
   echo -en "\e]PA53a6a6" #green
   echo -en "\e]P37c7c99" #brown
   echo -en "\e]PB9e9ecb" #yellow
   echo -en "\e]P4395573" #darkblue
   echo -en "\e]PC477ab3" #blue
   echo -en "\e]P55e468c" #darkmagenta
   echo -en "\e]PD7e62b3" #magenta
   echo -en "\e]P631658c" #darkcyan
   echo -en "\e]PE6096bf" #cyan
   echo -en "\e]P7899ca1" #lightgrey
   echo -en "\e]PFc0c0c0" #white
   clear # bring us back to default input colours
fi

Then it was just a case of setting the relevant colourscheme in my .vimrc:

1
2
3
4
5
if &t_Co < 256
    colorscheme miro8   " colourscheme for the 8 colour linux term
else
    colorscheme miromiro
endif

And you can see a screenshot of the finished product on Flickr.

Notes
  1. miro8.vim

  2. miromiro.vim

Comments