4

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>
2

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.

  • Oh, I see. Certainly, without the buffer argument, if there are multiple buffers and one of them is a Python file, the map will work on all of them regardless of their type. Thanks. – Dorsal6 Jun 23 '17 at 18:21

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.