I have a lot of lines with with filetype-specific autocommands defining mappings and abbreviations, which looks like this:

augroup vimrc
au FileType blah nnoremap <buffer> aa bb
au FileType blah nnoremap <buffer> cc dd
au FileType blah inoreabbrev <buffer> www xxx
au FileType blah inoreabbrev <buffer> zzz yyy
augroup END


I'm wondering if it would be more efficient to assign one function for each file type, and declare all mappings/abbrevs there. I'm asking purely from performance standpoint, not code style.

  • 1
    Potential answer writers: please do not speculate! Performance comparisons should be done with actual data. See also questions to clarify when talking about performance – D. Ben Knoble Oct 7 '19 at 2:21
  • 1
    <buffer> should be written after the mapping and abbreviation commands, not before. If performance is a concern, the best thing to do is to move all the mappings/abbreviations in a filetype plugin. See: reddit.com/r/vim/wiki/… – user938271 Oct 7 '19 at 5:00
  • 1
    There is already one autocmd which sources the filetype plugins: github.com/vim/vim/blob/… This single autocmd is more efficient than several ad hoc filetype autocmds in the vimrc, simply because for all the settings to be sourced, the filetype needs to be checked only once, whereas with several autocmds it needs to be checked again and again for no good reason. If a filetype plugin is not possible because you need to keep all your config in a single file, then use a function to avoid the repetition of filetype checks. – user938271 Oct 7 '19 at 5:00
  • 1
    purely from performance standpoint No difference at all. – Matt Oct 7 '19 at 5:33
  • 1
    @user938271 Filetype plugin makes much sense and, of course, should be recommended. But it's only profitable if there's no FileType blah opened in the current session. Otherwise, it's a bit slower (an additional file read operation). – Matt Oct 7 '19 at 5:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.