Rory's Blog
Home
Blog
Projects
Facebook
Google+
Github
RSS

Vim: Macros

03/21/15

So, with my last vim post, I talked about the usefulness of vim registers. As it turns out, vim macros both further show how useful registers are and completely replace how I was using them before.

Whereas before I would copy something into a register with

"<key>y

now I record something into that register with

q<key><actions I want to repeat>q

And then I save my keystrokes into that register, and

@<register key>

repeats those keystrokes.

This is much more powerful than a simple copy-paste board where I can copy to various different keys. Now, any regular action that can be properly defined as some series of keystrokes can be saved into a register.

So, not only does this mean that I can insert an html paragraph at the right indentation now with '@p' instead of '"pp', I can also save how I "publish" my blog and diary posts into a register as well. As you can imagine, Stephan, this saves a lot of time.

Another cool thing, though, is that because I'm literally using the same registers, I can literally paste the register's recorded keyboard input using

"<register>p

So, what does register p look like now?

o<enter><p><enter></p><escape>

Which translates as "Enter insert mode in a new line below current cursor, then press enter to make another new line, then insert "<p>" and press enter to go to the next line and insert "</p>", and then exit insert mode.

Now let's go over my save register, register s (the following is all one line):

gg/blogpost<enter>V/^      <\/div><enter>y:sp ../index.html<enter>gg/blogpost<enter>P jo<tab><a href="<escape>"#pa"><escape>j>>A</a><escape>

Anyways, that macro above goes to the top of the file I'm in, searches for the string "blogpost", enters VISUAL LINE mode, searches for the matching closing div tag, yanks the highlighted text to the default register, opens up ../index.html, goes to the top of the file, searches for the first instance of the string "blogpost", pastes the yanked text above that first instance, goes to the title of the post, and adds a hyperlink to the blogpost file itself.

I can't believe I used to do that by hand for every single post. Painful. Also, I can't believe I still do the static HTML file thing. I really need to redo the lost work I did on my node setup.

So, anyways, that's vim macros—crap, I overwrote a hard-to-make macro.

Saving Macros in the Vimrc

It's surprisingly easy to accidentally overwrite one's macro, and that's assuming you want them saved between sessions in the first place (some don't, I totally do). Accidentally pressing q and not realizing it, to go on to press any other key is pretty easy (the easiest being q, g, j, k... any of the many move the cursor keys).

Luckily, there's a way to set macros in one's vimrc.

let @<register key>='<keystrokes for macro>'

Mix this with my ability to paste the recorded keyboard input above, and I can record some keyboard input to a macro, test it out, and if I like it save it to my vimrc.

Different Strokes for Different Folks Filetypes

So, you know, there's only so many registers, I don't know how many are good for macro placement, but it is limited. And stuff I want to do in an html file doesn't translate well to stuff I want to do in css, javascript, or any other kind of file.

Luckily, I already figured out how to have different vim setting for different filetypes. So I can save my html macros in my html.vim, and I can make macros for javascript and save them in my javascript.vim, and so on and so forth (this is exactly what I will do).

Going Further?

Hmmm... saving my registers to some sort of vim config automatically might be nice. Maybe that's something vim functions could do? Maybe, I don't know, I know nothing about vim functions.

....

Which is exactly why my next vim post will be about vim functions, Stephan.

PS: I have discovered that there's an HTML code tag. What does it do? Use the default monospaced browser font, of course. I'll start using it from now on instead of my code class (though, I'll have to figure out how I want to style it).