Vim Tips Wiki
Tip 225 Printable Monobook Previous Next

created 2002 · complexity basic · author Dimitri Antoniou · version 6.0+

Vim can interact with the tricks that the latest xdvi or kdvi does:

  • If one clicks at some place in xdvi (usually Ctrl+Button1), Vim automatically jumps to the corresponding line in the LaTeX source file ("reverse search")
  • Also, from inside Vim, one can jump to the corresponding line in xdvi which becomes highlighted ("forward search").

First of all, the latex document need to tell the latex compiler to set marks in the dvi file. Only put:


in the preamble of your latex document, or compile with:

latex --src [your_file.tex]

It works perfectly well with Vim or gvim on one side and xdvi on the other side, even with multiple couple (vim, xdvi). It works also fine with kdvi but can be a little less "smooth".

With xdvi[]

It's very simple. In your .vimrc file, add at the end:

:map \ld :execute '!xdvi -editor "vim --servername 'v:servername' --remote +\%l \%f" -sourceposition ' . line(".") . expand("%") . " '" . expand(Tex_GetMainFileName(':r')) . ".dvi' >/dev/null&" <CR><CR>

and in your .bashrc:

alias vim='vim --servername vimtex'

The first allows the forward search by \ld in normal mode.

To allow the reverse search, Vim has to be opened with vimtex servername; that's the reason for the second line. (I don't use the normal shortcut \ls as that doesn't allow me to use the v:servername variable, see end of this tip.)

Keep in mind that any other program that communicates with Vim server should be aware of the name change (for example jabref). A little script like ouvre can avoid to systematically name the Vim server "vimtex".

Note: On my system (Ubuntu 8-10, with the latex-suite installed via vim-addon-manager) this works, but only if I substitute 'gvim' for 'vim'. Also, for things to work I must, in order, 1) Open my tex file with gvim, 2) Open my dvi file with the command \ld 3) reverse-search using Ctrl + left-mouse-click.

With kdvi[]

If you run it on gnu/linux, I've made a little script to do the work for you (for use with Vim): but this doesn't allow running multiple couples of (vim, kdvi). The tips for xdvi would apply, but I've no more time to set it.

Then in kdvi in "configuration> configure kdvi > dvi specificity" set Editor to "vim".

You can then forward search in Vim or gvim by \lf in normal mode.

If you want to use it from gvim only, in the script change:

echo "alias vim='vim --servername KDVI'" >> ~/.bashrc


echo "alias gvim='gvim --servername KDVI'" >> ~/.bashrc

It works by a little script in your ~/bin/ directory.

Here is the install script:

if [[ ! -e ~/bin ]] ; then
mkdir ~/bin/
# setting the script for forward search
echo "#"'!'"/bin/bash" > $script_name
echo "doc_dvi=\${1%.tex}.dvi" >> $script_name
echo "ligne=\$2" >> $script_name
echo "doc_partie=\$3" >> $script_name
echo "kdvi -u file:\$doc_dvi\#src:\$ligne\$doc_partie &" >> $script_name
echo "exit 0" >> $script_name

chmod +x $script_name

# setting of vim for forward search
echo "\" \" To use kdvi forward search, comment it if you don't want to." >> ~/.vimrc
echo ":map \\lf :execute \""'!'"$script_name \". Tex_GetMainFileName()\" \". line(\".\")\" \".expand(\"%\")\"& \" <CR><CR> " >> ~/.vimrc

echo "# To use reverse search with kdvi" >> ~/.bashrc
echo "alias vim='vim --servername KDVI'" >> ~/.bashrc

exit 0

Windows tip[]

If you use the MikTeX distribution on Win32, you can play the same game with Vim and yap. I use the following with Vim 6.0 and (old) MikTeX 1.20b.

From my Vim script for tex files:

execute '!start yap -1 -s' line(".") . expand("%:t") expand("%:p:r")

(Of course, I have a :map and :menu to do this.) This expands to something like:

!start yap -1 -s 1353foo.tex c:\path\to\foo

In yap, from the View/Options panel, choose the "Inverse Search" tab and enter:

gvim --remote +%l %f

in the "Command line" box. This can probably be improved: doesn't the installer put a .bat file somewhere?

The following mapping may also be useful (in one line):

map _g :exe 'silent !xdvi -editor "vim --servername ' . v:servername . ' --remote +\%l \%f" -sourceposition ' . line (".") . expand("%") . " " . expand("%:r") . ".dvi &" \| redraw!<CR>

It starts up an xdvi (if necessary) and does a forward search. Reverse search will jump to the location in the same Vim.

As a followup to benji's excellent tip for yap, (win32) the command :!start as opposed to :! does a kind of fork, so Vim won't wait for the executed program to finish.

That is, type:

exe 'silent !start yap -1 -s' line(".") . expand("%:t") expand("%:r") .'.dvi'

so you don't have to close yap before Vim becomes responsive again. See :help :!start.

I make use of the above a lot, and it usually works great. However, to handle spaces in the file path, it needs a little modification:

exe 'silent !start yap -1 -s' line(".") . expand("%:t") '"' . expand("%:r") . '.dvi' . '"'

The corresponding mapping (to _g) is:

map _g :exe 'silent !start yap -1 -s' line(".") . expand("%:t") '"' . expand("%:r") . '.dvi' . '"'<CR><CR>

I am using the following in my ~/.Xdefaults:

xdvi.editor: gvim --servername %f --remote-silent +%l %f

With the advantage that I can have multiple Xdvi instances with different files and they all know, what Vim they have to address to.

Also, you may have multiple Xdvi instances with the same file and they will all interact with the same Vim.

There's not clientserver support on vanilla Vim [to be verified as it works well now with Vim].

Problem solving[]

This part has been partly reviewed, other contributors welcome.

0-Check the configuration of the source file (file.tex)

Check you've got the package srcltx.sty and \usepackage{srcltx} (which should be commented out when one finishes and is ready for printing etc).

If you do not have the LaTeX package srctex, try compiling your file with:

latex --src [your_file.tex]

which works for me.


Forward and reverse search only works for quite recent xdvi versions (>= 22.38), (or recent Unix/Linux versions), xdvi can interact with any editor provided with a client-server mode. Vim as to be compiled with the client-server option, on Ubuntu 8.04 it's ok, for gvim it's usually the case. On Linux Mandrake 9.0. gvim is compiled with the client-server option and Vim is not.

1-Check that your xdvi version provides the "source-special" functions, by typing 'xdvi -version' or 'man xdvi'

2-Configuration of xdvi

xdvi has to be called with the following options

xdvi -editor 'gvim --remote-wait +%l %f' file.dvi &

(or with 'xterm -e vim --remote-wait +%l %f' if Vim has been compiled with the client-server option).

To make it easier to use you can define an environment variable XEDITOR to set the value of 'editor' or add the following line in your .Xdefaults xdvi.editor: gvim --remote-wait +%l %f Launch 'xrdb -load ~/.Xdefaults' to make sure that xdvi.editor is set to the good value. If you have defined such a variable, xdvi will always be launched with the -editor option. To avoid this unsafe behavior, launch 'xdvi -safer'.

3-Conversely, if you call xdvi within gvim with the -sourceposition option, cursor is placed in the dvi file on the corresponding position.

Syntax is:

xdvi -sourceposition l:cfile.tex file.dvi

l is the line number, c is the column number

Be careful: there is no space between "c" (column number) and file.tex.

To make it easier to use, you can do the following.

First create a file named for example xdvi_source.vim with following lines.

" This script calls xdvi source and puts cursor in xdvi viewer at
" the position corresponding to the position in the source file
:exec "silent !xdvi -sourceposition ".line(".").':'.col(".")."%".' '."%:r.dvi"

Then launch the following command within gvim editing the .tex source file. Pressing simultaneously Ctrl and → will open the dvi file at the right position:

:noremap <buffer> <C-Right> :so xdvi_source.vim<CR>

Seems it doesn't work for multiple tex source files.

You don't actually need to create that extra file (I prefer my directories to be 'clean' and I always forget to copy the necessary files when starting a new document in a new directory). You can just type:

:noremap <buffer> <C-Right> :exec "silent !xdvi -sourceposition ".line(".").':'.col(".")."%".' '."%:r.dvi"<CR>

The problem I run into very often, though, is that I often split my LaTeX source into multiple files (usually one per chapter). The problem is that


is than usually wrong (e.g. it should be book.dvi, while the file being edited is chapter1.tex). I think it should be possible to check for files in the directory containing the pattern "{include}\|{input}\{%<" in which case it is PROBABLY this file that will result in the dvi. No way to be sure though, so it is probably best to have a texrc file for every project (extra file, darn!).

If it doesn't work with previous tips, getting xdvi work without support for multiple couple xdvi/vim works by adding:

:let g:Tex_ViewRule_dvi = 'xdvi -editor "vim --servername vimtex --remote +\%l \%f"'

To your .vimrc file and aliasing "vim" to "vim --servername vimtex"

I split my LaTeX source into multiple files and directories. I use a main directory with the main .tex file and a tree of directories for each chapter. To fix this tip I used:

:map \ld :execute '!cd ' . expand(Tex_GetMainFileName(':p:h')) . ' && xdvi -editor "gvim --servername 'v:servername' --remote-wait +\%l \%f" -sourceposition ' . line(".") . substitute(expand('%:p'),expand(Tex_GetMainFileName(':p:h')).'\/','','') . " " . expand(Tex_GetMainFileName(':p:r')) . ".dvi &" <CR><CR>


For kdvi the command should read:

kdvi file:name.dvi#src:lname.tex

where l is the linenumber and name is the filename.