Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

To start with, the way vim keystrokes combine is pretty nifty. To move forward a word, w. To delete the next word, dw. To delete the next 6 words, 6dw. To move to the next "g" character on the line, fg. To delete everything to that character, dfg. To delete everything inside the parens surrounding the cursor, di(. Something I do a lot with viemu in ms sql: Select everything inside parens with vi(, then hit F5 to execute the subquery.

But it got really fun when I started using macros. Once you're doing everything with keystrokes and text objects, you can record a macro while you're doing it. So, keeping with sql, let's say I want an update statement.

First I use an IDE feature to give me a list of all columns in a table, comma-delimited.

qa starts recording a macro into register a

Wi<return> advances to the next column name and inserts a carriage return

<Esc> q stops recording macro

10@a plays the macro ten times

Now I've got each column on a different line. I go back to the first line.

qw starts recording a macro into register w

ia.<Esc> puts "a." at the beginning of the line

yw copies the column name

A = b.<Esc> goes to end of line and adds " = b."

pA, pastes the column name and adds a comma to the end

j^q moves to beginning of next line and stops macro recording

10@w does the same macro ten more times

Now with a few keystrokes, I've changed this: column1, column2, column3, ...

To this:

a.column1 = b.column1,

a.column2 = b.column2,

a.column3 = b.column3, ...

And the key point is, I did it without having to think about it much. I used the same editing techniques I would have used to make that change for just one line, and with half a dozen extra keystrokes I made it apply to as many lines as I needed.

Possibly I made small mistakes in the exact keystrokes above. When doing it for real, you see all the changes on that first line as you do them, so if something isn't quite right, just hit u to undo and keep going. It's so easy, I can record and replay new macros all the time. I almost never have to do repetitive editing.

Another trick, not available in viemu but available in vim: select a bunch of lines, then type :norm and your next editing commands after that will get applied to all the lines. For example, to add a comma to the end of every selected line, :norm A,



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: