The Mac latex editor 'TeXShop' has a great feature -- I can right-click anywhere in my tex and it shows me the corresponding character of the generated PDF. Similarly I can click on PDF and jump to that character in the tex source code.

Is it possible to do the same thing in vim, and some PDF viewer? (I'm interested on Mac myself)

  • It might be possible to go from vim to pdf, but the other way around requires functionality in the pdf view to send back some sort of message to vim reporting where in the tex file to jump.
    – jecxjo
    Apr 16 '15 at 15:48
  • Such a thing exists (you can pass --synctex=1 to pdflatex, and it is understood by some PDF viewers). I don't know to feed that information back to vim however. Apr 17 '15 at 14:08
  • It looks like there was a synctex + vim discussion over at the Tex site (note its for Linux but Mac shouldn't be much different) tex.stackexchange.com/questions/2941/…
    – jecxjo
    Apr 17 '15 at 14:14

For the first situation (tex selection to PDF) if you have a tex compiler that allows for input from stdin you could echo your visual selection.

function! EchoPipeCmd(cmd) range
  echo system('echo ' . shellescape(join(getline(a:firstline, a:lastline), "\n")) . ' | ' . a:cmd)

command! -range=% -nargs=+ EchoPipeCmd :<line1>,<line2>call EchoPipeCmd(<q-args>)

If you visually selection a block and enter :'<,'>EchoPipeCmd command the selection is sent to /bin/echo as an escaped string and then piped to the command you provide. As an example

:'<,'>EchoPipeCmd tee /tmp/output.txt

will use tee to both echo the selected text and also write it to /tmp/output.txt.

If you want to make a command and hardcode in the tex compiler info you'd do something like this (assuming the command to compile is tex --stdin -o <filename>)

command! -range=% -nargs=0 CompileTex :<line1><line2>call EchoPipeCmd("tex --stdin -o /tmp/output.pdf")

Now you can visually select and call :'<,'>CompileTex and you'll get a pdf file.

  • This isn't quite what I want... with texshop, it runs latex on your whole document, opens the PDF viewer, and then moves to the line in the PDF which lines up with the currently open line in vim. Apr 17 '15 at 14:14
  • That's much simpler to achieve but the moving of the PDF view might be a bit tricky. vim has a built in set of compiler functions. Check out :h compiler-tex. After setting up a few options you can basically call :make % and it will compile your latex document.
    – jecxjo
    Apr 17 '15 at 14:19

Yes, this is possible. However, the answer depends on the choice of PDF viewer, and it is not necessarily straightforward.

In order to enable such synchronization, you must enable synctex.

Due to the complexity of the answer, I will instead refer you to my LaTeX plugin vimtex, which has implemented such synchronization support for several PDF viewers. More viewers may be added relatively easily if desired/needed. Here is an example of how a command may be built that opens a PDF viewer (in this case SumatraPDF, which is only available on Windows) and performs a forward search from the vim location to the corresponding location in the PDF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.