2

I want to map F2 to compiling tex files, and F3 for viewing tex files.

This is what I put in my .vimrc:

if &filetype == "tex"
nnoremap <F2> :Latexmk<cr>
inoremap <F2> <Esc>:Latexmk<cr>a
nnoremap <F3> :LatexView<cr>
inoremap <F3> <Esc>:LatexView<cr>a
endif

These Latex* commands are from LaTeX-Box plugin. I can execute them manually without any problems. Typing :echo &filetype in any *.tex file returns tex.

Hanlon
  • 432
  • 4
  • 13
  • your vimrc is only sourced at the start of vim, but you will open files after that, it does not work with an simple if. HAve a look at `autocommands` (type `:h autocmd` in vim) – Doktor OSwaldo Feb 05 '18 at 08:56
  • @DoktorOSwaldo Ok, I understand. autocmd FileType tex nnoremap :Latexmk works (and so does every other command in my question above) if I open tex file, and it does not work when I open non-tex file, which is exactly what I wanted. But if I open tex file and non-tex file in separate tabs (or split windows) then upon pressing F2 Vim asks for LaTeX file if non-tex file is focused (if tex file is focused everything is normal). Is there a way to resolve this? – Hanlon Feb 05 '18 at 09:57
  • yes, use ``: `nnoremap ` or else the map wil work on every buffer – Doktor OSwaldo Feb 05 '18 at 10:43
  • I don't understand that "syntax". Please, give example with one of the commands from my question. – Hanlon Feb 05 '18 at 10:46
  • have a look at `:h ` there you will find it ;) If you have problems to understand it, just ask again. – Doktor OSwaldo Feb 05 '18 at 10:47
  • Ok. I get it now! – Hanlon Feb 05 '18 at 10:51

2 Answers2

3

You have two methods…

  • Create a self-clearing group in your vimrc and add as many autocommands as needed:

    augroup Tex
        autocmd!
        autocmd FileType tex nnoremap <buffer> <F2> :Latexmk<cr>
        autocmd FileType tex nnoremap <buffer> <F3> :LatexView<cr>
        autocmd FileType tex inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
        autocmd FileType tex inoremap <buffer> <F3> <Esc>:LatexView<cr>a
    augroup END
    
  • Use a ftplugin:

    Put the following in after/ftplugin/tex.vim:

    nnoremap <buffer> <F2> :Latexmk<cr>
    nnoremap <buffer> <F3> :LatexView<cr>
    inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
    inoremap <buffer> <F3> <Esc>:LatexView<cr>a
    

The second method is recommended because it is a lot cleaner and less expensive than the first.

romainl
  • 186,200
  • 21
  • 280
  • 313
1

I replaced the code above with this:

autocmd FileType tex nnoremap <buffer> <F2> :Latexmk<cr>
autocmd FileType tex nnoremap <buffer> <F3> :LatexView<cr>
autocmd FileType tex inoremap <buffer> <F2> <Esc>:Latexmk<cr>a
autocmd FileType tex inoremap <buffer> <F3> <Esc>:LatexView<cr>a
Hanlon
  • 432
  • 4
  • 13
  • Since it does not solve your problem (not correctly), this should be an update to your question, not an answer. However, since it is an easy fix and you are new, it is ok. Just add the `` there to make it complete. – Doktor OSwaldo Feb 05 '18 at 10:45
  • Beteween declaration of map type and map itself. – Hanlon Feb 05 '18 at 10:55
  • @Vuk Welcome to [so], and thanks for contributing! Be sure to "accept" your answer once the time-limit passes (via the check mark). That lets future readers know there is a solution to the problem. – jpaugh Feb 05 '18 at 16:18