As the title stated

autocmd BufRead,BufNewFile not-py-file nnoremap <buffer> <F3> :call some_func<CR>

is what I want to acheive, but I don't know how to implement that not-py-file logic. Any help?

  • Here py-file mean filename ends with .py.
    – qeatzy
    May 25, 2018 at 13:24

1 Answer 1


One way you could achieve this is to create the mapping for all files, and then remove it for *.py files:

autocmd BufRead,BufNewFile * nnoremap <buffer> <F3> :call some_func<CR>
autocmd BufRead,BufNewFile *.py nunmap <buffer> <F3>

Vim executes autocommands in the order they are created (See :help :autocmd), so the two commands will always run in the correct order.

If that seems inelegant to you, you could instead have your autocommand check the filetype before it creates the mapping:

function! CreateNonPyMapping(extension)
  if a:extension !=? 'py'
    nnoremap <buffer> <F3> :call some_func<CR>

augroup NonPyMapping
  autocmd BufRead,BufNewFile * call CreateNonPyMapping(expand('<afile>:e'))
augroup END
  • Many thx, saved my time. The first solution is quite decent, and second one more extensible and might be preferred if .vimrc sourced more than once.
    – qeatzy
    May 25, 2018 at 14:14
  • 1
    what about au BufRead,BufNewFile *.*\(.py\)\@<! ...?
    – Mass
    May 27, 2018 at 17:16
  • @Mass I had no idea you could do that! Looking at :help file-pattern I realise I had always glossed over: \ special meaning like in a pattern. Does that mean you can use literally any regular expression element that begins with a \ in a file pattern?!
    – Rich
    May 29, 2018 at 8:36
  • 1
    Just about- internally vim uses glob2regpat() on the pattern which turns . into \., * into .*, among other things, but then it's used as a regular regex. Most pattern elements like \@<! simply pass through; you just have to be careful of the translation. On the other hand, I'm not sure which approach is actually faster and yours is certainly less difficult to understand.
    – Mass
    May 29, 2018 at 12:13

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.