Debugging Guide for Vim Errors

2 minute read Published: 2022-04-25

Vim's default configuration presents significant debugging challenges due to its minimalistic design. While plugins substantially enhance Vim's capabilities, they can introduce complex error scenarios that require systematic troubleshooting techniques. This guide provides a structured approach to diagnosing and resolving Vim-related issues.

Error Message Management

Vim's error messages are transient and may disappear after a key press. To inspect recent errors:

:echo errmsg

In normal mode, use :messages to view all historical messages. For persistent debugging, capture messages in a new buffer:

:vnew | put=execute('messages')

Stack Trace Analysis

Complex errors often manifest as stack traces. The following example illustrates how to interpret such messages:

Error detected while processing function <SNR>163_on_exit_vim[15]..1:
line    1:
E687: Less targets than List items

The <SNR>163 and [15] components indicate that the 15 line in 163rd script triggered the error. Locate the responsible script using:

:vnew | put=execute('scriptnames')

The output may look like this:

163: ~/.vim/plugged/vim-gitgutter/autoload/gitgutter/async.vim

Navigate to the script file with gf on the path. Then inspect the function at the specified line (e.g., 15j).

Debugging Workflow

Once the error location is identified, execute targeted debugging:

debug echo expand('%:h')

or in neovim use:

vim.cmd(":debug echo expand('%:h')")

This command enables interactive debugging with the following control commands:

CommandAction
stepExecute command but step into functions
nextExecute command but step over functions
contContinue execution until next breakpoint
quitStop execution immediately
interruptForce pause until next command
finishContinue until current script/function completes
breakadd/breakdelManage breakpoints with positional arguments

Breakpoints locations can specify as follows:

Logging Capabilities

Vim includes built-in logging through verbose mode:

vim -V9 myVim.log

This command starts Vim in maximum verbosity, writing detailed logs to myVim.log. Comprehensive documentation exists in Vim's native help system and relevant Stack Exchange discussions.

Resources