Blog's control panel: | Home | Tags | Index | Rss 2.0

Blogging with Vim

Sun, 08 Apr 2007 | Permalink | Tags: , , ,

In the face of blogging clients, xml-rpc servers, blogging APIs and tons of other kludge, here is how I use vim and svn to post on my pyblosxom based blog.
For pyblosxom a post is a text file placed in a predetermined directory, so my bloggin process looks like:
1. create a file
2. commit to svn
3. check-out from svn into the right place on the server

Post templating and creation

The first problem I had to solve was the base skeleton of a post, which I was used to copy manually all the times from an older post. This was easy to solve, I just had to tell vim to load a template whenever I edited certain files, and ended up creating an alias for it:
alias blog='vim "+0r ~/.blog/post.tmpl"'
Unfortunately that appends the template to a file even when it's not a new post, so I had to turn that into a bash script and test for the file changing the vim command accordingly. You can download the script here. There are a few extra goodies in there, like checks for a sane filename (no punctuation!) and a vim command to load the correct filetype in order to get syntax highlighting right. Also, I define a BLOG directory and an extension, which means I can now simply do blog vimBlogging and it doesn't matter where I am, a file will be created in the right place with the right ext.
Time permitting I'd like to add bash_completation support to it so when you do blog it lists all your posts and offers autocompletation on 'em.

Committing to SVN

Aside from this post and blogging, is you use Vim and SVN/CVS/SVK do yourself a favor and get this plugin, rarely something has improved my productivity so much. Once it's setup you can browse the log, diff files and another tons of neat things all from vim. The first thing I got myself was a shortcut to commit to svn:
nmap \post VCSCommit
Now whenever I type \post a nice buffer will be open where I can input my commit message. Saving will perform the actual commit. The plugin is pretty much straightforward to setup and use, there are some extra features you can enable like versioning info printed on the status bar, just read the doc files coming with it
In case you wanted to do more customizations and make sure nothing is loaded if the plugin isn't, there's some more work to do. In the first place you cant just put the code in your .vimrc like we've done so far: plugins are loaded after .vimrc and so you cant test for 'em. The trick is to create a function and then use an autocommand like this:
function VCSIfLoaded()
    if exists('g:loaded_VCSCommand')
        let g:VCSCommandEnableBufferSetup=1
        nmap \post VCSCommit
    endif
endfunction

au VimEnter * :call VCSIfLoaded()
Thanks to the guys on #vim@freenode.net for all the help in figuring out the above.

Publishing the lot

This step is really up to you, you can either use a svn hook like postcommit to directly deploy the file to destination, or you can create yourself a small script that checks out files from the repository and subsequently copy them to the right place.

Conclusions

I can now post from vim with all the related benefits: automatic spell-checking, nice syntax highlighting, all my beloved shortcuts and no need to leave my xterm. And SVN provides me with an instant way to publish my articles, find when I wrote what, and what corrections I did, and makes pyblosxom instantly multiuser with fine grained ACLs. And this all to the cost of a small bash script and a plugin which makes life easier but per-se aren't even necessary. Enjoy!




SpikeLab.org is a Filippo Spike Morelli copyright 2005-2008
This work is licensed under Creative Commons Att-SA License.