Vim and python efficiently

Andreas Poisel andi at opencan.cc
Sat Feb 10 16:06:12 EST 2001


Viktor Lakics <lakicsv at usa.net> wrote:

> Hi All,

> I would like to collect the tips, hints and macros, regarding how to edit python
> sorce code in vim. I mean stuff like macros for commentify,
> uncommentify, bracket and keyword autocomlpletion, python style
> indenting,  etc. (I know that all this exist in IDLE, but I like vim
> very much and I do not like to use different editors for different
> languages... 
[...]

Hi Viktor,

I really like your idea of a python + vim HOWTO. I'm not a vim expert, but I
have some small pieces of python code I use in vim, maybe it's useful for you:

I use this function to get a list of the python functions in the current buffer
with their line numbers:

def Functions(fname):
	print"----------------------------------------------------------------"
	cbuffer= vim.current.buffer
	zn= 0
	for line in cbuffer:
		zn= zn+1
		if re.compile(fname).search(line):
			print "%s %s" % (string.strip(line), zn)

I call it with 
map <F5> :py Functions("def ")<C-M>
from my .vimrc

Sometimes I work with files containing similar large functions. I use this one
to get the name of the function the cursor is in:

def thisFunc(str):
	cbuffer= vim.current.buffer
	cwindow= vim.current.window
	(line, column)= cwindow.cursor
	lnr= line-1
	while (lnr > 0):
		line= cbuffer[lnr]
		if string.find(line, str) != -1:
			print string.strip(line)
			break
		lnr= lnr-1

I call it with 
map <F6> :py thisFunc("def ")<C-M>
from my .vimrc

This one is a classbrowser using tkinter and Pmw. Its an ugly hack and I'm sure
it's buggy (I don't know, because I don't use this) but somehow it works.
You can navigate through buffers and classes by doubleclicking their names and
jump to a buffer/class/method by klicking the "go" button or doubleclicking on a
methods name.

class ClassBrowser:
	def __init__(self, kid, mid):
		sys.argv= ["/home/andi/vim/avim.py"]
		self.mid= mid
		self.kid= kid
		self.methDia= None
		self.claDia= None
		self.buffers= []
		self.getBuffer()
		self.root= Tk()
		Pmw.initialise()
		self.erzeugPanes()
		self.BufferBrowser()
		self.root.mainloop()

	def erzeugPanes(self):
		self.pane= Pmw.PanedWidget(self.root, hull_width= 650, 
									hull_height= 300, 
									orient= HORIZONTAL)
		self.pane.add("buffer", size= 200)
		self.pane.add("classes", size= 200)
		self.pane.add("methods", size= 200)
		self.pane.pack(expand= YES, fill= BOTH)

	def getClasses(self):
		self.classes= []
		self.cladict= {}
		self.lnrdict= {}
		lnr= 0
		for line in vim.current.buffer:
			lnr= lnr+1
			if re.compile(self.kid).search(line):
				kl= string.strip(line)
				self.classes.append(kl)
				self.cladict[kl]= []
				self.lnrdict[kl]= lnr
			elif re.compile(self.mid).search(line):
				me=  string.strip(line)
				try:
					self.cladict[kl].append(me)
				except NameError:
					ff= "Functions"
					try:
						self.cladict[ff].append(me)
					except KeyError:
						self.lnrdict[ff]= lnr
						self.classes.append(ff)
						self.cladict[ff]= []
						self.cladict[ff].append(me)
				self.lnrdict[me]= lnr

	def getBuffer(self):
		bnr= 1
		for buffer in vim.buffers:
			self.buffers.append("%s %s" % (bnr, os.path.basename(buffer.name)))
			bnr= bnr+1

	def goMethod(self):
		m= "%s" % self.methDia.getcurselection()
		lnr= "%s" % self.lnrdict[m]
		vim.command(lnr)
	
	def goClass(self):
		k= "%s" % self.claDia.getcurselection()
		lnr= "%s" % self.lnrdict[k]
		vim.command(lnr)

	def goBuffer(self):
		b= string.split("%s" % self.bufDia.getcurselection())
		vim.command("b%s" % b[0])

	def BufferBrowser(self):
		self.bufDia= Pmw.ScrolledListBox(self.pane.pane("buffer"),
listbox_selectmode= SINGLE,
										  labelpos= NW, usehullsize= 1,
										  hull_width= 200, hull_height= 200,
										  label_text= 'Buffers:',
										  items= self.buffers,
										  selectioncommand= self.goBuffer,
										  dblclickcommand= self.ClassBrowser)
		self.bufDia.pack(expand= 1, fill= BOTH, padx= 5, pady= 5, side= LEFT)
		Button(self.root, text= "go!", command= self.root.destroy).pack(side=
RIGHT, anchor= S)

	def ClassBrowser(self):
		self.getClasses()
		if self.methDia:
			self.methDia.destroy()
			self.methDia= None
		if self.claDia:
			self.claDia.destroy()
			self.KlaDia= None
		self.claDia= Pmw.ScrolledListBox(self.pane.pane("classes"), 
										  listbox_selectmode= SINGLE,
										  labelpos= NW, usehullsize= 1,
										  hull_width= 200, hull_height= 200,
										  label_text= 'Classes:',
										  items= self.classes,
										  selectioncommand= self.goClass,
										  dblclickcommand= self.MethodBrowser)
		self.claDia.pack(expand= 1, fill= BOTH, padx= 5, pady= 5, side= LEFT)

	def MethodBrowser(self):
		if self.methDia:
			self.methDia.destroy()
			self.methDia= None
		self.methDia= Pmw.ScrolledListBox(self.pane.pane("methods"), 
										  listbox_selectmode= SINGLE,
										  labelpos= NW, usehullsize= 1,
										  hull_width= 200, hull_height= 200,
										  label_text= 'Methods:',
										  items= self.cladict["%s" %
self.claDia.getcurselection()],
										  selectioncommand= self.goMethod,
										  dblclickcommand= self.root.destroy)
		self.methDia.pack(expand= 1, fill= BOTH, padx= 5, pady= 5, side=
LEFT)


I invoke this one in the .gvimrc:
map <F5> :py ClassBrowser("class ", "def ")<C-M>

Maybe you can use this somehow.
-- 
Andi



More information about the Python-list mailing list