Using the conceal Vim feature with LaTeX

Vim 7.3 has just been released and with it comes the “conceal” feature (you can download MacVim 7.3 here). One neat application of this feature is that when editing LaTeX files certain backslash commands are replaced by their corresponding Unicode glyph. This is what I am talking about:

You’ll see that Greek letter commands, superscripts/subscripts and mathematics commands are concealed and in their place is rendered the corresponding Unicode glyph. The cursor line however is rendered as is without any concealment so that you can still edit the LaTeX code. (I have on purpose made this line identical to the line above it to let you see what has been concealed.) Inline mathematics (that which goes between two dollar signs) is shown on the last line. Note that the dollar signs are hidden. All in all this makes it a lot more pleasant to skim through a .tex file (but it won’t replace compiling and reading the pdf instead).

To help get you on your way there are a few things you need to know in order to get started with the conceal feature. First of all you need to enable it by typing :set cole=2. You’ll immediately notice lots of grey on grey characters…uh, what? This is the (unfortunate) default syntax coloring for concealed items. To fix it you have to change the Conceal highlight group, e.g. try :hi Conceal guibg=white guifg=black (reverse the colors if you are using a dark color scheme). After fiddling a bit with the colors to match your color scheme you are ready to go!

However, I have found that concealed superscripts and subscripts often do not look very good and fortunately there is a way to disable them. Namely by adding the line let g:tex_conceal="adgm" to your ~/.vimrc file (it also works to put this line in ~/.vim/ftplugin/tex.vim as mentioned below). The g:tex_conceal variable is a string of one-character flags:

a = conceal accents/ligatures
d = conceal delimiters
g = conceal Greek
m = conceal math symbols
s = conceal superscripts/subscripts

Thus "adgm" means conceal everything except superscripts and subscripts. (I did not mention accents/ligatures earlier but "a" does what you’d expect: for example, \"a and \ae will turn into ä and æ respectively, if accents are enabled.)

The conceal support for editing tex files is still in its early stages and you may come across commands that do not get concealed, or perhaps you have some custom LaTeX commands that you would like to add to the list of concealed commands. In either case you should edit the file ~/.vim/after/syntax/tex.vim (create the folders and the file if they don’t exist). Say you would like \eps to render as ε, then add this line:

syn match texGreek '\\eps\>' contained conceal cchar=ε

Mathematics commands should be added to the texMathSymbol group. For example, if you want \arr to be concealed by ←, then add this line:

syn match texMathSymbol '\\arr\>' contained conceal cchar=←

If you find standard LaTeX commands that should be concealed but aren’t, please notify the tex syntax file author so that he may add them (you can find the contact details by looking at the syntax file :tabe $VIMRUNTIME/syntax/tex.vim).

Finally, I personally edit several different types of files and like to keep separate settings for each file type. The simplest way of doing this is to keep your filetype-specific settings inside ~/.vim/ftplugin/filetype.vim. Here’s an excerpt from my ~/.vim/ftplugin/tex.vim file:

" Set colorscheme, enable conceal (except for
" subscripts/superscripts), and match conceal
" highlight to colorscheme
colorscheme topfunky-light
set cole=2
let g:tex_conceal= 'adgm'
hi Conceal guibg=White guifg=DarkMagenta

Some of the relevant help files on this topic are :h 'cole, :h 'cocu, and :h conceal. I should also mention :h 'ambw; it may be helpful to set this to double if you find that some Unicode glyphs “spill over” into the neighboring display cell.

16 thoughts on “Using the conceal Vim feature with LaTeX

  1. Thanks for the documentation of this really neat feature, Björn! It really works like a charm. One small issue, though, which I had with my installation was that g:tex_conceal was ignored when I defined it in the ftplugin/tex.vim file. It appears that when this file was read, the syntax file was already loaded (and used its default setting ‘admgs’. Defining g:tex_conceal in the vimrc then worked perfect.

    • I guess it is safest to put the g:tex_conceal in ~/.vimrc then. This is what I had done initially too, but it seemed nicer to keep it in ~/.vim/ftplugin/tex.vim and since that worked for me I went with it. I have to read up on the order in which Vim reads configuration files I guess… :-)

  2. Hi, Björn!

    I downloaded and installed MacVim version 7.3 build 53 for PowerPC+x86 for Mac OS X 10.5 ‘Leopard,’ and I just have to say it is simply awesome!  If only all software were this good.

    Although this is a little selfish, I hope you’ll continue to maintain a PowerPC (or at least Universal Binary) version of MacVim for Mac OS X 10.5 ‘Leopard’ for the sake of people still using PowerPC-based Macs like myself, as Mac OS X 10.5 is the last version of Mac OS X that runs on PowerPC machines.  MacVim is the only text editor I’ve found (and believe me, I’ve searched far and wide) that makes editing mixed Hebrew-Arabic-Russian-English LaTeX and XHTML documents anywhere close to being tolerable.

    By the way, I came across the Readers’ Choice Awards 2009 from the Linux Journal, and I was interested to learn that among Linux Journal readers, the three most widely used text editors were found to be vi (36%) followed by gedit (19%) followed by Kate (11%).  Emacs didn’t even make the cut!

    Keep up the great work!

    —Austin, Haifa, Israel

  3. This is very nice. I find, with macvim Version 7.3 (53), that it is necessary to set the colours “manually” by doing a “:” command, however. I’m not sure why the colours are not set by my ~/.vimrc file, which I am pasting below, in case it’s of interest.

    $ cat ~/.vimrc
    set gfn=Monaco:h13
    set tw=70
    set cole=2
    “let g:tex_conceal= ‘adgm’
    hi Conceal guibg=Black guifg=White

  4. Pingback: Vim, LaTeX, and ‘conceal’ « PhilTeX

  5. Many thanks for the interesting blog!

    I added a couple of personal conceal rules to my config files, but struggled with a few. Can anybody help with these?

    I would like to replace ‘\{‘ by ‘{‘ and ‘\}’ by ‘}’. Also ‘\item’ by ‘•’. Finally I would like to replace ‘\,’ and ‘\;’ by ‘ ‘, i.e. an empty space (works fine with \quad and \qquad).


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s