3

My Vim configuration file is getting bigger (15KB+). And I try not to make my vim launch slower by sourcing more (larger) files at startup. For the same purpose I use only essential plugins and I try to keep the number of plugin as less as possible.

So, somewhere in my .vimrc file, I have these lines:

autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4
autocmd FileType python setlocal textwidth=78
autocmd FileType python match ErrorMsg '\%>80v.\+'
autocmd FileType python inoremap <F5> <esc>:upd\|!python %<cr>
autocmd FileType python nnoremap <F5> :upd\|!python %<cr>
autocmd FileType python nnoremap <leader>8 :w\|call Flake8()<cr>
autocmd FileType python setlocal formatoptions-=t
autocmd BufWritePost *.py call Flake8()

Now I see in first 7 lines, all lines have autocmd FileType python in common. So my thinking is, if we manage to replace all those word with something less then Vim will fire up faster. But I don't know how to do that.

Can we group them? How? Anything else?

Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
  • Mandatory troll: `:%d` cleans your vimrc up the best. Alternatively, `:new|saveas! $MYVIMRC` is quite effective – sehe Aug 27 '12 at 09:49
  • 1
    Also, slightly less trolling, [`:help autocmd-groups`](http://vimdoc.sourceforge.net/htmldoc/autocmd.html#autocmd-groups) – sehe Aug 27 '12 at 09:52

4 Answers4

5

Just put

setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=78 formatoptions-=t
match ErrorMsg '\%>80v.\+'
inoremap <F5> <esc>:upd\|!python %<cr>
nnoremap <F5> :upd\|!python %<cr>
nnoremap <leader>8 :w\|call Flake8()<cr>

in ~/.vim/after/ftplugin/python.vim.

Vim will read this file only when you open a Python file.

I've been meaning to do this for some time, actually. And for the same reasons.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • 4
    This shadows the default `ftplugin/python.vim`, and it only works (i.e. doesn't prevent the default settings) because it doesn't `:let b:did_ftplugin = 1`. I find it clearer to put this into `~/.vim/after/ftplugin/python.vim`; that's what the after directories are for. – Ingo Karkat Aug 27 '12 at 09:55
  • @IngoKarkat, you are right. I've updated my answer with `/after`. Thank you. – romainl Aug 27 '12 at 10:16
4

Filetype-specific stuff should be moved to ~/.vim/after/ftplugin/{filetype}.vim, as romainl has already pointed out.

I moved all my custom mappings and commands into ~/.vim/plugin/my{mappings,commands}.vim, and mostly only put real settings (the :set commands) and plugin customizations into .vimrc. Any mappings / commands that aren't simple one-liners and delegate to functions then use the autoload mechanism. This keeps the amount of stuff read at startup small.

TL,DR: Autoload is great; all plugins should use it.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
2

I am using a scheme where a group of options is set in a function that is called by an auto command.

function! s:C_options()
    setlocal cinoptions={0,:1s,g1s,t0,(0,=.5s
    setlocal noautoindent
    setlocal nosmartindent
    setlocal cindent
    call s:PROG_options()
endfunction

autocmd BufRead,BufNewFile *.c call s:C_options()

It is quite similar to using a filetype but you can use nested call, like having general programming options in s:PROG_options(), so you should be able to reduce further the size of your .vimrc.

The after\filetypesolution might be more efficient regarding initial load time, but I would rather have most of my customization in a single .vimrc than scattered in the .vimdirectory.

Xavier T.
  • 40,509
  • 10
  • 68
  • 97
1

At least you can merge the first two lines with the 7th

autocmd FileType python setlocal expandtab shiftwidth=4 tabstop=4 softtabstop=4 textwidth=78 formatoptions-=t
autocmd FileType python match ErrorMsg '\%>80v.\+'
autocmd FileType python inoremap <F5> <esc>:upd\|!python %<cr>
autocmd FileType python nnoremap <F5> :upd\|!python %<cr>
autocmd FileType python nnoremap <leader>8 :w\|call Flake8()<cr>
autocmd BufWritePost *.py call Flake8()

But I can't imagine how you can get rid of the others. On the other hand I don't think that these autocmd commands are taking so long to execute.

apparat
  • 1,930
  • 2
  • 21
  • 34