Vim verbosefile with timestamps - vim

I have a number of very long and complex Vim configuration files, but these seem to be causing some unpredictable performance slowdowns.
Is there a way of producing a logfile of Vim's internal activity (in the style of verbosefile) which also includes timestamps for each function call to allow me to debug these issues. Alternatively, are there any other tools to help me in this task?

One thing you can try is invoking vim with --startuptime some_file. This will dump the steps performed during startup, along with timestamps. Try :help --startuptime for some more information.
Unfortunately, this shows you timestamps for files, not functions. If you want to see how long a specific function takes, you could put a call to it in a file under the plugin directory, so it's executed on boot. This won't be possible in all cases and it won't give you a lot of granularity, but it might help you narrow down the problem. Someone else might have a better idea, but I'd start by trying something like this.
EDIT:
I just found two things that may help with this problem. One of them is reltime(). Here's an example from vim's help:
let start = reltime()
call MyFunction()
echo reltimestr(reltime(start))
All it does is show the time a function takes to execute. Combined with, for example, Decho, this could provide a simple log of said functions.
The second one should be a lot more powerfule, the :profile command. It lets you profile a piece of vimscript and place the results in a file. :help profile should tell you all you need to know. Unfortunately, it's only available with the +profile feature, which seems to be enabled only in the "huge" feature set. If your vim doesn't have it, you'll need to recompile. If that's a problem, or you don't want to bother, you might be able to achieve good results with reltime alone.

If --startuptime does not fit your needs, you may use the following command:
vim --cmd 'profile start profile.log' \
--cmd 'profile func *' \
--cmd 'profile file *' \
-c 'profile pause' \
-c 'qa!'
(replace --cmd with -c and add -c 'Command that causes slowdown' if problem occurs not at startup). You will get all timings in the profile.log. Table with function timings will be present at the end of the file, to get per-script timings, use the following
script:
" 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])'))
This will open a new file containing just the same table as at the end of profile.log, but 1) for scripts, not for functions, 2) unsorted.

Better late than never? I was searching to see if I could get vim to include some timestamps in the &verbosefile, as it causes vim to append to the file with no indication of when the appending started.
I adapted the solution from ZyX's answer here and after trying some things out, found that a simple echo "foo\n" will append foo and a newline to the &verbosefile:
let s:VerboseLS = ' **** V'.'E'.'RB'.'OSE LO'.'GG'.'ING S'.'TA'.'RTED ****'
let s:VerboseLE = ' **** V'.'E'.'RB'.'OSE LO'.'GG'.'ING E'.'ND'.'ED ****'
function! ToggleVerbose()
let l:timestamp = strftime("%c")
if !&verbose
let l:msg = "\n\n" . l:timestamp . s:VerboseLS . "\n\n"
set verbosefile=~/.vim/log/verbose.log
set verbose=100
echo l:msg
else
let l:msg = "\n" . l:timestamp . s:VerboseLE . "\n"
echo l:msg
set verbose=0
set verbosefile=
endif
endfunction
nnoremap <silent> <Leader>vl :call ToggleVerbose()<CR>
Now I get nice banners in my log:
Sun Feb 17 14:18:02 2013 **** VERBOSE LOGGING STARTED ****
...[snip]...
Sun Feb 17 14:18:08 2013 **** VERBOSE LOGGING ENDED ****
I defined those banners the way I did so that if I source that script file while verbose logging is on so if I search the log for "VERBOSE LOGGING" or similar I won't match on them.
So, you could adapt this method to use reltime if your vim has('reltime') for sub-second precision and maybe make a timing wrapper to call that echos the start/end times for function calls (although you'd have to refactor when you call things inline without the call command).
Ooh, I just had an idea... I wonder if &verbosefile can be set to a named pipe that goes to util that prepends time stamps... Let's see...

Related

Vim: cannot see :echomsg messages in message history list

I'm working on a vim script and want to see the value of a variable that is generated in the script for debugging purposes.
I use something like:
echomsg 'My variable = ' . b:variable
reload the source with
:source %
and then look the message history list with
:messages
But there's nothing there and I'm pretty sure that the command would be executed.
What am I doing wrong here?
EDIT:
I'm trying to make a change to vim-cucumber and so I wanted to see the value of a variable. In the ftplugin/cucumber.vim file, I have the following:
let b:cucumber_root = expand('%:p:h:s?.*[\/]\%(features\|stories\)/step_definitions/mobile_website\zs[\/].*??')
echomsg 'cucumber_root = ' . b:cucumber_root
If you look at the top of file you will find
if (exists("b:did_ftplugin"))
finish
endif
Which stops the file from being sourced again after the first time. You need to unlet b:did_ftplugin before sourcing the file again.
Other options would be to use another tpope plugin to do this for you called scripttease with the :Runtime command.

Can I call multiple function on Vim startup?

I frequently send files to Vim from Visual Studio. I have it set up as an external tool with the following parameter:
"+call cursor($(CurLine), $(CurCol))"
However, I also want to be able to call my own function as well. When I'm editing a file from VS I want the window to be large, so I expected to be able to do something like this:
"+call cursor($(CurLine), $(CurCol)); +call Embiggen()"
However, that doesn't work. I've tried a few variations (e.g. , call Embiggen(), etc).
Obviously I could write my own PlaceCursorAndEmbiggen function, but I don't really want to do that. Is there any way to call multiple functions on Vim startup?
Eureka!
Simply pass two strings:
"+call cursor($(CurLine), $(CurCol));" "+call Embiggen()"
Maybe the solution would have been easier to find had you used the alternative, more commonplace syntax: -c "cmd" instead of "+cmd". According to :help -c, you can pass up to 10 of these.
These exact commands can be combined into one using pipe symbol:
"+call cursor($(CurLine), $(CurCol)|call Embiggen()"
. There are much more that can be combined this way, but some like :normal can’t, use #Ingo Karkat’s or your own answer for them. If you are short* on +commands and still don’t want to create a .vim file you can use either :execute
vim -c "execute 'normal! 1' | execute 'normal! 2'"
or (bash/zsh) -S with process substitution:
vim -S <(echo '
normal! 1
normal! 2
')
. Though most of time it is better to just create a .vim file.
* You can pass up to 10 + or -c (they are equivalent and they are not counted separately) and 10 other --cmd, though letter is less useful.

Is it possible to access vim's command-line arguments in vimscript?

I found the answer to this question while writing it, so I've broadened it a little. I wanted to access the --servername argument, in order to create dynamic settings in my .vimrc file.
Through vim's help, I found the v:servername variable, and my script is working. However, now I'm curious if it's possible to access any arbitrary command-line argument. For example, if I wanted to know if vim was in Lisp mode (-l) or Debugging mode (-D), how would I do it? There seems to be no corresponding v: variable for them.
Here are the variables I found by autocompleting :help v:<Tab>
Is there a general way to access command-line arguments from vimscript?
Strangely, I think the answer may be "No, there is no direct way to access startup options specified on the command line".
The :args command and argv() can be used to access the filename(s) specified on startup, but that's not what you want.
I see on Vim's forums that someone offered this solution to get the startup command line on Linux:
:exe '!tr "\0" " " </proc/' . getpid() . '/cmdline'
I assume there's analogous command on Windows. . . .
You can look over that forum thread here:
http://groups.google.com/group/vim_use/browse_thread/thread/43773f27cdc10265/ad17ae8180c0fb6e?show_docid=ad17ae8180c0fb6e
My googling indicates that this feature has been proposed but never implemented. However I did come up with a bit of a kludge that nevertheless works:
:echo split( system( "ps -o command= -p " . getpid() ) )
# => [ 'vim', ... arguments ... ]
(Tested on OS X Lion.)
The getpid() function gets Vim's PID, then we call ps externally with options to return nothing but the "command" value for process, then use split() to split the command into a list.

VIM: dynamic runtimepath per module.vim and slow startup

I'm using VIM 7.1 on Debian. I have 9 plugins that I load via pathogen.vim. It takes around 8 sec's to load which is quite slow since this is in non-GUI/xterm mode. I ran vim -V and it shows that each module is being searched for in multiple directories.
Initially, ftoff.vim, debian.vim and other "system" related .vim files are searched for in ~/.vim/ and then in /usr/share/vim/vim71/ - I fixed this by moving my .vimrc to .vim/vimrc and: export VIM=/root/.vim, within .vimrc i did a set runtimepath=/usr/share/vim/vim71
But now, when the modules load, they alter this runtimepath and when pathogen loads it's even worse. Is there a way to specify a hash of module-name to dirPath so that this error prone lookup is avoided? Or a way to manually specify runtimepath on a per module basis within vimrc?
Here is an example of my runtimepath after pathogen loads my modules. Obviously, any further loading of a module invovles searching all those pathnames before locating the right path.
runtimepath=~/.vim,~/.vim/bundle/Align294,~/.vim/bundle/minibufexpl.vim_-_Elegant_buffer_explorer,~/.vim/bu
ndle/The_NERD_Commenter,~/.vim/bundle/The_NERD_tree,~/.vim/bundle/pathogen,~/.vim/bundle/vim-addon-mw-utils,
/.vim/bundle/tlib,~/.vim/bundle/snipMate,~/.vim/bundle/SuperTab,~/.vim/bundle/surround,~/.vim/bundle/taglist
~/.vim/bundle/Align294,~/.vim/bundle/minibufexpl.vim_-_Elegant_buffer_explorer,~/.vim/bundle/pathogen,~/.vim
bundle/snipMate,~/.vim/bundle/SuperTab,~/.vim/bundle/surround,~/.vim/bundle/taglist,~/.vim/bundle/The_NERD_C
mmenter,~/.vim/bundle/The_NERD_tree,~/.vim/bundle/tlib,~/.vim/bundle/vim-addon-manager,~/.vim/bundle/vim-add
n-manager-known-repositories,~/.vim/bundle/vim-addon-mw-utils,/var/lib/vim/addons,/usr/share/vim/vimfiles,/u
r/share/vim/vim71,/usr/share/vim/vimfiles/after,/var/lib/vim/addons/after,~/.vim/bundle/snipMate/after,~/.vi
/after,~/.vim/bundle/snipMate/after
I use vim-addon-manager and have 33 paths in rtp, but it takes around 0.7-0.8 seconds to start and immideately close vim (with vim -c 'qa!'), so the problem is either one of the plugins or your system. To check how long it takes to load each plugin, try the following script:
vim --cmd 'profile start profile.log' \
--cmd 'profile func *' \
--cmd 'profile file *' \
-c 'profdel func *' \
-c 'profdel file *' \
-c 'qa!'
You will get all timings in the profile.log. Table with function timings will be
present at the end of the file, to get per-script timings, use the following
script:
" 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])'))
This will open a new file containing just the same table as at the end of
profile.log, but 1) for scripts, not for functions, 2) unsorted.
If problem is your system, you may try the following:
When computer starts create a ram disk and mount it to ~/.vim, then copy all plugins there.
Try merging plugins into a single file, see :h scriptmanager2#MergePluginFiles() (vim-addon-manager must be activated)
Upgrade your computer
Try creating a hardlinks to all plugins in ~/.vim:
cd ~/.vim/bundle;for d in *;do cd "$d";for f in **/*.vim;do t="$HOME/.vim/$(dirname "$f")";test -d "$t"||mkdir -p "$t";ln "$f" "$t";done;cd ..;done
it might not be related, but for me the variable DISPLAY makes a big difference in the time it takes to start vim (even when I have vim compiled without gui).
Try with
DISPLAY= vim
and
DISPLAY=:0 vim
and see if you notice a difference.
http://pastebin.com/R6E4czN7
I've pasted the output of vim -V to pastebin (should be valid for 1 month). It's self explanatory. There are a gazillion searches(414 search lines - most of them are useless). I need to reduce the number of incorrect searches.
1297651453.71068: Searching for "/root/.vim/bundle/pathogen/autoload/scriptmanager.vim"[J
1297651453.71456: Searching for "/root/.vim/bundle/snipMate/autoload/scriptmanager.vim"[J
1297651453.71846: Searching for "/root/.vim/bundle/SuperTab/autoload/scriptmanager.vim"[J
1297651453.78737: Searching for "/root/.vim/bundle/surround/autoload/scriptmanager.vim"[J
1297651453.79179: Searching for "/root/.vim/bundle/taglist/autoload/scriptmanager.vim"[J
1297651453.79684: Searching for "/root/.vim/bundle/The_NERD_Commenter/autoload/scriptmanager.vim"[J
1297651453.80756: Searching for "/root/.vim/bundle/The_NERD_tree/autoload/scriptmanager.vim"[J
1297651453.83: Searching for "/root/.vim/bundle/tlib/autoload/scriptmanager.vim"[J
1297651453.86193: Searching for "/root/.vim/bundle/vim-addon-manager/autoload/scriptmanager.vim"[J
1297651453.8662: line 3: sourcing "/root/.vim/bundle/vim-addon-manager/autoload/scriptmanager.vim"[J
1297651453.88259: finished sourcing /root/.vim/bundle/vim-addon-manager/autoload/scriptmanager.vim[J

Is there a way to configure vimdiff to ignore ALL whitespaces?

I'm using vim -d file1 file2 in order to see the differences between them. This works fine, but I want to ignore whitespace changes - they are irrelevant for source code files.
Vim help states that the following command will do the magic:
set diffopt+=iwhite
But unfortunately, this command only adds -b to diff tool command line, and that only ignores trailing whitespaces. The correct command line key for diff should be -w, to ignore all whitespace changes. But I can't find how to modify the diff command line directly from Vim. Of course I can compile a custom diff, or replace diff with diff.sh, but that looks kinda ugly :(.
Is there a better way to modify how Vim interacts with the diff tool for displaying file differences?
This implements what you want (taken from the diffexpr docs with -b changed to -w):
set diffopt+=iwhite
set diffexpr=DiffW()
function DiffW()
let opt = ""
if &diffopt =~ "icase"
let opt = opt . "-i "
endif
if &diffopt =~ "iwhite"
let opt = opt . "-w " " swapped vim's -b with -w
endif
silent execute "!diff -a --binary " . opt .
\ v:fname_in . " " . v:fname_new . " > " . v:fname_out
endfunction
... I'm still looking for a better diffexpr helper with respect to handling which lines map to which (GNU diff, even with -w instead of -b, is rather baffled by combining extra whitespace with minor edits like commented lines). Maybe diffchar?
Yes. Set the iwhite option as you did, but additionally, make diffexpr empty.
From the relevant section of the vim docs:
iwhite
Ignore changes in amount of white space. Adds
the "-b" flag to the "diff" command if
'diffexpr' is empty. Check the documentation
of the "diff" command for what this does
exactly. It should ignore adding trailing
white space, but not leading white space.
Note also that you can provide a custom diff command line by setting diffexpr. See the discussion on the vimdiff man page, in particular:
The 'diffexpr' option can be set to use something else than the standard
"diff" program to compare two files and find the differences.
When 'diffexpr' is empty, Vim uses this command to find the differences
between file1 and file2:
diff file1 file2 > outfile
Thanks ire, that helped me. I now only need to have this (simpler than what is proposed by Adam K) in my ~/.vimrc :
set diffopt+=iwhite
set diffexpr=""
And it does it... That is still the most powerfull diff tool I know of, far better than any other.
I know it's an antique question but for others like me who didn't know, this is now available:
:set diffopt+=iwhiteall
Adds the "-w" flag to the "diff" command if 'diffexpr' is empty.
See :h 'diffopt'
For those hitting "Invalid argument" doing set diffopt+=iwhite, try without the + like so:
set diffopt=iwhite
However, a more robust approach would be to set ignore whitespace while preserving existing options. Beware though, that the "Invalid argument" error is likely caused by one of those existing options not being supported. In my case it was the "internal" option therefore I needed to set options in the following order:
set diffopt-=internal
set diffopt+=iwhite
Or add the following to your .vimrc:
if &diff
set diffopt-=internal
set diffopt+=iwhite
endif
Credit to https://www.micahsmith.com/blog/2019/11/fixing-vim-invalid-argument-diffopt-iwhite/
Addressing an issue brought up in the comments of Adam Katz's solution:
Depending on the vim version and setup of the user, a silent command can neglect to redraw the screen after it is issued. I also encountered this problem, which arose whenever I executed :diffo after using the suggested diffexpr. My solution was to change the silent execute command to the following:
silent execute "!diff -a --binary " . opt .
\ v:fname_in . " " . v:fname_new . " > " . v:fname_out | redraw!
This forces a redraw after the command is issued.

Resources