Skip to main content

Enabling italics in Vim and tmux

Mar 17, 2020 · Last updated: Apr 7, 2020 ·
Posted in weblog#tech
Categories: unix, vim, tmux

If you're like me, you fiddle around with your development setup and dotfiles frequently tweaking and breaking things and learning along the way.

I only recently found out that you could have comments on Vim appear in italics. All you need to do is add highlight Comment cterm=italic to your .vimrc. Except, it's usually not that straightforward! I spent 3.5 hours researching how to properly enable italics in Vim and then, in Vim inside tmux and found out some genuine and a few stupid reasons things might not work as expected on your system.

Here, I will provide you official instructions to solving this problem and things you should care about if they don't work.

Italics in Vim

Before jumping right in, I think it's a good idea to learn some basics of what is actually needed to render italics in your terminal.

  • Environment variables

    An environment variable is a named object that contains data used by one or more applications. In simple terms, it is a variable with a name and a value. The value of an environmental variable can for example be the location of all executable files in the file system, the default editor that should be used, or the system locale settings. ArchWiki

  • TERM

    TERM is a common environment variable used by a Linux system that contains the type of the running terminal. It is used by programs running in the terminal that wish to use terminal-specific capabilities. To see the value of TERM environment variable in your system, run echo $TERM. In most cases, it will be something like xterm-256color if you're using a 256 color terminal.

  • Terminfo

    Terminfo is a data base describing terminals, used by screen-oriented programs such as vi, and libraries such as curses. Terminfo describes terminals by giving a set of capabilities which they have, by specifying how to perform screen operations, and by specifying padding requirements and initialization sequences.

  • infocmp

    infocmp can be used to compare or print terminfo descriptions.

  • tic/toe

    tic is the terminfo entry-description compiler. This command translates the terminfo files from source format into compiled format. To view all terminfo entries on your system, type toe.

And now we can begin. To have italics enabled in terminal Vim/Neovim, you need to have a terminal emulator that supports italics. To check this for your terminal, type the following:

$ echo -e "\e[3m foo \e[23m"

If you see the output foo (in italics), then congratulations! Your terminal can support italics. If you don't, I recommend you switch to a more modern terminal emulator.

If you don't see italicised foo above, then you need to add a custom terminfo. Fortunately, there is one that we can use. Create a file named xterm-256color-italic.terminfo and put these lines in it:

xterm-256color-italic|xterm with 256 colors and italic,
  sitm=\E[3m, ritm=\E[23m,

Now, compile the xterm-256color-italic.terminfo file with tic:

$ tic xterm-256color-italic.terminfo

Lastly, load it in your ~/.bashrc or ~/.zshrc:

$ echo "export TERM=xterm-256color-italic" >> ~/.bashrc
# or
$ echo "export TERM=xterm-256color-italic" >> ~/.zshrc

Source your .bashrc (source ~/.bashrc), restart the terminal and try the test above to make sure your terminal now outputs the italicised foo.

Now, to have italicized comments in Vim, add this line to your .vimrc / init.vim after you declare your colorscheme:

highlight Comment cterm=italic

Save and open the file again. Did it work? Yes? Great. But if it didn't, restart your terminal and open the file again. This should work now.

If you still don't see italics, add these lines to your .vimrc:

set t_ZH=^[[3m
set t_ZR=^[[23m

These are special terminal options that enable italics mode and italics end respectively using proper escape sequences used by Vim to get information about the running terminal.

Note: The characters ^[ must be entered with <Ctrl-v><Esc>.

Sidenote: If you're using jellybeans.vim colorscheme for Vim, put this line after colorscheme jellybeans:

let g:jellybeans_use_term_italics = 1

" For onedark.vim -
let g:onedark_terminal_italics = 1

Italics in Vim inside tmux

If you made it this far, chances are you are using a terminal multiplexer like tmux for managing multiple sessions in the terminal. But if you try the test above inside tmux, you'll notice italics don't work anymore. This is because tmux sets the environment variable TERM to screen-256color instead of xterm-256color and hence uses the italics escape sequence incorrectly. For tmux 2.1 and above, you'd have had to create a new terminfo entry:

$ cat <<EOF|tic -x -
tmux|tmux terminal multiplexer,
    ritm=\E[23m, rmso=\E[27m, sitm=\E[3m, smso=\E[7m, Ms@,
    use=xterm+tmux, use=screen,

tmux-256color|tmux with 256 colors,
    use=xterm+256setaf, use=tmux,

And then tell tmux to use it in ~/.tmux.conf:

set -g default-terminal "tmux"
# or
set -g default-terminal "tmux-256color"

For more recent tmux versions, just adding the above line to ~/.tmux.conf suffices. A new terminfo entry may not be required.

See tmux FAQs and tmux 2.1 FAQs.

If you still don't see italics in tmux, make sure that you don't have any tmux sessions running on your system (tmux kill-server), then try again.

Also, make sure you have an italics capable font enabled. I have tested this on rxvt-unicode with Source Code Pro and on Alacritty with Mononoki Nerd Font.

Italics in Vim + tmux + Alacritty + Mononoki Nerd Font : Italics in Vim+tmux+urxvt Italics in Vim + tmux + urxvt + Source Code Pro Font : Italics in Vim+tmux+Alacritty

Further reading

Read comments on: Hacker News · Lobsters
Submit feedback
Share on: Hacker News · Lobsters · Reddit · Twitter