Italic fonts in iTerm2, tmux, and vim
Published onUsing tmux version 2.1 or above? Check out the update below.
Recently, I’ve been trying to restrict my coding workflow to the terminal. I’ve always used iTerm2 as my terminal, and, since switching to vim about 18 months, have used MacVim as my editor. I felt I was losing efficiency switching between windows though, and losing power by not exploiting things like splits.
Currently, my setup is using iTerm2 with tmux to manage terminal splits and ‘windows’, and then regular old vim to edit. I like it, but the setup’s for another post.
This one is about enabling italics in these three tools. MacVim has native support for italics, and although iTerm2 has supported italics for sometime, getting my italics back proved to be somewhat cumbersome. I’ll explain what I did to get italics in iTerm2, tmux, and vim, as shown below, in this post.
The font
In order to see italicised text at all, we need a typeface with a italic variant. I like Consolas, which is included in all recent versions of Windows, and will be present on an OS X system if Microsoft Office has been installed. If you don’t have it, you can probably find it online, or use a free typeface with an italic variant like Ubuntu or Anonymous Pro. (There’s also Menlo, which is included with OS X.)
iTerm2
iTerm2 has support for italics built in. First, make sure it’s enabled in your terminal profile.
Next, we need to tell the terminal what italic actually means, which is done by using a special TERM
entry.
(This is not a subject I’m familiar with, but worrying about the technicalities here doesn’t seem to be necessary.)
Luckily, one has already been made for us, courtesy of Stefan Schüssler.
# A xterm-256color based TERMINFO that adds the escape sequences for italic.
xterm-256color-italic|xterm with 256 colors and italic,
sitm=\E[3m, ritm=\E[23m,
use=xterm-256color,
Create a file called xterm-256color-italic.terminfo
, where ever you like, with the above contents.
This file needs to be processed and added to the TERM
database.
$ tic xterm-256color-italic.terminfo
Finally, we need to tell iTerm2 to use this new TERM
, xterm-256color-italic
, by default.
This is done in the terminal pane of whatever profile you’re using.
The new entry probably won’t be in the list, but we can just type it in.
If you close and reopen iTerm2, executing the following should show italicised text.
$ echo `tput sitm`italics`tput ritm`
If you don’t see italicised text, something else might be overriding the TERM
environment variable.
Check its value is xterm-256color-italic
.
$ echo $TERM
xterm-256color-italic
If it’s different, check you’re dotfiles (like .bashrc
).
vim
I really like having comments in italic. It differentiates them one step more from the surrounding code, and it makes sense semantically too, as a comment is treated very differently from anything else in the source.
To enable italicised comments in vim, add this line to your .vimrc
file after anything else theme related (like colorscheme
).
highlight Comment cterm=italic
Then open up a new vim session and type a comment. Glorious.
You could also edit your colorscheme directly by changing the comment highlighting declaration.
tmux
If you open up a new tmux session, and then open vim, you’ll notice the italics aren’t working anymore.
This is because the TERM
for a tmux session is, by default, different to that of a non-tmux session.
# Inside a tmux session
$ echo $TERM
screen-256color
To fix this, we need to install a new TERM
entry, like we did before, and then tell tmux to use it.
So, as before, copy the contents below in to a file, this time called screen-256color-italic.terminfo
.
# A screen-256color based TERMINFO that adds the escape sequences for italic.
screen-256color-italic|screen with 256 colors and italic,
sitm=\E[3m, ritm=\E[23m,
use=screen-256color,
If you squint, you’ll notice the only difference to the previous .terminfo
is replacing all instances of xterm
with screen
.
Then, load this in to the TERM
database.
$ tic screen-256color-italic.terminfo
Add a line to your tmux.conf
to tell tmux to use the new TERM
.
set -g default-terminal "screen-256color-italic"
I suspect there’s a cleverer way, but I had to kill my tmux process to bring the changes into effect.
$ killall tmux
Starting a new tmux session and opening vim, you should see what we’re after: italics!
Caveats
If you ssh
in to a remote machine from your tmux session, you might get some errors related to your new TERM
.
This is because the remote machine doesn’t have the database entry we just installed locally.
To fix this, repeat the installation of the xterm-256color-italic
and screen-256color-italic
TERM
entries.
(The screen
entry isn’t necessary if you won’t be using tmux on the remote machine.)
We can extract the terminfo from our local database, and then upload it to the remote machine.
$ infocmp xterm-256color-italic > xterm-256color-italic.terminfo
$ scp xterm-256color-italic.terminfo user@remote:
$ ssh user@remote
# On the remote machine
$ tic xterm-256-italic.terminfo
Repeat for the screen
terminfo.
You can delete the .terminfo
files, from both the remote and local machines, once you’ve loaded them with tic
.
tmux 2.1 and above
The release of tmux 2.1 saw several changes to the way tmux handles
the terminal type, amongst other things. For getting italics working, we now
need a new terminfo called tmux
.
Luckily, there’s already an FAQ on how to add the new terminfo entry with a single command.
$ 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
We then just need to tell tmux to use this new terminfo.
set -g default-terminal "tmux"
You might need to restart your terminal and/or the tmux server for the changes to take effect.
Thanks to Landon Schropp for pointing this out in the comments.