<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Milton Soares Filho &#187; script</title>
	<atom:link href="http://blog.msoares.pro.br/tags/script/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.msoares.pro.br</link>
	<description>Random rants and SW developers' stuff</description>
	<lastBuildDate>Fri, 28 May 2010 14:42:04 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Vim Scripting Using Python</title>
		<link>http://blog.msoares.pro.br/archives/vim-scripting-using-python</link>
		<comments>http://blog.msoares.pro.br/archives/vim-scripting-using-python#comments</comments>
		<pubDate>Fri, 10 Apr 2009 01:15:12 +0000</pubDate>
		<dc:creator>milton</dc:creator>
				<category><![CDATA[Planet INdT]]></category>
		<category><![CDATA[devel]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[vim]]></category>

		<guid isPermaLink="false">http://msoares.dreamhosters.com/?p=39</guid>
		<description><![CDATA[Trying to solve a colleague&#8217;s problem when editing Edje files, I pushed myself into learning how to do scripting inside Vim.
Scripting wasn&#8217;t ever my choice for reasonably complex editing tasks since I refused to learn Yet Another Scripting Language just because my favorite editor wanted me to. But, for the sake of all lazy guys [...]]]></description>
			<content:encoded><![CDATA[<p>Trying to solve a colleague&#8217;s problem when editing Edje files, I pushed myself into learning how to do scripting inside Vim.</p>
<p>Scripting wasn&#8217;t ever my choice for reasonably complex editing tasks since I refused to learn <em>Yet Another Scripting Language</em> just because my favorite editor wanted me to. But, for the sake of all lazy guys like me, Vim started to add python support, and python was a <strong>must learn</strong> bullet in my language listing.</p>
<p>However, not everything are flowers and the entry point must be configured using common vim script. Well, at least until vim supports python from its core, which &#8211; I believe &#8211; is not far from possible, since python integration <a href="http://www.vim.org/sponsor/vote_results.php">has been voted</a> as top priority for a long time.</p>
<p>To successfully import your python script inside vim context, one can wrap it into a vim function in an external file. Let&#8217;s call it <code>extras.vim</code>.</p>
<pre name="code" class="python">
" Title-ize sentences using python str methods
function! PyMakeTitle() " the ! erases previous definitions
python &lt;&lt; END " here-document (bash-style), read 'till given word
import vim
w = vim.current.window
b = vim.current.buffer
line, col = w.cursor
line -= 1
b[line] = b[line].title() # str.title() method
END
endfunction
</pre>
<p>After using <code>:source extras.vim</code> command to load it, one can call this function by typing <code>:call PyMakeTitle()</code>. Remember repeating the <code>:source</code> step every time the script gets updated.</p>
<p>The net effect of this function is to turn all initial word letters in the current line into capitals. It proved me to be useful when editing a large LaTeX document where all section titles were small letters only.</p>
<p>If it comes to be a very useful function, you may map it to a key-stroke by using <code>:nmap \t :call PyMakeTitle()&lt;CR&gt;</code> inside your vimrc script.</p>
<p>A more complex example accessing internal vim properties. Ok, it&#8217;s a bit useless, but it demonstrates well such features.</p>
<pre name="code" class="python">
" Auto Documentation (example code)
function! PyCreateDoc()
python &lt;&lt; END
import vim
name = vim.eval("expand(\"&lt;cword&gt;\")") # expand word under cursor
ts = int(vim.eval("&amp;ts")) # tab space property
il = int(vim.eval("indent(\".\")")) # indentation on current line
w = vim.current.window
l, c = w.cursor
docstr = '%s# @brief %s - write description' % (' ' * il, name)
b = vim.current.buffer
b[l-1:l-1] = [ docstr ]
print 'Indent set is %d, cursor at (%d, %d)' % (ts, l, c)
END
endfunction
</pre>
<p>With the cursor over a function name, type <code>:call PyCreateDoc()</code> and watch it insert a line above containing doxygen-like formatting.</p>
<p>For more examples, there is an excellent material at vim&#8217;s online help (see <code>:h python</code>). Furthermore, there are some plugins being made using only python, take a look at <strong>Omni Completion for Python</strong> (<a href="http://www.vim.org/scripts/script.php?script_id=1542">pythoncomplete.vim</a>) for a good reference on the power of using python inside vim.</p>
<p>Happy Easter Vim&#8217;ing!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.msoares.pro.br/archives/vim-scripting-using-python/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
