LaTeX runtime for NeoVim

by Willie Wong

I’ve just recently migrated to using NeoVim instead of traditional Vim. One of the nice features in NeoVim (or nvim) is that it now supports asynchronous job dispatch. This makes it a bit nicer to call external previewers for instance (otherwise the previewer may block the editing). So here are the latest LaTeX runtime code that I use, modified for NeoVim.

function Dvipreview()
	let dviviewjob = jobstart(['xdvi', '-sourceposition', line(".")." ".expand("%"),  expand("%:r") . ".dvi"])

function PDFpreview()
	let pdfviewjob = jobstart(['evince', expand("%:r") . ".pdf"])

au BufRead *.tex call LaTeXStartup()

function LaTeXStartup()
	set dictionary+=~/.config/nvim/custom/latextmp/labelsdictionary
	set iskeyword=@,48-57,_,:
	call SimpleTexFold()
	set completefunc=CompleteBib
	set completeopt=menuone,preview
	runtime custom/latextmp/bibdictionary
	call SetShortCuts()

function SimpleTexFold()
	exe "normal mz"
	set foldmethod=manual
	if search('\\begin{document}','nW') 
		if search('\\section','nW')
		while search('\\section','nW')
	if search('\\begin{entry}','nW')
		while search('\\begin{entry}','nW')
	exe "normal g`zzv"

function SetShortCuts()
	" Map <F2> to save and compile
        imap <F2> ^[:w^M:! latex -src-specials % >/dev/null^M^Mi
        " Map S-<F2> to save and compile as PDF 
        " apparently <S-F2> sends the same keycode as <F12>?
        imap <F12> ^[:w^M:! pdflatex % >/dev/null^M^Mi
        " Map <F3> to Dvipreview()
        imap <F3> ^[:call Dvipreview()^M
        " Map S-<F3> to PDFpreview()
        " apparently <S-F3> = <F13>
        imap <F13> ^[:call PDFpreview()^M
        " Map <F4> to bibtex
        imap <F4> ^[:! bibtex "%:r" >/dev/null^M^Mi
        " Map <F5> to change the previous word into a latex \begin .. \end environment
        imap <F5> ^[diwi\begin{^[pi<Right>}^M^M\end{^[pi<Right>}<Up>
        " Map <F6> to 'escape the current \begin .. \end environment
        imap <F6> ^[/\\end{.*}/e^Mi<Right>
        " Map <F7> to search the labels dictionary for matching labels
        imap <F7> ^[diwi\ref{^[pi<Right>^X^K
        " Map <F8> to rebuild the labels dictionary
        imap <F8> ^[:w^M:! ~/.config/nvim/custom/ %^M^Mi
	" Map <F9> to search using the bibs dictionary
        imap <F9> ^[diwi\cite{^[pi<Right>^X^U
        imap <S-Tab>C ^[diwi\mathcal{^[pi<Right>}
        imap <S-Tab>B ^[diwi\mathbb{^[pi<Right>}
        imap <S-Tab>F ^[diwi\mathfrak{^[pi<Right>}
        imap <S-Tab>R ^[diwi\mathrm{^[pi<Right>}
        imap <S-Tab>O ^[diwi\mathop{^[pi<Right>}
        imap <S-Tab>= ^[diWi\bar{^[pi<Right>}
        imap <S-Tab>. ^[diWi\dot{^[pi<Right>}
        imap <S-Tab>" ^[diWi\ddot{^[pi<Right>}
        imap <S-Tab>- ^[diWi\overline{^[pi<Right>}
        imap <S-Tab>^ ^[diWi\widehat{^[pi<Right>}
        imap <S-Tab>~ ^[diWi\widetilde{^[pi<Right>}
        imap <S-Tab>_ ^[diWi\underline{^[pi<Right>}


Pay attention that the control characters did not copy-paste entirely correctly in the SetShortCuts() routine. Those need to be replaced by the actual control-X sequences. The read labels shell script is simply

grep '\label{' $1 | sed -r 's/.*\\label\{([^}]*)\}.*/\1/' > ~/.config/nvim/custom/latextmp/labelsdictionary

(I probably should observe the proper directory structure and dump the dictionary into ~/.local/share/ instead.)