I am aware that you can either use an Autocommand or defining a key-binding in some ftplugin file.

However, once the key-binding is defined, it will stay, no matter the filetype.

However, imagine that I want the mapping nnoremap <F12> <cmd>echo "Hello" be valid only for python filetypes, how shall I do?

1 Answer 1


how shall I do?

You shall read the relevant documentation, :help :map-buffer:

If the first argument to one of these commands is "<buffer>" the mapping will
be effective in the current buffer only.  Example:
        :map <buffer>  ,w  /[.,;]<CR>
Then you can map ",w" to something else in another buffer:
        :map <buffer>  ,w  /[#&!]<CR>

"But how do I get there if I don't know what to search for?" you might ask. Well, you get there by:

  1. figuring out that you need help with mapping,
  2. asking Vim for help with :help mapping,
  3. and looking around. The relevant section is only 200 lines in.

That is, if you missed the "ADDITIONALLY..." section in :help usr_40.

The solution, in short…

  1. Create ftplugin/python.vim (or after/ftplugin/python.vim if you want it to be sourced after the built-in one).
  2. Add nnoremap <buffer> <F12> <cmd>echo "Hello"<CR> to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.