I am currently running the following configuration for latex: NVim 0.9, Vmitex and Evince.

I have been trying to map a key to open <current-file>.log, while the current-file is a name taken from a currently opened file without extension, e.g. from something.tex.

Here is my first attempt:

autocmd FileType tex nmap <buffer> <F5> :view \| split %:p:r.log<CR>

while it works it also creates multiple issues. Essentially the *.log can be recreated, deleted by external process latexmk, not existing etc. For instance, if the file was opened by nvim, then closed but later the file was cleared by external process, nvim would warn about it but would also bring the old version of the file, probably from a swap.

Therefore, I'd rather check if the file exists before I open it and display a message if the file is not there.

The following is what I've tired to accomplish:

autocmd FileType tex nmap <buffer> <F5> if !empty(glob(expand("%:p:r.log"))) | view \| split %:p:r.log<CR> | else | echo expand("%:p:r").'.log does not exist!' | endif

Unfortunately I've got error parsing the init.vim:

Error detected while processing BufReadPost Autocommands for "*":
Vim(else):E581: :else without :if:  else 

3 Answers 3


Try using <bar> with :*map commands:

autocmd FileType tex nmap <buffer> <F5> if !empty(glob(expand("%:p:r.log"))) <bar> view <bar> split %:p:r.log<CR> <bar> else <bar> echo expand("%:p:r").'.log does not exist!' <bar> endif

and consider writing nnoremap, and using ftplugins with functions:

" ~/.vim/after/ftplugin/tex.vim

function s:open_log(log_file) abort
  if filereadable(a:log_file)
    execute 'sview' a:log_file
    echomsg a:log_file 'does not exist'

nnoremap <buffer> <F5> :call s:open_log(expand('%:p:r.log'))<CR>
  • Hi. Thanks for the answer. What's the advantage of nnoremap over nmap here? I don't think I fully understand it. I thought it would have benefit if I tried to map <F5> to another key which was mapped to something else and I wouldn't like the other key to get executed. Since I am mapping a single key to a function, I can't see the advantage. Thanks.
    – Celdor
    Nov 3, 2022 at 18:39
  • *noremap commands affect the right-hand side expansion, not the left. Effectively, without it, any keys on the right-hand side that are mapped are expanded as mappings. With nore, you are guaranteed that the commands behave as their unmapped selves. In general, stick to the nore variants unless and until you need the variants without it (such as when creating a mapping that uses another mapping, which needs to be recursive). See also the difference between :normal and :normal!
    – D. Ben Knoble
    Nov 3, 2022 at 19:38
  • That was probably explained in the manual and I just missed it. Thanks :)
    – Celdor
    Nov 3, 2022 at 20:39
  • 1
    @Celdor It is, but so are the answers to 95% of the questions on this site! Sometimes it just helps to have someone else explain it in a different way. We've all been there!
    – Rich
    Nov 4, 2022 at 9:02

I think you just need to change your pattern in the autocmd to *.tex

A file like abc.txt will not match the autocmd pattern tex.

  • 1
    IMO autocmd FileType tex is recognised as it should be. Otherwise, the first version of my attempts would not work and it does work; BTW I added : in :view so that it is correctly parsed as a norm command. It seems the issue of the second version is with parsing if, as if the if is not recognised at all.
    – Celdor
    Nov 2, 2022 at 15:40
  • My mistake. I had not tested an autocmd pattern without the *. All the examples in $VIMRUNTIME/filetype.vim use the star and I assume that pattern matching worked like in a shell command line rather than regex. Nov 2, 2022 at 16:04
  • 2
    @ChrisHeithoff Not all autocommands match the pattern against the file name. For FileType autocommands, it matches against the filetype.
    – Rich
    Nov 4, 2022 at 9:00

I found a similar problem in this post. Apparently, map class of commands also accept conditional expressions but this has to be flagged by adding <expr> as an option.

In case someone has a similar problem, the following command finally works:

autocmd FileType tex nmap <expr> <buffer> <F5> empty(glob(expand("%:p:r").".log")) ? ":echo expand(\"%:p:r\").'.log does not exist!'<CR>" : ":split \| view %:p:r.log<CR>"

Also, please note I had used a pipe of wrong sequence of commands, that is :view \| split %:p:r.log<CR> instead of :split \| view %:p:r.log<CR> and the main file was changed to read-only mode instead of the log.

Your Answer

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

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