How do you use buffer-local autocommands in VimScript?

I am trying to write a VimL snippet so that the user can toggle the checkbox of unwanted trailing spaces with a hotkey. (This is my first ever Vim script, except it copies things into mine .vimrc

, so ... a grain of salt: P)

I want us to currently find hilighting trailing whitespace? be a specific buffer; but I'm having a lot of trouble figuring out how autocommands interact with buffers.

For example, here's my first hit at augroup

buffer-local autocmd


augroup  ExtraWhitespace
   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 fires immediately when called:

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 , when - , or how to get the autocommands to execute on the buffer . (Of course not true!)<abuf>






At this point I have changed <buffer=abuf>

for *

; but it screwed up the functionality of this function when loading multiple buffers, which is bad. As such, any help in determining this is appreciated. /=


source to share

1 answer

First, I don't know how it works <buffer=abuf>

. The documentation seems to be inconsistent for him.
It looks like the behavior for <buffer=abuf>

was changed / fixed with patch 7.4.637 before it caused problems even when used correctly. <buffer=abuf>

should only be used when starting autocmd. So your function would probably work if you called it in VimEnter or BufAdd.

The following modified version of what you were trying to use without using <buffer=abuf>

  augroup ExtraWhitespace
     autocmd! * <buffer>
     autocmd BufEnter    <buffer> match ExtraWhitespace /\s\+$/
     autocmd InsertEnter <buffer> match ExtraWhitespace /\s\+\%#\@<!$/
     autocmd InsertLeave <buffer> match ExtraWhitespace /\s\+$/
  augroup END


The first thing you should notice is what has au!

been replaced by autocmd! * <buffer>

. au!

shouldn't be there, as this will remove all autocmd in the ExtraWhitespace group from all buffers. This means that you can only define it in one buffer. ( autocmd! * <buffer>

only removes autocmds in the current buffer)

Second, you must notice what is being used <buffer>

. This means that autocmd will only be created for the current buffer when the function is called. Local buffer autostart must be called for every buffer you want to define.

Other great comments

You have

fun! HighlightExtraWhitespace()
   if exists('b:ews') && b:ews == 1
     "echom "-- Adding ExtraWhitespace hilighting"
      highlight ExtraWhitespace ctermbg=red guibg=red
     "echom "-- Removing ExtraWhitespace hilighting"
      highlight clear ExtraWhitespace
au ColorScheme * call HighlightExtraWhitespace()


The selection is global, so clearing it in one buffer will delete the entire selection group. So it's best to just leave the selection in place and redefine it every time the color scheme changes.

autocmd ColorScheme * highlight ExtraWhitespace ctermbg=red guibg=red


It is recommended to use the long form of command names in scripts. (The short form is for input only). The long form is more readable and easily identifiable, so it au

will be autocmd




All Articles