thoughtbot is filled with vim and vigor
Posted by Mike Burns
Dec 19
Slowly over the past year thoughtbot has turned from an all-TextMate (and one Emacs) group into a mostly-vim group. Here are some tips, tricks, and scripts we've mastered along the way:
From Mike Burns
I've been a vim user for six years, and brought the vim fever to thoughtbot. I tend to rely more on the built-in commands than on scripts and plugins. My favorite commands start with f and t (think first and 'till - see :help f or :help t):
Finding characters with f and t rocks. For example, if you're at the beginning of:
{:foo => 1, :bar => 3, :baz => 8, :barney => -1}
And you want to put a newline after the comma, you can do:
f,a<ENTER>
To jump to the next comma and repeat the prior action you can do:
;.
This is because ; repeats the prior f or t, and . repeats the prior modification.
If you have to do this a lot in a row, you could make a macro from it:
qa0f,a<ENTER><ESC>q
This sets the macro named 'a' as: go to the beginning of the line, find the first comma, and append a newline after it. You can then repeat this macro with @a and repeat that macro (the one you just did) with @@.
You can also use t and f as a motion for, say, c and d. Like if you're at the beginning of:
<img src="/images/michaelphelpssupersaiyan.gif" />
And you need to change the image source to "/images/giant_french_spider.jpg". It goes like this:
2f/ ct"giant_french_spider.jpg<ESC>
The 2 in front of f finds the second /; the space immediately after moves to the next character (could also use l (lowercase L) instead). Then change the text up to (but not including) the doublequote with ct"; type the text and hit escape to get back to command mode.
From Jason Morrison
Three of my favorite things in Vim are the rails.vim plugin, the ack.vim plugin, and visual block mode.
The rails.vim plugin does an excellent job of
syntax highlighting, navigating from one file to another (type :help :RT for a description of
how to quickly jump between e.g. models and tests or a controller action and its view), and a slew of other things.
The Ack plugin for Vim nicely integrates Ack search results into a new buffer. For example,
:Ack delete app/
gives you:
Finally, visual block mode, a killer feature available in several editors, comes in handy when changing swaths of similar code. Let's say you start with this:
{
:first_name => (primary_contact && primary_contact.first_name),
:last_name => (primary_contact && primary_contact.last_name),
:organization => (primary_contact && primary_contact.company)
}
and then you:
- Place the cursor after the first "primary_contact"
- Type
control-vto enter visual block mode - Type
f.to move onto the first period - Type
jjto move down twice - Type
cto change the highlighted text - Type an underscore, and hit
escapeto apply the change to all selected lines.
then you end up with:
{
:first_name => (primary_contact_first_name),
:last_name => (primary_contact_last_name),
:organization => (primary_contact_company)
}
Often this can be done with a search/replace, but this is the technique I usually favor due to its brevity.
From Chad Pytel
vim was my first programming text editor, and its been fun to come back to it after all this time. It fits like an old glove.
I haven't gotten used to visual editing mode yet, so I tend to use line numbers to quickly do things. for example, to move lines 40 through 50 to line 30, you can do:
40,50m30
Many of the commands you'd use with visual mode, work with line numbers too, for example:
35,70=
14,16y
16,20d
From Jon Yurek
Getting around in vim is way quicker than just holding down the arrow keys. If you're checking a bit of code and moving between it and somewhere else, you should set a mark. m<letter> sets a mark, which you can jump to with '<letter>. Likewise, jumping to a specific line is as simple as :<line-number>. Got a failing test on line 146? :146 and you're there. So you can set a mark for the line you're on with ma, jump to line 20 with :20, fix your typo, then jump back with 'a, and you don't have to remember exactly what line you're on. Even better ` instead of ' will jump to the column you were on, not just the line. Also cool is `. which jumps to the last place you edited.
Thing is, when you do that, you may find your cursor is at the bottom of the screen, making further code hard to read. Fix that with z., which will zip the line the cursor is on to the middle of the window. You can also snap it to the top (z<ENTER>) or bottom (z-) just as easily.
When you cut a line with d (or yank it with y), the text you paste goes into a register. You can control the register it goes into by hitting "<letter> first. For example, "gyy will copy the current line into the g buffer. You can paste this text back by specifying the register before your p command: "gp.
From Dan Croak
I'm a casual vim user, mostly when pairing with Joe. My favorite commands are basic, but nice:
:AV
Say I'm in app/models/user.rb. This opens the *Alternate* file (unit test) in a *Vertical* window split next to the model.
gf
From Tim Pope's rails.vim:. When I put my cursor over a piece of text and type gf in a Rails app, I *go* to the *file* I'd expect. It works for partials, requires, and more.
My configuration comes directly from Joe's dotfiles.
Comments on this post
Dec 19
theJareCare said,
vim tips? pfft.
i’d like to C-x k this blog post, emacs!!!!!!
http://www.youtube.com/watch?v=S76pHIYx3ik
Dec 19
kent said,
wow. this movie with Stallman is creepy.
vim ftw!!!
Also http://marc.info/?l=git&m=122955159617722&w=2
Dec 19
Geoffrey Grosenbach said,
The Ack plugin is a nice tip. When I used vim, it was my first interaction with a tags file. That works well for jumping to method definitions and, I think, even works for code completion.
Dec 19
albemuth said,
You guys are nuts, it’s like writing a script that outputs your code
Dec 20
Igor Petrushenko said,
I feel like I’m back to Linux, when I’m using vim…don’t you?
Dec 20
Don said,
Ah, Vim. Not only is there an obscure key combination to do whatever you want, there’s two. The way I learned jump to line is not ”:20”, but “20G”. I have no idea what “z-” does, but I know “zz”, “zt”, and “zb”.
I guess my favorite Vim tip for Ruby that I learned recently is jumping to method definitions. “]m” jumps to the next method definition, “[m” jumps to the previous. There are also “]M” and “[M” to jump to the “end” that marks the end of the method. Like everything, it uses counts and actions. If you’re in a method and want to clear out everything until the end, “d]M”. Didn’t think I’d use it, but now I do quite a bit.
Thanks for the tip on the Ack plugin!
Dec 20
Mischa said,
I’m curious how you guys use window splitting. I’ve been trying to find the right balance with that.
Dec 20
Steven G. Harms said,
At work, pretty much 5-8 hours a day are spent in vim: mutt, editing perl, etc. In my side projects (Ruby) or when working in LaTeX, I’m usually in Textmate. I also used emacs for about 3 years.
Textmate is as “as close to an integrated IDE” as I’d like to go. It is essentially an emacs with OSX GUI overlain and minus the C-? command arcana. Accordingly, its biggest strengths come from the inheritance of the emacs visual selection modes only later ported to vim: line select and rectangle select. The other key navigational features ( lacking on old school TTYs ) that made emacs so stellar at the time of its inception were word-(backwards|forwards), (sentence|line)-(backwards-forwards), etc. These are swallowed by the NSText* interface basics. As such, I can see why emacs can say “meh” to Textmate. Textmate loses qua efficiency insofar as the Cocoa magic requires your hands to leave the home row to access the navigational keysets.
Further, textmate loses out because its copy-board is single instance, AFAIK. The kill-ring in emacs can hold n-many kills ( copies / cuts ) and you can choose to cycle backwards. Vim also has a solution to this: the named register for a copy / cut.
As far as quick transforms go, I think Vim has the edge. As cited above, a macro is simple to present in vim. It’s slightly less simple in emacs – but you gain the ability to use elisp – so maybe there’s a wash. I don’t think Textmate has integrated speed in macros (again, afaik).
I also think search and replace in textmate/emacs suffers in comparison to vim. In TM you highlight, cmd+alt+r to get to a script and then pipe through sed or perl or ruby’s regex replace. In vim, highlight (or:% prefix)s/foo/bar/gc is as simple as you can get and incredibly powerful.
I think if TM could fix on-the-fly-quickie-macro ( a la vim ) and get vim-mode search and replace it would be a perfect straddle between the emacs and vim camps.
See Also: http://stevengharms.com/emacs-v-vi-is-rooted-in-the-love-of-lisp
Dec 20
Henrik N said,
Steven: TextMate has Paste Previous and Paste From History.
Dec 20
Antoine said,
Good post ! I’m glad you find my Ack tip useful.
Dec 21
Shadowfiend said,
Indeed, I prefer zz to z. for centering and `` to `. for jumping to the last point, since it means less movement in both cases. zt and zb are nice, too, since they are `top’ and `bottom’ for mnemonics, but once you’ve learned them it doesn’t have much added change (aside from perhaps being closer to your fingers).
Dec 21
Mike Burns said,
@Mischa: I edit multiple files like this: vertical split for tests; horizontal split on the non-test window for views or related; horizontal split on the test window for factories, shoulda helpers, test_helper, or related; new tab for any file with its own test; new instance of gvim for results of grep or files from other projects.
Dec 21
Steven G. Harms said,
@Henrik: I’ll take a look at that paste from history. So, it would appear TM follows the ‘endless kill-ring-buffer’ design move like emacs. Textmate: the best emacs since AquaEmacs.
Dec 22
Colin Jones said,
Thanks for all the tips, guys; I’ve been getting into Vim myself the last 3 months or so. The NERD Tree plugin (http://www.vim.org/scripts/script.php?script_id=1658) is also great for anyone migrating from TextMate (like the Project Drawer over in that world).
Dec 26
Michael Mahemoff said,
+1 rails.vim for bringing vim to prominence in the rails community over the past few months.
@Shadowfiend Have you tried :set scrolloff=999 ? Then it’s always centered, which I like, so no need for zz and scrolling micro-management. (It also frees up “z” for other macros.)
Sorry, comments are closed for this article.
© 2000 - 2009 by thoughtbot, inc.
written by a bushel of tiny robots
Come “ride the toad” on Hoptoad, the app error app.
Thunder Thimble: Brand monitoring for social media.
Widgetfinger: Simple content management for simple websites.
Tee-Bot, funny shirts your friends won't understand!
Umbrella Today: “It’s like totally the simplest weather report ever, Julie.”
Thoughtbot
thoughtbot is a technology consulting firm that provides web application development and design services. We focus on building modern systems, embracing good ideas and delivering elegant solutions.
Interested in learning Rails?
Sign up for our beginning or advanced training.
Archives