3

Suppose I am editing a file.tex. How would I open file.pdf in zathura (or any pdf viewer) from vim?

I understand how to reference file.tex using '%' but I'm not sure how to reference file.pdf.

7

% indeed stands for the current file. %:r (see :help filename-modifiers) strips off the file extension. You can then use :! to launch an external command, e.g. the PDF viewer:

:! zathura %:r.pdf
| improve this answer | |
2

You can do something like this

let main = expand('%')
let pdf = fnamemod(main, ':r') . '.pdf'

The above steps may of course be combined into a single step. Given the above, you can now open the file in zathura as follows:

silent execute 'zathura' . pdf

I propose you combine all of the above in one function, which makes it easy to map. E.g.:

nnoremap <silent> \lv :call OpenInZathura()<cr>
function! OpenInZathura()
  let main = expand('%')
  let pdf = fnamemod(main, ':r') . '.pdf'
  silent execute 'zathura' . pdf
endfunction

I'll take the liberty of mentioning my LaTeX-plugin vimtex, which has support for zathura and other pdf viewers. For zathura, vimtex sets up both forward and backward search with synctex. It also handles more complex cases such as multifile projects.

| improve this answer | |

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.