What is <buffer> used for on vim mapping?
From the :help command, in the key mapping section:
If the first argument to one of these commands is
<buffer>
the mapping will be effective in the current buffer only.
I don't understand this definition. Aren't all commands applied to the buffer you're working on? It also mentions that you could use this <buffer>
to map the same key combination to different commands on different buffers. From that alone I'd think you'd use it if you wanted to map something while you're working with a file, so it doesn't affect other buffers? I'm confused.
To provide a concrete example of where I'm coming from, the following is a mapping to run a python script with F9, which I found on Stack Overflow. I'm questioning what the use of <buffer>
is, since the mapping seemed to work just fine without it:
autocmd FileType python nnoremap <buffer> <F9> :!clear;python %<cr>
It means the mapping only exists in the buffer where it was defined. If you switch to another buffer (file) the mapping does not work (exist) there.
One example of why this is useful is for mappings that are only useful for specific filetypes (in this example, Python files).
Usually these mappings are defined in a filetype plugin
that gets sourced each time a new buffer of that file type is loaded.