10 September 2013

Blogging with Markdown from Vim

If you're a programmer, you probably spend a lot of time in a text editor. You may also have spent a lot of time choosing your text editor. You've probably also spent a lot of time learning and perfecting your use of that text editor. And if your text editor is Vim, you probably hate entering text anywhere other than your text editor.

If you're into writing rich content, you probably spend a lot of time using content markup tools. If you're a programmer, you probably prefer using some kind of markup language over a GUI. If you've been exposed to Markdown, you probably don't want to consider anything else.

It follows logically that as a Vim-using, content writing programmer who likes Markdown, I would like to write my blog entries using the marvelous Markdown syntax in the comfort of my powerful Vim editor. To the average blogger, this may seem like a ridiculous desire, but to a guy like me, once I discovered this was possible, I wouldn't have it any other way.

Initially, I set out to develop a Vim plugin that would allow me to post to my Blogger account. Since Blogger is a Google product, I wasn't surprised to find that my expectation that there would be a Blogger API was correct. Paired with my moderate Vim plugin skill, this was a promising starting point for my plugin.

Even though I didn't think anyone had done this before, I decided to search for an existing plugin that could at least act as a foundation. To my surprise, I found Blogger.vim, which, at first glance, covered all of my needs out of the box. It provides a way to list existing posts and write new posts using Markdown (which gets converted to HTML). It even has an automatic Gist option where code blocks longer than 5 lines are automatically uploaded as a Gist with your connected GitHub account. (This was a feature I had thought about a long time ago; I was pleasantly surprised to see that someone has already done it!)

There were a few areas that I found the plugin was lacking or made me uncomfortable, however.

First, there is no way to apply labels (or tags) to posts. While a fairly minor grievance, it would be nice to be able to manage every aspect of content creation without using a web GUI.

Second, it doesn't provide a way to list draft posts. I'm probably never going to write a blog post in one sitting, so I need a way to continue editing an existing draft. The plugin does have a way to create drafts, but after you've quit Vim, you won't be able to get back to it.

Finally, it uses plain-text authentication in your ~/.vimrc file. While this keeps the set up process simpler, an important fact is that my Blogger account is the same as my Google account. I don't want my most important password visible to all who care to browse my Vim configuration. It would be preferable for the plugin to establish an OAuth connection (or something similar) that only has access to the Blogger features I allow.

For these reasons, I decided to stick to my initial plan and develop the plugin on my own. I'll follow up with another post once I get around to doing that.

05 September 2013

Clearing a register in Vim

Earlier this week, I had a repetitive programming task to carry out. It involved converting lots of SELECT statements in SQL into Kohana ORM code. For part of this task, I needed a way to find a bunch of non-adjacent lines in the code and move them somewhere else. My editor of choice is Vim, so there are many ways this can be done.

The first method I thought of was yanking or deleting lines into a specific register. In Vim, you can do edit operations using a number of different registers, including one named for each letter on the keyboard. For example, you can delete a line into the x register with "xdd. You can also append to a register by referring to it by its capital letter variant. So, if we had previously yanked a line into register x, we could append another line to it with "Xyy. This was to be the basis of my macro: finding relevant lines and appending them to a register.

I figured it would be a good idea to clear the contents of the register at the beginning of the macro, so that successive runs don't accumulate larger and larger register contents. I didn't know how to do this at the time; I don't think there's a way to yank "nothing". Then a thought occurred to me: macros are stored in registers!

Whenever you record a macro, you choose a letter to identify it. For example, to record a macro that deletes 3 lines and identify it as e, you would use qe3ddq (qe - start recording into e; 3dd - delete 3 lines; q - stop recording). This identifier e is actually the same as the register e. To demonstrate this, you can paste the contents into your buffer with "ep. You can then edit it and yank it back into the e register and run the new macro (if it still makes sense) with @e.

So what does this mean? We have identified a quick way to clear a register: record an empty macro! So, to clear out my x register, I can just use qxq (qx - start recording into x; q - stop recording). And there you have it!

Of course, after spending no more than a minute on Google, I found a more proper way to do this using the VimL scripting language in command mode: :set @x = ''. This way seems a bit more direct, but it's not as quick or easy.

image