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 straighforward! 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 enable 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. linux.die.net.

  • infocmp  command

    infocmp can be used to compare or print terminfo descriptions.

  • tic  command

    tic is the terminfo entry-description compiler. This command translates the terminfo files from source format into compiled format.

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 see the output foo  (in Italics), then congratulations! You terminal can support Italics. If you don’t, switch to a better terminal emulator. Here are some options:

If you don’t see italicised foo above, then you need to add a custom terminfo. Fortunately, one has been already provided for us. So you can do:

$ curl -L https://gist.githubusercontent.com/sos4nt/3187620/raw/bca247b4f86da6be4f60a69b9b380a11de804d1e/xterm-256color-italic.terminfo -o xterm-256color-italic.terminfo

and 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

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  / ~/.config/nvim/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.

A quick aside: If you’re using jellybeans.vim colorscheme for Vim, put this line after colorscheme jellybeans (official documentation):

let g:jellybeans_use_term_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. To enable italics, you need to create a new terminfo entry called ‘tmux’:

$ 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,
EOF

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

set -g default-terminal "tmux"

This method has been mentioned in the tmux 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). I wasted too much of my time because I didn’t realize I had to kill all sessions and get a fresh start to see the method in action.

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 + Source Code Pro Font

Read More