スタック・オーバーフローに参加する
684万人以上のプログラマーが集まるスタック・オーバーフローに参加しませんか?
簡単な登録後、すぐにご利用いただけます。
登録

I’ve got a lot of plugins enabled when using Vim – I have collected plugins over the years. I’m a bit fed up with how long Vim takes to start now, so I’d like to profile its startup and see which of the many plugins I have are responsible.

Is there any way to profile Vim’s startup or script-running? Ideally I’d like to know how long Vim spends in each Vim script it loads.

share|improve this question

10 Answers 10

up vote 122 down vote accepted

If you're using Vim 7.2.269 or later, then there's the --startuptime option you can use.

vim --startuptime vim.log

from the help (vim -h):

--startuptime <file> Write startup timing messages to <file>
share|improve this answer
4  
And as of patch 7.2.286, there's no equal sign needed. "vim --startuptime vim.log" – jamessan Nov 11 '09 at 19:17
16  
if you want it to just print it, try vim --startuptime /dev/stdout +qall – Capi Etheriel Jan 15 '13 at 17:37
    
@barraponto There's also time vim +q if you only want to time vim's startup as a whole. – Braden Best Jan 8 '16 at 21:30

You can use vim own profiling mechanism:

vim --cmd 'profile start profile.log' \
    --cmd 'profile func *' \
    --cmd 'profile file *' \
    -c 'profdel func *' \
    -c 'profdel file *' \
    -c 'qa!'

After running the above you will find a file called profile.log in the current directory with all required information. To get per-script information table similar to already present per-function one, use (after opening this file in vim):

" Open profile.log file in vim first
let timings=[]                      
g/^SCRIPT/call add(timings, [getline('.')[len('SCRIPT  '):], matchstr(getline(line('.')+1), '^Sourced \zs\d\+')]+map(getline(line('.')+2, line('.')+3), 'matchstr(v:val, ''\d\+\.\d\+$'')'))
enew                            
call setline('.', ['count total (s)   self (s)  script']+map(copy(timings), 'printf("%5u %9s   %8s  %s", v:val[1], v:val[2], v:val[3], v:val[0])'))

It will be unsorted, but you can always use built-in :sort command if number of scripts is too large.

share|improve this answer
    
I didn't realise vim had a profiling command, thanks for pointing this out. – Benj Dec 2 '11 at 10:08
    
@Benj It can be disabled. According to the doc you either require vim with huge set of features or self-compiled one where you explicitly enabled +profile without enabling this set. – ZyX Dec 2 '11 at 17:19
1  
Would +3 this if I could. It helped me track down a check in dbext.vim, which was taking over three seconds github.com/johnsyweb/dotfiles/commit/09c3001 – Johnsyweb May 30 '13 at 13:23
    
@ZyX, How can I do this in windows shell (gvim)? It doesn't work in windows gvim. I inserted this command in windows shell gvim --cmd 'profile start profile.log' --cmd 'profile func *' --cmd 'profile file *' -c 'profdel func *' -c 'profdel file *' -c 'qa!' It does create a lot of empty files in vim. – Reman Aug 15 '13 at 14:42
    
@Remonn Use double quotes. Or bash from cygwin. – ZyX Aug 17 '13 at 16:29

You could run vim -V, pipe the output through a utility that adds timestamps and analyze the output. This command lines does this, e.g.:

vim -V 2>&1 | perl -MTime::HiRes=time -ne 'print time, ": ", $_' | tee vilog

You might have to blindly type :q to get back to your prompt. Afterwards, you should find the file vilog in your current directory with hires timestamps at the beginning of each line.

If you can do with a granularity of a second, you can do this:

vim -V 2>&1 | perl -ne 'print time, ": ", $_' | tee vilog
share|improve this answer
1  
Fantastic, what a great solution. – Benj Nov 6 '09 at 15:59
3  
Did you know the "perl -n" does the while (<>) {} for you. – Benj Nov 6 '09 at 16:07
1  
Now that you mention it: yes, I did. I'm going to edit the answer to get shorter commands. Thanks. – innaM Nov 6 '09 at 16:14

I refined the vim -V solution by Manni to show the delta time:

vim -V 2>&1 | perl -MTime::HiRes=time -ne '$a = time unless defined $a; print time - $a, ": ", $_' | tee vilog
share|improve this answer
1  
Sweet! I took the liberty to shorten this a bit and make it more "Perlish". – innaM Nov 6 '09 at 16:23
    
Cheers Manni, Good call ;-) – Benj Nov 6 '09 at 16:28

I created this Github project in order to better answer your question. Basically, it sums up the timing for each function calls for every plugins, which is not obvious (but important) from the raw vim profile output.

You will get a result figure like this:

vim-plugins-profile figure

Along with text output like this:

Generating vim startup profile...    
Parsing vim startup profile...     
Crunching data and generating profile plot ...    

Your plugins startup profile graph is saved     
as `profile.png` under current directory.    

==========================================    
Top 10 Plugins That Slows Down Vim Startup    
==========================================    
   1    105.13  "vim-colorschemes"    
   2    42.661  "vim-easytags"    
   3    31.173  "vim-vendetta"    
   4    22.02   "syntastic"    
   5    13.362  "vim-online-thesaurus"    
   6    7.888   "vim-easymotion"    
   7    6.931   "vim-airline"    
   8    6.608   "YankRing.vim"    
   9    5.266   "nerdcommenter"    
  10    5.017   "delimitMate"    
==========================================    
Done!    
share|improve this answer
    
I could not add figures due to low reputation. You can add in the figure by simply adding a ! before the tag. – hyiltiz Oct 18 '15 at 3:35
    
Thank you so much @BenC – hyiltiz Oct 18 '15 at 22:20
    
+1 This is pretty cool ;-) My question is 6 years old now (hard to believe) so I hope you were doing this more for your benefit than mine. Still, I'm sure it'll be useful for other viewers of the question, which has been surprisingly popular. – Benj Oct 19 '15 at 14:54
1  
@Benj Yes, I was trying to do the profiling myself, then found your question. I was not satisfied with the answers and just did some improvements. I believe 6 years changes the trend a bit -- it is very convenient to get candy graphs! – hyiltiz Oct 19 '15 at 17:12

Based on the work done by @hyiltiz that depends on R, I made a Python version of the profiler, since this is more often available on a system that R.

It's also slightly easier to extend, thus the features are:

  • Automatic detection of the plugin folder,
  • Bar plot thanks to matplotlib,
  • Run the analysis over several executions to get the average/standard deviation,
  • Supports both vim and neovim,
  • Can be used with a full vim command to test lazy-loading features, opening a file with a specific filetype etc.,
  • Export result to a csv file.

The output is similar to what vim-plugins-profile provides:

$ vim-profiler.py -p nvim

Running nvim to generate startup logs... done.
Loading and processing logs... done.
Plugin directory: /home/user/.config/nvim/plugged
=====================================
Top 10 plugins slowing nvim's startup
=====================================
1         3.326   vim-fugitive
2         2.936   tcomment_vim
3         2.315   vim-hybrid
4         1.751   lightline.vim
5         0.959   vim-sneak
6         0.943   supertab
7         0.542   vim-surround
8         0.536   fzf.vim
9         0.450   fzf
10        0.434   auto-pairs
=====================================

vim-profiler

share|improve this answer

If you're loading your plugins from a .vimrc file, what you could do is put a q on some line part way through the file to make it quit so you can use a process timer, like the unix time command. More thoroughly, this would look like:

  1. backup existing .vimrc file
  2. comment out all but a select number of plugins
  3. insert a q line
  4. call time vim repeteadly and average
  5. restore backup

This is not elegant but I think it will get the job done.

share|improve this answer
    
Hmm, not bad in a pinch. I've already got my vimrc split into lots of seperate files so shouldn't be too hard to automate. – Benj Nov 6 '09 at 14:21

There is a plugin to profile the vim start-up time.

http://www.vim.org/scripts/script.php?script_id=2915

share|improve this answer

Isn't there a bash time command that can be used like so:

time vim

EDIT: Doesn't include the scripts start up time. Use @jamessan suggestion instead.

share|improve this answer
    
Yes, there is but that would only tell you how long vim took to both open and close, not how long it took to parse each script. – Benj May 8 '12 at 22:09
    
You're right. Forgot about that when posting this. My bad. – roher4 May 9 '12 at 1:45

It can be convenient to trace the --startime when opening a particular file

gvim app/views/layouts/application.html.erb --startuptime time.log
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.