Monthly Archives: March 2013

VIM Better FoldText

Update 3: Improved the code for extraction of fillchar from ‘fillchars’ option and taking ‘foldcolumn’ into account.

Update 2: Fixed the regex for removing the fold marker from the displayed text by splitting the ‘{{{‘ in the regex to ‘{{‘. ‘{‘ so that it is not treated as a fold marker by vim. Thanks to Amadan on reddit here.

Update: Fixed ‘NeatFoldText()’ to get foldchar from ‘fillchars’ settings.

Initially when I started using VIM, I had borrowed configs from my mentor Vagmi Mudumbai (to whom I am eternally thankful!!!).

He used folding in VIM actively. I on the other hand though, barely got around using VIM itself, found folding to be quite confusing and hard to work with. The first folding mapping I learnt was ‘zR’, which is for recursively opening all folds in the current documents, so I don’t have to deal / worry about them. I became so habitual to using ‘zR’ that one day I decided I was better off without folding altogether, after all I was clearly not using folding anyways. That day I removed folding related configs from my ~/.vimrc. Much later as I became more comfortable using VIM, I came back to folding and have not looked back ever since!

I have always thought though that the vim’s default folding look and feel could use some beautification, and although I have been aware about ‘foldtext’, I never really got around to understanding how to take advantage of it to customize things like I wanted.

After quite a bit of effort and quite a few iterations, I now present to you my ‘NeatFoldText()’, a vim function that makes the fold line look really impressive, meaningful and neat at the same time! It puts focus on what’s most important (the text in the start of the fold). And right aligns the number of lines in the fold. Also formats it to fixed size which makes it look neat and consistent.

Put this in your $VIMRC (~/.vimrc) :

And this is how the fold text look like :


This example should also prove to be a great starting point / example for those who wish to tweak the folding text further.

VIM Table Mode

UPDATE 2: Released v 2.0 of the plugin. Check github for details.

UPDATE : I have added more features to the original plugin, showcasing some core features in a new demo (Part 2).

Recently a friend of mine showed off emacs table mode to me and to be honest I felt a bit jealous :). Hence I began to search for existing vim plugins that might offer a similar functionality and I did find a plugin vim rst tables, which is a ftplugin that aids the creation of tables within rst files and allows you to format existing content into a table, although this plugin came closest to what I was looking for it wasn’t quite there. Then I found a very interesting & simple plugin shared as a gist by the infamous Tim Popecucumbertables.vim which I found quite impressive.

I began experimenting with this in an attempt to make it a bit more comprehensive and to allow for better formatted tables creation and after hacking on it for a couple of nights I came up with the table-mode vim plugin.
Continue reading

Vim Railscasts Colorscheme

VIM in my muscle memory is one of my popular posts, the day I published it, I attracted over 3,200 unique views in a single day, mostly from Hacker News and Reddit Programming. Although it is an extremely densely packed post with lots of information, I hope it has helped some to appreciate VIM like I do.

In my several years of VIM usage, I have tried several colorscheme’s, though I have come to like only a select few. One colorscheme that I have liked the most and have always ended up coming back to is the Railscasts theme. There are 2 versions of this script :

  1. – the original GUI only theme by Josh O’Rourke.
  2. – as the name suggests, this is an extension of the original theme by Josh O’Rourke with sane 256 color equivalents for terminal vim written by Nick Moffitt.

I have tried both versions, but there are several inconsistencies between them. I also tried using Josh’s version along with csapprox, but I did not like some of the color conversions made by it. It was simply not ‘coherent’ enough for my liking between the gui & terminal. Also some of the elements of Nick’s version in the terminal were annoying, for eg.) the default (Normal) background for terminal in Nick’s version was transparent. I use a terminal with transparency so I can often read stuff from my editor in the background or even the browser, but this does not work well with my editor, when I am coding I would not like the distraction of anything behind my code.
Continue reading

Soulmate Rails

One of the most common things to do in any standard rails web applications is auto-completion. One interesting solution to this is by using redis’ (which is blazing fast) sorted sets, storing key value pairs for partial matches. For example, if we wish to index the word ‘Dhruva Sagar’ (my name), you can simply create key -> value pairs of ‘d’ -> ‘Dhruva Sagar’, ‘dh’ -> ‘Dhruva Sagar’, ‘dhr’ -> ‘Dhruva Sagar’ and so on. Since hash lookups are of constant time complexity O(1), they’re very fast and hence the completions are too! Combine this with using sorted sets of redis to easily order / sort information on a specific criteria and you have a blazing fast solution. Of course the actual implementation can be further optimized by means of caching query results and make the system perform even further.

Soulmate is a cool gem by the guys at SeatGeek that provides a pretty cool implementation of the same. However it’s implementation wasn’t quite a good fit for my case since I wanted to work with a rails application and didn’t want to either host a separate server or mount the provided sinatra app inside of rails. I came across this nice post by Pat Shaughnessy – Finding your soulmate: autocomplete with Redis in Rails 3.1 which showcases how one can use the Soulmate gem and use it’s API to roll out your own auto-completion engine within your rails 3.1 application very easily.

This inspired me to fork Soulmate and extend it to build a Soulmate Rails gem over the weekend which provides a clean and intuitive API to do this from within Rails. Please do check it out, star it, fork it and use it to your hearts content :) . I’ve released it in alpha phase right now but it is fully usable with a reasonable test coverage. I’ll be contributing a lot to this, hopefully you find it useful too.

UPDATE : I released the first stable version 0.2.1, it should be usable as is without any issues.