LATEX and VI

I use vi (strictly speaking, vim) in Linux (and Mac OS X) to write LaTeX documents.  I have found it useful to have the following three lines—explained below—in my .vimrc file:


  map ^T :!/bin/cp % /tmp/`pwd \| sed "s/\//__/g"`__%^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M:!openout_any=r bibtex /tmp/`pwd \| sed "s/\//__/g"`__`echo % \| sed 's/\(.*\)\.tex/\1.aux/' \| sed 's/\(.*\)\.TEX/\1.aux/'`^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M

  map ^Y :!xdvi -geometry 1350x800 -copy -keep -xoffset 1.0cm -p 1370 -bg white /tmp/`pwd \| sed "s/\//__/g"`__`echo % \| sed 's/\(.*\)\.tex/\1/' \| sed 's/\(.*\)\.TEX/\1/'` >>/tmp/xdvi_error_messages.log 2>&1 &^M 

  map <F10> :!/bin/cp % /tmp/`pwd \| sed "s/\//__/g"`__%^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M:!bibtex /tmp/`pwd \| sed "s/\//__/g"`__`echo % \| sed 's/\(.*\)\.tex/\1.aux/' \| sed 's/\(.*\)\.TEX/\1.aux/'`^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M:!dvips   -f /tmp/`pwd \| sed "s/\//__/g"`__`echo % \| sed 's/\(.*\)\.tex/\1.dvi/'` \| /usr/bin/lpr^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD^[OD

If you do not use BibTeX, the first of the three preceding lines can be simplified to:


  map ^T :!/bin/cp % /tmp/`pwd \| sed "s/\//__/g"`__%^M:!latex -output-directory=/tmp -interaction nonstopmode /tmp/`pwd \| sed "s/\//__/g"`__%^M

(Note that the control characters must be inserted into the .vimrc file using Control-v; for example, the

  ^T

above is entered as “Control-v Control-t.”  The character

^[M

is entered as “Control-v” followed by the “Enter” key.  The character

^[OD

is entered as “Control-v” followed by the left arrow key.)

The first of the three lines above enables one to use “Control-t” (in vi command mode) to copy the file being edited into the /tmp directory (with the file name prefixed by a derivative of the present working directory, to avoid collisions) and then run LaTeX on that file.  I find this preferable to running LaTeX on the file in its own directory, as the directory then gets cluttered with .dvi, .log, .aux files, etc. 

The second of the three lines above enables one to use “Control-y” (in vi command mode) to view the DVI file produced from the LaTeX document using xdvi.  (The xdvi flags are of course tailored to my own computer and most likely require modification on a different machine, in particular the 1350 by 800 geometry of the window, the 1370 pixel per inch font size, and the one-centimeter horizontal offset.  Incidentally, the reason for redirecting standard output and standard error to the file /tmp/xdvi_error_messages.log is that otherwise xdvi messages will annoyingly appear right in the terminal you are using, obscuring the text of the file you are editing.)  Subsequent uses of “Control-t” modify the DVI file being viewed within the xdvi window; it is not necessary to open a new window with an additional “Control-y.”

The third of the three lines above enables one to use the function key “F10” (in vi command mode) to print the DVI file produced from the LaTeX document.  The large number of left arrow keys at the end moves the cursor back to the position after the “dvips” command, so that one can insert, for example, “-pp2-5” before hitting “Enter” if one only wants to print pages 2 through 5; otherwise, one can simply hit “Enter” to print the whole document, or “Escape” to cancel the command.

Of course, various other LaTeX-specific commands can be useful in the .vimrc file, for instance:

  map! ^F \frac{1}{}^[?1}{^Mlla

(creates a fraction in vi insert mode, and after entering the denominator and escaping to vi command mode, typing “n” will typically position the cursor over the “1” in the numerator, which can then be edited).

Though not specific to LaTeX, it is also possible to edit the .vimrc file so as to enable vi to automatically back up files.

Greg Marks