Vim Tips Wiki
Advertisement

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created November 28, 2011 · complexity basic · author Wuzzeb · version 7.0

When you are editing a large LaTeX project, it is easy to jump around in Vim using tags, marks, file tree plugins, etc. One problem is after you jump to a tag in Vim the pdf open in other window does not change page. This tip will allow you to add a command to vim to change the page of the pdf to match the current location in Vim.

Finding pdf filename and line for current cursor

First, copy one of the OpenPDF functions below specific to your operating system.

Then drop the following functions in your tex.vim ftplugin file or your vimrc file

"Load PDF to the page containing label
function! LoadEvinceByLabel(l)
  for f in split(glob("*.aux"))
    let label = system('grep "^.newlabel{' . a:l . '" ' . f)
    let page = matchstr(label, '.\{}{\zs.*\ze}}')
    if ! empty(page)
      call OpenPDF(substitute(f, "aux$", "pdf", ""), page)
      return
    endif
  endfor
endfunction

"Load PDF to the page containing the nearest previous label to the cursor
function! EvinceNearestLabel()
  let line = search("\\label{", "bnW")
  if line > 0
    let m = matchstr(getline(line), '\\label{\zs[^}]*\ze}')
    if empty(m)
      echomsg "No label between here and start of file"
    else
      call LoadEvinceByLabel(m)
    endif
  endif
endfunction

The first function looks through the aux files created by compiling the latex file for the label passed in as a parameter. If it finds the label, it loads the page number and then loads the pdf to the given page.

The second function searches for the nearest label from the current position and calls LoadEvinceByLabel.

To use them, add something like:

map <buffer> <LocalLeader>e :call EvinceNearestLabel()<CR>

Now \e will load the pdf viewer to page containing the nearest label to the current cursor position. Note this works great on multi-file projects since the aux file is searched to find the pdf name. So you can jump around in Vim using all the wonderful tools and plugins (tags, marks, nerdtree, project, etc.) and then tell the pdf viewer to jump to the given page.

If you have set up tags, you can also add:

com! -nargs=1 -complete=tag Pdf call LoadEvinceByLabel("<args>")

for a command :Pdf that you can use tab completion on labels to jump to a given location.

Linux

On Linux, use the following OpenPDF function:

function! OpenPDF(file,page)
  exec 'silent ! evince --page-label=' . a:page . ' ' . a:file . ' > /dev/null 2>&1 &'
endfunction

Evince is smart enough so that if the pdf is already open, it just changes to the page of the existing window, and if the file is not open yet, it creates the new window and jumps to the given page.

Windows

Acrobat

This is currently untested since I don't have access to a Windows machine. Commands can be sent to Acrobat using [1]

function! OpenPDF(file, page)
  execute '!start cmcdde acroviewr10 control [DocOpen("' . a:file . '")][DocGoTo("' . a:file . ',' . a:page . '")]'
endfunction

If you use the vim-latexsuite plugin, acrobat needs to be closed before compiling which can be done with the following commands.

let s:AcroDDE = "cmcdde acroviewr10 control "
let s:ClosePdf = s:AcroDDE.'[DocOpen("%:p:r.pdf")][DocClose("%:p:r.pdf")] '
let g:Tex_CompileRule_pdf = s:ClosePdf.' & texify -bp --src $*'
let g:Tex_ViewRule_pdf =
        \ 'C:\Program Files (x86)\Adobe\Reader 10.0\Reader\AcroRd32.exe'

Mac

Need some ideas. Some AppleScript in OpenPDF should do the job.

Comments

Advertisement