Vim Tips Wiki
Line 42: Line 42:
 
The first allow 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 of the second line. (I don't use the normal shortcut "\ls" for it doesn't allowed me to use the "v:servername" variable, see end of this tip)
 
The first allow 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 of the second line. (I don't use the normal shortcut "\ls" for it doesn't allowed me to use the "v:servername" variable, see end of this tip)
   
Keep in mind that other program that communicate with vim server should be aware of the name change (for example jabref). A little script like [http://corentin.barbu.free.fr/code/stable/ouvre ouvre] can avoid to sistematically name the vim server "vimtex".
+
Keep in mind that other program that communicate with vim server should be aware of the name change (for example jabref). A little script like [http://corentin.barbu.free.fr/codes/stable/ouvre ouvre] can avoid to sistematically name the vim server "vimtex".
   
 
==With kdvi==
 
==With kdvi==

Revision as of 17:29, 4 December 2008

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 225 Printable Monobook Previous Next

created March 20, 2002 · complexity basic · author Dimitri Antoniou · version 6.0


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

  • If one clicks at some place in xdvi, 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 :

\usepackage{srcltx}

In the preambule 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 &" <CR><CR>

and in your .bashrc :

alias vim='vim --servername vimtex'

The first allow 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 of the second line. (I don't use the normal shortcut "\ls" for it doesn't allowed me to use the "v:servername" variable, see end of this tip)

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

With kdvi

If you run it on gnu/linux, I've maid a little script that make the work for you (for use with vim) : install_fsearch_kdvi.sh but this doesn't allow to run multiple couple of (vim kdvi) the same tips than ahead 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 make forward search in vim or gvim by "\lf" in normal mode.

If you want to use it from gvim only change :

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

to

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

It works with a little script in your ~/bin/ directory. Please review it in case it doesn't work.

Here is the source code of the install script :

#!/bin/bash
script_name=~/bin/fsearch_kdvi.sh
if [[ ! -e ~/bin ]] ; then 
mkdir ~/bin/
fi
# 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 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

i.e. 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 do 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 know with vim].



Problem solving

This part has been partly reviewed, other contributors welcome. It's 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.

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

1-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).

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 environement 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>

Comments

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


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

latex --src [your_file.tex]

which works for me


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

"%:r.dvi"

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!)


For kdvi the command should read:

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

where l is the linenumber and name is the filename normal way would be :


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

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

To your .vimrc file