My vimwiki plugin has Enter key bound to the function to create a "hyper"-link to another file (and if it already is a link then open the linked file to view/edit).

I want to be able to jump back and forth between wiki files and files in /tmp/wiki-tag-reports (using a vimscript function I managed to write thanks to help from here) and when I have a file from /tmp/wiki-tag-reports/some-tag-report.mkd open the Enter key should trigger that vimscript function instead of the vimwiki's usual enter-key function.

Is this a simple thing to do?

I don't know how to approach such a "conditional" loading of vimscript. Currently/previously I was using two separate vimrc's but that doesn't work in this switching-back-and-forth way. There must be some way using just a single vimrc and having some logic in it to check for the file path...? But maybe something else is required? Tips? Suggestions?

I thought I might be able to achieve this via:

    " note that DiaryTagBrowser() is defined earlier in the vimrc file
    autocmd BufRead,BufNewFile /tmp/wiki-tag-reports/*.mkd
      \ command! DiaryTagBrowser : call DiaryTagBrowser()
      \ nnoremap <Enter> :DiaryTagBrowser <Enter>

but hitting the Enter key in the tag report still invokes the vimwiki hyperlink function thing.

Is there a problem in this vimscript? Or maybe vimwiki plugin has bound the Enter key in a way that somehow takes precedence over this(and if that's the case then what to do about it?)?

  • there are multiple points here which I've now resolved... will post back to "answer" or close this question
    – alec
    Commented Jun 26, 2021 at 23:53
  • 2
    See :help map-precedence for precedence of mappings. Local mappings take precedence. I'd expect Vim wiki's mapping would be a local mapping, but it seems that might not be the case, given your answer of using nnoremap <Enter> directly in an autocmd fixing it... (?) You can check for that using :nmap <Enter> to query the mappings to see how they're defined.
    – filbranden
    Commented Jun 27, 2021 at 17:15

2 Answers 2


If you're trying to execute multiple commands in autocmd, make sure they're separated by | like below

" This works in vimrc
au VimEnter * echom "Hi " |
    \ echom " Bye"

" This does not work, vim complains about "undefined variable echom"
au VimEnter * echom "Hi "
    \ echom " Bye"

And you can avoid multi-line autocmds by using a function, like

" This works well with plugins, like vim-markdown and tagbar shown below
function! MapF1()
  " Check the file type with `&ft`
  if &ft == 'markdown'
    nnoremap <silent> <F1> :TocToggle<CR>
  elseif &ft != 'qf'
    nnoremap <silent> <F1> :TagbarToggle<CR>
autocmd BufEnter * call MapF1()
  • 2
    It's better to use <buffer> for filetype-specific mappings.
    – D. Ben Knoble
    Commented Sep 16, 2023 at 15:26

autocmd BufRead /path/to/file*.type is indeed the correct logic... but there are two problems here.

First, is the syntax... the lines starting with \ are odd... entering the full command like autocmd BufRead,BufNewFile /tmp/wiki-tag-reports/*.mkd nnoremap <Enter> :DiaryTagBrowser <Enter> works correctly.

Second problem, and more to the point of the question... is that plugins are invoked after the vimrc file. As How can I redefine plugin key mappings? describes overriding a plugin requires putting code in ~/.vim/after/plugin/mystuff.vim or using autocmd VimEnter (the VimEnter event).

However my attempt at the using the "after" method didn't work, and then I realized that I actually need my desired keybindings to be activated conditionally based on some kind of variable, and I'm not sure what options are available or how to implement that, so this question feels a bit dead-ended.

  • I personally work around the issues by letting autocmd call a function. In that function I can define different mappings based on the value of &ft and @% etc. Works fine with plugins including coqtail and easymotion.
    – Hoblovski
    Commented Mar 20, 2023 at 6:05
  • I guess that is a bad idea to global remap <Enter>. au BufRead,BufNewFile *.mkd nnoremap <buffer> <Enter> :DiaryTagBrowser<Enter> looks better because it localize mapping to a current <buffer>
    – Alex Kroll
    Commented Apr 19, 2023 at 21:31

Your Answer

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

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