Question

What is the preferable, general method of debugging/tracing vim plugins ? Suppose I have got a fairly sophisticated plugin Foo, which on a key pressed F9 opens new window with file-browser and gives the possibility to choose a file, after that the filename is copied into the main window. I would like to see what is called when I press the F9 key, some kind of call trace.

Was it helpful?

Solution

The best way I have found is to use the -V flag when starting (g)vim. You can specify a level of tracing N and a filename for the written log:

$ vim -V[N]{filename}

Then trace messages will be given for each file that is sourced. (See :help -V for more info.)

Trawling through the resulting logfile can be painful, but it is usually pretty informative. I find it best to view the logfile before and after the trigger event (pressing <F9> in your case) to get a picture of when is happening.

OTHER TIPS

If you already have vim open, try executing the command manually under VIM's built in debugger.

1) Find out what vim does when you press the key

:map <F-9>

2) Run the mapped command manually under debugger

:debug _mapped_command_

3) Right now you should be dropped in to the debugger, so

set verbose=20

4) And finally press n and Enter key to continue running the script

At this point you should see a whole bunch of output on the screen. You can press Space to scroll the screen, j/k to move by line.

Any output that starts with "Line #:" is the line vim is executing at that time.

For sophisticated plugins, normally command line debugging or tracing is not enough.

You can use BreakPts to do a visual debug inside vim.

It is based on remote debugging, so you need to debug a server instance of vim.

Basically:

Terminal 1:

$ vim --servername Foo
...
set breakpoint on any Foo function
do whatever operation which trigger Foo logic
...

Terminal 2:

$ vim
:BreakPts
:BPRemoteServ FOO
:BPDWhere locate (actual debug execution point)
:BPDNext or F12 (next execution line)
:BPDStep or F11 (step inside functions, dictionary functions)
:BPDEvaluate or F8 (if pressed on visual selection evaluates that)
:BPDCont or F5 (continue execution)

See some plugins are loaded dinamically so you need to operate with them prior to set breakpoints.

Once loaded you can set breakpoints from connected vim with:

:BPFunctions (Show debuggeable fuctions on RemoteServer)
:BPScripts (Show debuggeable scripts on RemoteServer)
:BPPoints (Show defined breakpoints on RemoteServer)

I have fix/tweak/evolve a lot of vim plugins thanks to this great plugin.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top