您的位置:首页 > 编程语言 > Python开发

Debugging Python in VIM

2009-03-23 21:46 453 查看



Debugging Python in VIM

Submitted by Peter on Thu Apr 14 2005 11:23
over 3 years ago

Following my thoughts yesterday, here are some VIM python scripts to add python
breakpoint and debugging features to VIM. With this set up the F7 key
will set a breakpoint on a line of code, Shift-F7 will remove all
breakpoints and Shift-F12 will execute a script in the python debugger.
This only runs on windows as far as I know, because it uses the 'start'
command to launch the debugger in a seperate process without VIM
waiting for it to finish. This allows you to look through the source
code (and fix it) while the debugging is still in progress.

This goes in a python block in _vimrc:

1  def SetBreakpoint():
2      import re
3
4      nLine = int( vim.eval( 'line(".")'))
5
6      strLine = vim.current.line
7      strWhite = re.search( '^(/s*)', strLine).group(1)
8
9      vim.current.buffer.append(
10         "%(space)spdb.set_trace() %(mark)s Breakpoint %(mark)s" %
11           {'space':strWhite, 'mark': '#' * 30}, nLine - 1)
12
13      for strLine in vim.current.buffer:
14          if strLine == "import pdb":
15              break
16      else:
17          vim.current.buffer.append( 'import pdb', 0)
18          vim.command( 'normal j1')
19
20  vim.command( 'map <f7> :py SetBreakpoint()<cr>')
21
22  def RemoveBreakpoints():
23      import re
24
25      nCurrentLine = int( vim.eval( 'line(".")'))
26
27      nLines = []
28      nLine = 1
29      for strLine in vim.current.buffer:
30          if strLine == 'import pdb' or strLine.lstrip()[:15] == 'pdb.set_trace()':
31              nLines.append( nLine)
32          nLine += 1
33
34      nLines.reverse()
35
36      for nLine in nLines:
37          vim.command( 'normal %dG' % nLine)
38          vim.command( 'normal dd')
39          if nLine < nCurrentLine:
40              nCurrentLine -= 1
41
42      vim.command( 'normal %dG' % nCurrentLine)
43
44  vim.command( 'map <s-f7> :py RemoveBreakpoints()<cr>')
45
46  def RunDebugger():
47      vim.command( 'wall')
48      strFile = vim.eval( "g:mainfile")
49      vim.command( "!start python -m pdb %s" % strFile)
50
51  vim.command( 'map <s-f12> :py RunDebugger()<cr>')


This relies on using the runscript plugin, and modifying the function 'SetMainScript' as follows:

function s:SetMainScript()
let s:mainfile = bufname('%')
let g:mainfile = bufname('%')
echo s:mainfile . ' set as the starting program.'
endfunction


This allows F11 to be used to select which file is executed to start
debugging. F12 can still be used to launch the script outside of the
debugger.

Now I don't need WingIDE or a pc upgrade.

Filed under:

php

python

vim

windows

wingide

Matt Says:

Wed Nov 23 2005 19:49
over 3 years ago

What do you mean by "a python block in _vimrc"? I was unaware of any way of embedding Python in a vimrc, myself...

Peter Says:

Wed Nov 23 2005 20:39
over 3 years ago

_vimrc is just the standard vim config file. It is called _vimrc on
windows because windows does not like the name .vimrc. A block of
python can be defined in this, or any .vim file, as follows:

"
" typical vim options
"
set backupdir=c:/tmp/VIMbackup
"
" Block of python
"
python << EOF
import time
import vim

#
# Insert todays date
#
def InsertDate():
vim.current.line = vim.current.line + time.strftime( "%d %B %Y")

EOF

"
" Define vim mapping to call the python function
"
:map <C-D> :py InsertDate()<cr>


Now pressing ctrl-d will cause the date to be pasted into the current file.

I find it much easier to write macros in python that vim: the vim
language is plain weird. You need a version of vim compiled with python
support enabled. Then just go

:help python<cr>


Peter

john Says:

Thu Mar 30 2006 03:04
over 2 years ago

If you're running python 2.3, try replacing:

vim.command( "!start python -m pdb %s" % strFile)
with:

vim.command( "!start python %s" % strFile)

because the "-m" option wasn't added until 2.4. In this context the "-m
pdb" seems unnecessary because the F7 key adds "import pdb" at the top
of the script.

If you tried everything here but just saw a dos window appear and
disappear before you could read what it said, try this: open a dos
window, cd to the script's directory, and run the python command (eg
"python myscript.py"). Often the problem is a syntax error (python's
nice error handling doesn't kick in until the syntax is verified).

Adieu Says:

Fri Mar 23 2007 08:42
over 2 years ago

The runscript plugin has been moved to http://www.vim.org/scripts/script.php?script_id=127
Manpreet Singh Says:

Wed Nov 14 2007 21:56
about 1 year ago

I replaced

vim.command( "!start python -m pdb %s" % strFile)


with

winos = vim.eval('has("win32")')

if string.atoi(winos):

vim.command( "!start python -m pdb %s" % strFile)
else:

vim.command( "!xterm -e python -m pdb %s&" % strFile)


to use xterm to get the same effect as start on unix.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: