Saturday, November 24, 2007
I have automated the extract variable refactoring in Vim. Well, at least for languages where you don't have to declare types. This includes Python, the language I use at work. It can also be used with Ruby and other dynamic languages, as well as with languages featuring type inference, like Nemerle and Haskell.
Select (in visual mode) a value you want to extract, and press ctrl-e. This will pop up a dialog asking for the name of the variable. A line with the assignment will be added above and all occurrences of the value will be replaced with the given name. The replacement will happen only in the lines after the assignment.
To enable it, copy the snippet to your vimrc file.
let name = inputdialog("name please")
if name != ""
execute "normal O" . name . " = "
execute (line('.')+1) . ",$s/" . escape(getreg('"'), '/\') . "/" . name . "/"
noremap <c-e> y:call ExtractVariable()<cr>
December 7, 2007 at 1:23 PM
Copied to .vimrc and works with my python code.
PS. Next to your TODO list: - rename method (in whole project) ;)
- Beautiful Code: Resolver One
- Debugging memory leaks in IronPython apps
- Why Vim's modes frustrate newbies
- Four-monitor desktop
- Antipattern: static subject to observer map
- August 2007
- September 2007
- October 2007
- November 2007
- December 2007
- January 2008
- February 2008
- March 2008
- April 2008
- August 2008
- January 2009
- May 2009
- June 2009
- February 2010
- March 2010
- August 2010
- January 2011