Frage

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?

War es hilfreich?

Lösung

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.

Andere Tipps

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.

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.

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top