Links Vim to ipdb, lldb, and gdb for lightweight but powerful debugging. Vim does not have to be compiled with "+python".
ipdb, lldb, and gdb can be used as text-based debuggers. Although they can give code context with the "list" command, it would be helpful to have vim highlight where you are in the code. lvdb incorporates this functionality.
Install python dependencies (if you want to debug python. This is not necessary for using lldb or gdb)::
# ubuntu 16.04 or later
# alternatively, sudo apt-get install ipython3 python3-ipdb python3-setuptools
apt-get install ipython python-ipdb python3-setuptools
Install the Vim plugin (if using pathogen):
cd ~/.vim/bundle
git clone https://github.com/esquires/lvdb
Install the python plugin (only necessary for debugging python files):
python setup.py install
To start/stop the vim debugging monitor:
:call lvdb#Python_debug()
To toggle absolute and relative line numbers
:call lvdb#ToggleNumber()
An example of mapping these commands is:
let mapleader = "\<space>"
nnoremap <leader>d :call lvdb#Python_debug()<cr>
nnoremap <leader>n :call lines#ToggleNumber()<cr>
lvdb will also toggle line numbers automatically if you desire (see :h rnu and :h nu for details. The toggling is only available for gvim). When debugging (setting breakpoints, etc) it is often nice to see absolute line numbers but when moving around in Vim, it is nice to have relative numbering (to do things like "4j" to move down 4 lines). To toggle this setting, set the following in vimrc:
let g:lvdb_toggle_lines = 0 # never toggles lines (default)
let g:lvdb_toggle_lines = 1 # only toggle lines in *.py files when the vim debugging monitor is active
let g:lvdb_toggle_lines = 2 # always toggle lines for *.py files
let g:lvdb_toggle_lines = 3 # always toggle lines for all files
Finally, lvdb will echo whether it is running when you call ``Python_debug(). To silence this, set
let g:lvdb_silent = 1 # default 0
-
cd into tests and open
temp.py
. Notice that thelvdb.set_trace()
line is highlighted. If it is not, then something is probably turning your syntax off. Check your other plugins or vimrc. You can set syntax on by typing ":syntax on" -
In vim, type
\d
to start the debug monitor(assuming you have set to "" as suggested in the mappings/settings section. You can set it to whatever you want though)
-
In the system shell (e.g., bash), type
python temp1.py
lvdb will start and break at the
lvdb.set_trace()
line (this does the same thing as ipdb but outputs some debugging information so vim knows what line/file ipdb is at). In addition, vim will jump to the same location.You can now type "s" or "n" to step through the code as normal. Vim will update the active line or open a new tab if a new file is encountered in the code.
-
In Vim, type
\d
to end the debug monitor.
-
Compile the code
$ gcc -g -o temp temp1.c temp2.c
-
start lldb
$ lldb temp
-
Source the provided
lvdb_lldb_helper.py
script (this can be put in a~/.lldbinit
file)(lldb) command script import /path_to_this_repo/python/lvdb/lvdb_lldb_helper.py
-
In another terminal, open
temp1.c
in vim and type "\d" to start the debug monitor(assuming you have set to "" as suggested in the settings/mappings section. You can set it to whatever you want though)
-
You can now type "s" or "n" to step through the code as normal. Vim will update the active line or open a new tab if a new file is encountered in the code.
-
In Vim, type
\d
to end the debug monitor
-
Create a .gdbinit file with the following contents:
set logging file /tmp/lvdb.txt set logging on
-
on the command line, cd into
lvdb/tests
and compile by typing:$ gcc -g -o temp temp1.c temp2.c
-
open
temp1.c
in vim and type "\d" to start the debug monitor(assuming you have set to "" as suggested in the settings/mappings section. You can set it to whatever you want though)
-
In the system shell (e.g., bash), type
$ gdb -x .gdbinit -f temp
-
You can now type "s" or "n" to step through the code as normal. Vim will update the active line or open a new tab if a new file is encountered in the code.
-
In Vim, type
\d
to end the debug monitor
see LICENSE in the root directory