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
    Jun 26 at 23:53
  • 1
    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
    Jun 27 at 17:15

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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