I'm using Latex a lot lately and I find myself having to leave vim so I can execute the command pdflatex *filename* too often (compile the code and create a pdf file).

I'd like to make a command command! Latex execute "pdflatex %" something like that to be able to compile the code directly from vim but I don't know how to expand the filename since "pdflatex %" is a string and the terminal understands it as so.

How could I have the filename expand to what I want?

  • So you want pdflatex *filename* to run on your shell, right?
    – Tumbler41
    Dec 19, 2016 at 22:57

1 Answer 1


expand() is what you need.

command! Latex execute "!pdflatex " . expand('%')
  • Works fine, thanks. If I wanted though, every time I executed the command, to save the file first how could I do it?
    – Wylex
    Dec 19, 2016 at 23:03
  • 1
    That would be: command! Latex execute ":w | !pdflatex " . expand('%')
    – grodzik
    Dec 19, 2016 at 23:07
  • I just found out it doesn't work if the file name contains a black space. The compiler interprets it as two different files. Any idea how to fix it?
    – Wylex
    Dec 19, 2016 at 23:13
  • 2
    Try wrapping expand('%') inside shellescape(expand('%'))
    – grodzik
    Dec 19, 2016 at 23:16
  • 2
    @Wylex command! Latex execute "!pdflatex % should work, you don't need the expand() here since :! already does that. The command in your question is missing a ! though. Dec 19, 2016 at 23:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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