I'm trying to write a snippet of VimL to allow the user to toggle the hilighting of unwanted trailing whitespace with a hotkey. (This is my first-ever Vim script, beyond copy-pasting things into my .vimrc
, so … grain of salt :P)
I wish for the ‘are we currently hilighting trailing whitespace?’ to be buffer-specific state; but I'm having a lot of trouble figuring out how autocommands interact with buffers.
For instance, here's my first stab at an augroup
of buffer-local autocmd
s:
augroup ExtraWhitespace
au!
au BufEnter <buffer=abuf> match ExtraWhitespace /\s\+$/
au InsertEnter <buffer=abuf> match ExtraWhitespace /\s\+\%#\@<!$/
au InsertLeave <buffer=abuf> match ExtraWhiteSpace /\s\+$/
augroup END
… unfortunately, this immediately trips up when invoked:
Error detected while processing function ToggleExtraWhitespace:
line 19:
E680: <buffer=0>: invalid buffer number
line 20:
E680: <buffer=0>: invalid buffer number
line 21:
E680: <buffer=0>: invalid buffer number
No matching autocommands
I don't understand why <abuf>
is 0
, when bufnr('%')
is 1
, or how to get the autocommands to execute for buffer 1
instead. (Of course 0
is invalid!)
For the moment, I've swapped out <buffer=abuf>
for *
; but this screws up the functionality of this function when there are multiple buffers loaded, and That Is Bad. So, any help figuring this out is welcome. /=
<buffer=abuf>
? From the snippets you should only need<buffer>
. Also how are you loading theses? Is it being loaded during your vimrc? – Tractable