Reading vimscript stacktraces

When you get an ERROR in vimscript, vim will show you a stacktrace, as you might also know it from other languages. But reading them is not trivial and I haven't found any documentation of it so far. When you see them the first time, you might interpret them wrong and search the error at the wrong location.

Let's look at a stacktrace from vimwiki:

Error detected while processing function vimwiki#base#follow_link[58]..vimwiki#base#open_link[30]..vimwiki#base#edit_file:
line   21:
Error detected while processing function vimwiki#base#follow_link[58]..vimwiki#base#open_link:
line   30:
E171: Missing :endif
Error detected while processing function vimwiki#base#follow_link:
line   58:
E171: Missing :endif

The most relevant information is in the first three lines:

The first lines tell us that the error occurred in the function vimwiki#base#edit_file, wich was called by the function vimwiki#base#open_link, which was called by the function vimwiki#base#follow_link. From the names of the functions we learn in which file we will find them: autoload/vimwiki/base.vim.

The second line of the stacktrace tells us the line number where the bug occurred: line 21. But, here's the catch: all line numbers are relative to the function. So the bug occurs 21 lines below the definition of the function vimwiki#base#edit_file. The numbers in the square brackets are also relative line numbers, of where in the functions the next function was called.

The third line tells us the error that occurred. In this case the error is called ATTENTION and has the error code E325. You can look it up with :help E325 or :help ATTENTION.

The rest of the lines show how the error propagates though the callers. They are seldom useful (at least to me).

vim vimscript en

Creative Commons License How to read a vimscript stacktrace by Michael F. Schönitzer is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.