Since compiling a .tex file make take a few seconds, I prefer to do it in the background. There are two ways I've done this in the past: by running an asynchronous command using job_start()
, and by using tmux to send the compilation command to another terminal.
1. using job_start()
Add the following to your .vimrc:
function! RedrawScreen(channel)
redraw!
endfunction
function! RunLatexmk()
let tex_file = expand('%:p') " current file
let tex_cmd = 'latexmk -pdf -interaction=nonstopmode -cd ' . tex_file
let s:job = job_start(['/bin/sh', '-c', tex_cmd],
\ {'close_cb': 'RedrawScreen'})
endfunction
autocmd BufWrite *.tex :call RunLatexmk()
I have defined the RedrawScreen()
callback function because my screen often gets messed up when the job_start()
job completes. Note that this uses latexmk -pdf
which calls pdflatex
. If instead you want to use pdflatex
directly, you will have to first cd
into the directory of the .tex file before you can call pdflatex
.
2. using tmux to send the latexmk
command to another terminal
Add the following to your .vimrc:
function! TmuxSend()
let tex_file = expand('%:p') " current file
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 BufWrite *.tex :call TmuxSend()
Now, open another terminal and create a tmux session named "vim_output": tmux new-session -s vim_output
. Now, when you save your file in Vim, the latexmk
command will be run in the terminal you just created.
:Latekmk
command of latex-box plugin? Or are you looking for an autocommand to execute something on a file writing... Please clarify your question by indicating what you have searched and found so far.latexmk -pdf -pvc file.tex
)