Outdated!

The hack below is in the process of being replaced by a much cleaner solution posted by Ted Pavlic. I would advise you use his patch as it is easier to install than what is below and has a better long term future than what I propose. I will keep this page up, however, for reference.


Vim-LaTeX-PDFView

So, I started playing around with vim-latex. I became immediately jealous of how well designed the package is. The only problem is that I have become totally spoiled by my beloved TeXShop. Specifically, the ability to quickly switch between source and pdf have become part of how I work, and giving that up would be a harsh price to pay for vim-latex.

Happily, I discovered that I need not give up this feature. The secret is that there is a free (and awesome) PDF viewer for OS X called PDFView. It automatically updates whenever your file changes, and in addition it has some hooks to allow it to talk to VIM. What follows are some notes meant to clarify the process.

It is assumed that you have a functioning vim installation, X11, pdfsync, vim-latex, and Mac Vim. You will need to download and install PDFView.

Now, to get reverse searching up and running, you will need to install the following script somewhere: pdf_latex.scpt (save as Applescript). Next, open PDFView and go to Preferences > LaTeX and 1) click on Automatically reload documents 2) set PDFSync support to Custom 3) set Command to osascript 4) Set Arguments to full_path_to_pdf_latex.scpt "%file" %line. Finally, in System Preferences go to Universal Access and in Seeing select "Enable Access for Assistive Devices".

To get forward search running, you need to insert the following code snippit into ~/.vim/ftplugin/latex-suite/compile.vim Find the function "Tex_ForwardSearchLaTeX" (line 300 in my file). Somewhere in the function will be code of the form:


...
		let execString = 'silent! !'.viewer.' -name xdvi -sourceposition '.line('.').
		\ expand("%").' '.mainfnameRoot.'.dvi &'
		endif
	end
    call Tex_Debug("Tex_ForwardSearchLaTeX: execString = ".execString, "comp")
    execute execString
    if !has('gui_running')
...

Right before the call statement, place the following two lines

    unlet execString
	let execString = 'silent! !/Applications/PDFView.app/Contents/MacOS/gotoline.sh '.
	\ line('.').' "'.expand("%:p:r").'.pdf" '

Strangly, this works for terminal vim, but not gvim. It turns out that gvim defines 'macunix' whereas vim does not. This in turn causes different values for Tex_ViewRule_dvi to be set for the different versions. Hence, you should find the line near the top of Tex_ForwardSearchLaTeX() below and comment it out.

    " the default view format.
	" want to fail if no view rule for dvi (we want pdf, afterall).
    if Tex_GetVarValue('Tex_ViewRule_dvi') == ''
       return
    endif

If you comment out the entire three-line if statement, then forward searching will work for gvim.

As a final optimization, if you want PDFView to come to the front after a forward search, add the following after the execute execString command:

execute 'silent! !open -a PDFView'

This is a complete hack and will kill you ability to use DVI for forward search, but there you go. Note that if you keep PDFView.app somewhere other than /Applications you will need to adjust appropriately.

Finally, to use this system, just place the line \usepackage{pdfsync} in the preamble of any latex file you are editing. Compile away (using \ll in vim-latex). Now, typing \ls in visual mode will jump to the pdf file based on the line number in the source. Command-clicking in the PDF will take you back to vim (well, gvim). Cheers.

UPDATE: I've been working with multifile projects, and my hack does not work on any file but the main file. The right thing to do is to parse the pdfsync file, find the appropriate number, and feed it into the applescript. Sadly, I can't justify the time to do this, but if you can, please send me the code!