Using Vim Macros to Undo Shortened URLs

March 08, 2019

I’ve been tinkering with my blog setup. I use Instapaper to do most of my reading on the internet. As I find links that intrigue me, I add them to my Instapaper queue. Once upon a time, every link that I starred in Instapaper got immediately posted to twitter… which explains most of my tweet count.

I have decided to be a little more thoughtful. Rather than blasting the interent, now when I like a link on Instapaper, I use an IFTTT applet to save the title and link to my personal wiki on dropbox, in a folder marked links. I have been manually copying the interesting ones out of that folder to build my monthly link posts. I just wrote a little script that grabs all the links for the current month, and outputs my preferred markdown format so that I can generate a link post at any point. I have also started pulling quotes and commentary out of the link into the saved text file, both in terms of generating the blog post and summarizing what I thought was important about it for my personal knowledge base.

One thing that bugged me about my applet was that it was saving all the links with ifttt’s link shortener… While I just discovered that you can turn that off (read the docs, right?), last month I spent a lot of time opening each link, copying the original URL, and putting it back in the blog post. Yucky.

In preparation for this month I was cleaning up the URLs I had “liked” before turning off the link shortener, and I realized that the way I was using vim was a great example of the power of Vim: movement, magic, and macros… so I humblebragged on Slack and stopped to recreate the edit as a demo:

  1. I open all the files I want to edit using nvim March*. (Because I had done this already, the first file that opens already has the unshortened URL.)
  2. Because I know I’m going to do this more than once, I use qq to start recording a macro to the “q” register.
  3. The IFTTT file always puts the URL at the bottom, so I hit G.
  4. I quickly jump to the URL using fh to “find the h”.
  5. Vim’s NETRW module allows you to follow http links and load a copy of the file that’s found there… so when I hit gf for go to file on the URL, it downloads the file and opens it.
  6. /href searches for “href”, taking me to the correct line. I use yi" to yank the new, lengthened, URL out of the quotes.
  7. I use <CTRL-o> to step out of the file and into the original file.
  8. A quick creation of a new line, p to paste my URL, and I step up to the previous line to delete it. I hit q again to stop recording the macro.
  9. I use a binding to jump to the next file, and can hit @q to play back my macro.

A “real” version of this macro would have also probably saved the file and automatically gone to the next file in the list using :bn, but this was a quick and dirty version for internet fame. It’ll do.

I’m always telling people it’s worth the effort to learn their preferred editor well. Maybe this task demonstrates why Vim is a good option. I didn’t even stop to think what I was doing until I was done, and I had accomplished a fairly complex and repetitive task with speed and efficiency.

  • 2019-03-08 16:07:02 -0600

    Remove dumb duplicate title.

  • 2019-03-08 15:30:30 -0600

    Post: Vim Macros example