Vim tricks and bits

Vim / Latex-Vim

I use Vim with the latex-suite. Some people don't like it, but I really think it's great. So there are two main configuration files: .vimrc for the general Vim bits, and .vim/ftplugin/tex/texrc for the vim-latex bits.

shortcuts and cooooool tricks

Paragraph remapping. Vim reformats paragraphs nicely, but also messes up your equations. This wonderful script rectifies that: now vim recognises things like \begin{equation} as the beginning of a new paragraph and won't format over it. Life changing!

Vim-latex predefines a number of shortcuts via IMAP. For example, typing '==' is automoatically expanded into '&=&', which I find extremely useful. I wanted to change some of these, and it took me a while to find where they were defined. The relevant files are

  • .vim/ftplugin/latex-suite/main.vim (if you installed latex-suite as a user in your directory)
  • /usr/share/vim/addons/ftplugin/latex-suite/main.vim (if you have access to the system-wide installation)

Type ctrl-a to augment the number under a cursor.

Parenthesis highlighting

In Vim 7, this comes in two flavours, both of which are badly documented: Either the matching brackets are highlighted whenever the cursor is over the parenthesis, or the matching bracket is shortly highlighted when its brother is typed. The first uses a plugin, which is very very annoyingly slow. It seems to be loaded by default but is switched off by adding let loaded_matchparen = 1 to the .vimrc file. The simple bracket highlighting is available for older versions of vim too. It is enabled by setting, again in the .vimrc file: set showmatch.

indents etc.

You can either use spaces or tabs for indent, but tabs are much better. To set the width of the tab, in .vimrc set for example " Set width of tab set ts = 3 If you have smartindent, the make sure the indent is the same as that used for tab width: " Switch smartindent on set si " Make sure width of smartindent is the same as that of tabs set sw=3 Usually, you also want to use vim's smartindent feature. However, when writing latex documents, this is a bit of a pain. To get rid of indentation specifically after 'for,while,if' etc. when editing .tex files, add set cinwords='' to your .vim/ftplugin/tex/texrc file.

Spell checker

Vim 7.0 comes with a neat in-built spell checker (much better than the old way of invoking ispell by \si ). To easily toggle it on and off, add this code to your .vimrc file: " Set spelling language.
set spelllang=en_gb
" Toggle spell checking for the current buffer with Ctrl-i
map <silent> <C-p> :setlocal invspell<CR>
imap <silent> <C-p> <ESC>:setlocal invspell<CR>a
which I stole from Yvan's Hole.

Blank screen after compilation with latex-suite

When I typed \ll, I used to get a message saying Ran Latex 1 time(s)
Press ENTER or type command to continue
but upon pressing enter, Vim would not redraw the screen, ie I'd end up with a blank screen. Pressing ctrl-l didn't help, I had to either scroll up one whole screen and then back down, or suspend and unsuspend vim. Turns out this is due to the multiple compile function, because setting TexLet g:Tex_MultipleCompileFormats = '' in the file ~/.vim/ftplugin/tex/texrc solved the problem. There is a bug report on this: Debian bug 365572.

Lee Patton suggests a nicer fix on his blog which keeps the multiple compile function.

wildcards

  • I usually am only interested in opening a few file types in vim, so I get vim to ignore all others. Specifially, getting it to ignore all the files generated when running latex, add this line to .vimrc :set wildignore=*.log,*.aux,*.dvi,*.aut,*.aux,*.bbl,*.blg,*.dvi,*.fff,*.log,*.out,*.pdf,*.ps,*.toc,*.ttt

environments

  • First, I use prosper to make slides (at times) and wanted to be able to type 'slide', hit F5 and get the whole environment, so I added the following to .vim/ftplugin/tex/texrc TexLet g:Tex_Env_slide = "\\\begin{slide}{<+title+>}\<++>\\\end{slide}\"
  • And something similar for pictures: TexLet g:Tex_Env_picture = "\\centering\\setlength{\\unitlength}{1cm}\\\begin{picture}(<+16,5+>)
    \\\put(<+0,0+>){\\includegraphics[width=<++>\\textwidth]{<+eps file+>}}\\\end{picture}\"
  • Bibtex shortcuts

  • When editing my reference.bib file, I like to add the URL from where I downloaded the paper (often just the doi). To get the shortcut BBB to produce this @ARTICLE{<+key+>,
    author = {<++>},
    title = {<++>},
    journal = {<++>},
    year = {<++>},
    volume = {<++>},
    number = {<++>},
    pages = {<++>},
    URL = {<++>},
    otherinfo = {<++>}
    }<++>
    ie to get it to add the 'URL' line, I edited the file /usr/share/vim/addons/ftplugin/latex-suite/bibtex.vim and added the following line let s:f_standsfor = 'URL' where the Fields are defined (just seach for a similar line). I then modified the global variable Bib_article_options by adding this line to the .vim/ftplugin/tex/texrc file: TexLet g:Bib_article_options = "vnpf"
  • Reverse search with Xdvi

    You can get Vim to a) open the tex file you're just viewing in a dvi viewer, and b) to jump to the right place. For this, you have to edit the compile rules of latex-vim, so that tags get added to the dvi files. Apparently this can cause trouble, though I haven't experienced any.
    • In the file .vim/ftplugin/tex/texrc , find the line which defines the dvi compile rule (it contains something like : TexLet g:Tex_CompileRule_dvi) and change it to TexLet g:Tex_CompileRule_dvi = 'latex -interaction=nonstopmode -src-specials $*'
    • Recompile your tex file (easily by hitting \ll
    • Position your cursor in vim on the bit you want to see in the dvi, and hit \ls
    • This works with xdvik 22.40v, and with vim 6.3 on an ubuntu machine. Don't know about other settings.

    Rebuild latex tree

    This is a command I can never remember: initexmf -u used to rebuild the latex tree. Run it whenever you add a package to the texmf directory.