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

One of the features that really got me into Vim was the macros feature. I actually ran into in accidentally several times when I was first learning the basics (and couldn't get out of recording mode). But macros, and especially the temporary nature of macros, have saved me (collectively) many hours:

q <macrokey> <do some stuff>; then q to stop recording. Then @<macrokey> to kick off the macro. Splendid.



I don't meet many other vim users who like macros but recently I've had call to use them daily. They completely remove any repetitive text editing actions, even relatively complex ones like "take these words, each on a new line and make them keys to an array, with the values initialized to ''"

The only problem is that they take a lot of practice before you get them right first time. After a while though, you learn to record them fairly quickly and they streamline your workflow. They also melt the faces of anyone pairing with you.

The best part is they get better with age. The more vim commands you know, the more opportunities you find for using them.


As a new vim user, I'm still trying to figure out what to do with them.

What do you find yourself macroing, generally?


For me the most important thing was learning to add a 'j' at the end of each macro and running it multiple times. So lets say you want to crack open an array that looks like this:

    $address = array(
     'name'  => '',
     'line1' => '',
     'line2' => '',
     'state' => '',
     'country' => '',
     'postcode' => '',
    );
That's a lot of commas and quotes etc. Nowadays I usually punch this into vim:

    name
    line1
    line2
    state
    country
    postcode
Then I start on the first line and record a macro that wraps that word in quotes, then appends hashrocket, quotes, comma, and then moves to the next line. Run the macro 5 times with 5@<register> and the you get your array.


If you're a heavy macro user, you might find `:noremap <Space> @q` to be handy. Just stick your temporary macro in q (the easiest one to stick it in :), then press the spacebar to run it whenever you want.




Consider applying for YC's Winter 2026 batch! Applications are open till Nov 10

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

Search: