3

I know that the buffer is the window you're currently working in (or at least that's what I think it is), but why should I use buffer when scripting :autocmd?

For example:

autocmd FileType cpp,javascript,java,php :iabbrev <buffer> iff if ()<left>  

will turn 'iff' into if () for a few file types.

I've tested it without buffer too and it still works. Is there any reason why I should include buffer?

  • the point is, do you want those abbreviations to be available globally to all buffers you edit or perhaps only to certain buffers of a particular filetype? – Christian Brabandt Feb 27 '17 at 15:07
  • But shouldn't vim know that I only want the abbreviation to work for certain filetypes since I used FileType? If I didn't put <buffer> would the abbreviation still be global, even with the FileTypes put in? – Matt Corby Feb 27 '17 at 15:11
  • 1
    Well, it is like executing a command only on the FileType autocommand, but afterwards it will affect every buffer, not just the one where the FileType autocommand fired. The :iabbrev command must have a way to know, whether the new abbreviation will be globally valid or only for your current buffer. – Christian Brabandt Feb 27 '17 at 15:43
1

The buffer is the what you're currently editing. It could be file, or not yet.

A window is the where it's edited.

Then, <buffer> is mandatory for your mappings/abbreviations, command (with -buffer this time), menus (with a plugin) to exist only in the context of your current buffer. It's not directly related to autocommands. autocommands only define the when the definition is done.

For instance, you certainly don't want your if abbreviation to be expanded in the same fashion in C++, Python, Pascal, or in vim scripts language.

PS: your abbreviation suffer of several issues:

  • it'll also be expanded in string or in comments
  • it won't play well with bracketing plugins as it isn't nore
  • it'll insert a trailing white space (search for eatchar, in Vim context)

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.