0

I've recently installed the VimWiki plug-in, and am learning about Vim's plugin architecture in general (and better using directories like after/ftplugin instead of cramming everything into my .vimrc file).

I would like to call a function prior to writing wiki files, like so:

autocmd BufWrite *.wiki call CleanMarkdown()

However, vimwiki sets its own BufWrite autocommand, which updates any tables-of-contents in the wiki file. I could clobber this autocommand with my own function that calls both the CleanMarkdown() plus whatever vimwiki is doing today, but that would be brittle in the face of possible future changes in the vimwiki plugin.

Is there a standard way to add to the list of things to do for a BufWrite autocommand?

Steve Hollasch
  • 2,011
  • 1
  • 20
  • 18
  • 2
    There is no rule barring multiple commands to be executed on a certain event. Actually, it's designed to do so. Have you tried using it? Did you encounter any problem? Edit: by the way, the help for `:au` explicitly says what you are asking for: it _adds {cmd} to the list of commands to be executed when {event} happens_ – sidyll Sep 24 '18 at 21:38
  • Thanks sidyll. I was unable to find any documentation that this was ok, and noticed that the autocmd from vimwiki had clobbered mine. Given your hint, I just added the autocmd to `after/ftplugin/vimwiki.vim`, without using the exclamation point. Sure enough, it _added_ my command to the event, and both are executed. Thanks for helping me solve this! – Steve Hollasch Sep 24 '18 at 21:42
  • Ah, of course, I skipped that part. If you use the `!` it will remove other commands before adding the described one. I'm sorry, I didn't remember about this possibility. – sidyll Sep 24 '18 at 21:51

1 Answers1

1

Multiplicity of autocmds

There can be many :autocmds for any event; the command is cummulative. The corresponding :autocmd! removes certain sets of commands (depending on the arguments given to it).

If you don't specify a [group], the autocmd will be defined in the global space, and there's a high risk of getting this cleared by some :autocmd!. Therefore, it is recommended to specify a [group] (especially in plugins). With this, you avoid that another (mis-behaving) plugin or customization clobbers your autocmd.

Integrating with vimwiki plugin

As the plugin already defines its own filetype, you don't need to duplicate the filetype detection logic, i.e. the *.wiki pattern. Instead, if you put your :autocmd definition in ~/.vim/after/ftplugin/vimwiki.vim, you can use the <buffer> special pattern to make this autocmd apply only to the current (VimWiki) buffer.

augroup MyVimWikiCleanup
autocmd BufWrite <buffer> call CleanMarkdown()
augroup END

Ordering

The :autocmds are executed in the order in which they were defined. By using the after directory, yours will be executed after the plugin's.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Looks like `autocmd BufNewFile ...` doesn't work. Does BufNewFile require `*.wiki`? – Steve Hollasch Sep 26 '18 at 18:26
  • You don't need an autocmd for that; just put the commands directly in the `ftplugin` script; that itself gets sourced by the `Filetype` event triggered by the `BufNewFile` of filetype detection. – Ingo Karkat Sep 26 '18 at 18:55