The way I like to do this is by using tmux to send the compilation command to another terminal. I like this method because it behaves as if you had typed the command in that terminal yourself.
Add this to your .vimrc
(note this code uses latexmk
instead of pdflatex
):
function! TmuxSend()
let tex_file = expand('%:p')
let tex_cmd = 'latexmk -pdf -interaction=nonstopmode -cd ' . tex_file
let tmux_cmd = 'tmux send-keys -t vim_output.0 "' . tex_cmd . '" ENTER'
let output = system(tmux_cmd)
endfunction
autocmd FileType tex :nnoremap <silent> <buffer> <F5> :call TmuxSend()<CR>
Now, open another terminal and create a tmux session named "vim_output": tmux new-session -s vim_output
. Now, when you press F5 in Vim, the latexmk
command will be run in the terminal you just created.