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?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityOne 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>
endif
endfunction
augroup NonPyMapping
autocmd!
autocmd BufRead,BufNewFile * call CreateNonPyMapping(expand('<afile>:e'))
augroup END
.vimrc
sourced more than once.
: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?!
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.
py-file
mean filename ends with.py
.