Maintain Color-Coded TODO Lists in Vim

Many Vim enthusiasts use Vim for pretty much all text manipulation in their daily lives. However, the plain text nature of this fantastic and powerful editor sometimes leaves a little left to be desired. For example, it would be nice to have your editor color code certain items in your TODO list for you, e.g. one color for items that are done in your list, another (hopefully a more provocative one) for those that aren’t done. I recently discovered a trick how to kind of make that happen in Vim, and I am sharing that here.

The first thing you need is some type of a marker in front of your rows that you want highlighted, so that Vim has a way of doing a RegEx matching against them. E.g.

[TODO] Write a blog post
[DONE] Goof off
[Nice to Have] Read a book

Here I have marked my rows with [TODO], [DONE], and [Nice to Have]

Next up, you need to invoke the following command in the command line mode:

:highlight MyGroupTodo ctermbg=red guibg=red
:let m1 = matchadd(“MyGroupTodo”, “^\[TODO.*”)
:highlight MyGroupDone ctermbg=green guibg=green ctermfg=black guifg=black
:let m2 = matchadd(“MyGroupDone”, “^\[DONE.*”)
:highlight MyGroupNTH ctermbg=cyan guibg=cyan ctermfg=black guifg=black
:let m3 = matchadd(“MyGroupNTH”, “^\[Nice to Have.*”)

Here is a screen capture of what it looks like in my current color scheme. Keep in mind that the appearance might be different based on what color scheme you currently have enabled, and you might have to change the colors of the matches to better suit your tastes and your color scheme. Furthermore, you can put these highlight and match commands in your .vimrc so that you don’t have to keep doing it over and over.

Being a visual person I appreciate colors and the ease of distinction that they provide. If that’s you, and you use Vim, then this is how you can do it. Look up :h match inside Vim for more detail. Notice that, in contrast to the example in the Vim help, I have used more specific regular expressions so that the entire line is highlighted – you might or might not want that.

Using Vim to semi-automatically ingest contents of files

The title is vague. The post has to do with the workflow I came up with and used for something today. Let me just walk you through a hypothetical scenario and how to solve it. The tutorial is *Nix specific.

Imagine you have a bunch of files under a directory, with some plain text content, and you want to ingest the contents of the files into a single file. That by itself is easy to do with a simple $ cat * > target_file. But how about if you want to actually have the names of the files along with the contents, so you know where the contents came from, and maybe you also want to edit some of the content out and perhaps also, you most likely want the files to be ingested in some sort of a temporal ordering. For the sake of this example let us assume you want the files sorted in the chronological order – you want the contents of the oldest file to appear up top.

Can’t think of a scenario? How about having a bunch of notes, each written once a week, with the dates being the names of the files – and you want to conflate the notes into a single, master-notes file for the whole year, and you want the contents in the order that they were created, with the notes created on the oldest date appearing first.

Here’s how you do it with the combination of a shell and Vim.

Step 1: $ ls -ltr > master_file.txt – this will put a list of the files in the chronological order into the master_file.txt file. Note that just the ‘t’ flag is for the reverse chronological order, and you need to revert that using ‘r’

Step 2: Open the master_file.txt in Vim

Step 3: Get rid of the stuff you do not want using visual blocks or whatever Vim magic you know in a few keystrokes

Step 4: Also get rid of the name ‘master_file.txt’. Now you have a raw list of all the files whose contents you want. and the files are listed in the chronological order

Step 5: Start recording a macro (say, qa)

Step 6: Place the cursor at the beginning of the first line. Press yy to copy the first file’s name

Step 7: Press o<Enter> to make some space

Step 8: Hit <Esc> followed by :r <Ctrl-r>” to paste the contents of the filename you just copied from the default register

Step 9: You might have to press <Del> once to get rid of the ^M character that gets pasted

Step 10: Hit <Enter> and the contents of the file will be in the buffer at the appropriate place

Step 11: Place the cursor at the beginning of the next file’s name, and stop recording the macro (q)

Step 12: Got 1000 files? 1000@a (or whatever register you recorded it in) is your friend!

And trust me – if you are a Vim user, it’s much faster and simpler than it seems here in the long description. If you are a newbie, trust me – it’s must faster, simpler and funner than it seems.


Productivity Boost with Vim

It is one of those moments when you realize that the few hours you spent learning a technology or actually tried to grok a book paid off and you reap the rewards of that investment. In particular, the technology happens to be vim. I am sure everybody’s favorite editor has this capability too, or something like that, or something fairly close, or even better – but the point I am trying to make here is that something that would have taken me half an hour previously took me 2 minutes to finish because I took the trouble to learn vim properly using Practical Vim.

Problem: Convert 25 citations from LaTeX format to Web format

Concretely, given several citations of the form

\item Author 1, Author 2, …, Author n, {\it Title of the Paper}, In Proceedings of Conference, Address, Year


<li><p>Author 1, Author 2, …, Author n, <font color=”darkred”>Title of the Paper</font>, In Proceedings of Conference, Address, Year</p></li>

So how do we do it? I did it in two steps.

[Step 1] Record a macro, perform adding <li><p> at the start and </p></li> at the end of each sentence, and repeat the macro for each sentence in parallel. Concretely,

qa0daWI<li><p><Esc>A</p></li><Esc>qjVG:norm @a

and there you have it!

[Step 2] Look for the regex that matches the tags around the titles of the papers, and then replace them with the desired text. Concretely (broken down into steps for clarity):

/\v\{\\it\_s(.*)}<Enter> (search for the pattern)

:%s//<font color=”darkred”>\1<\/font>/g<Enter> (reuse the pattern found in replacement)

And voilà, it works! Brilliant 🙂

My first Vim Theme

We all love different code editors, don’t we? Well, my all time favorite is Vim, hands down. I’ve used it for quite a while, and I always thought I could do magic with it. And then came along Practical Vim, that taught me how to really do magic with Vim. That has been followed by VimGolf, that helps me keep sharpening the saw and at the same time have some fun. And to be a bit smug, I am not too bad at it. Of course, there is still a lot to learn and practice. Like a musician, over and over and over again until I can play the ‘chords’ and ‘melodies’ in my sleep.

All these things notwithstanding, I’ve always been an aesthete. Things have to look pretty and neat. To this end, I thought it would be a good idea to get into the world of theming, and see if I can whip together a simple theme for Vim. The idea spawned from me having stumbled upon Adobe Kuler, which helps set up color schemes for Web sites and such. Lo and behold, before I knew it I was messing around with a theme that I liked (which I modified from all-blue to blue-green-red), and mucking around a .vim color file, putting together color values for different components, all complete with some finer points such as underline, bold and italics.

What came out of this 30-minute adventure, I present to you here. Some screenshots are attached to the repository. Take my word for it, making your own theme is not that hard. Hope you have fun.

The capabilities of Vim

Many have heard of the good things that Vim can do. Many propose and maintain that once you take the time to actually learn Vim, it will be your last editor, ever, because you’ll never want anything else. Many claim that nothing can beat the awesome combination of simplicity and power that is Vim.

This is a neutral description of some Vim stuff that I learned recently, and I’ll let the readers decide whether they conclude the same as the others above.

Apart from learning some built-in commands and keyboard shortcuts in Vim from the ground up, like here, I think it is also essential to actually learn some VimScript to be able to fully leverage the power of Vim – for example here. Finally, after having familiarized yourself with these, the next step would be to actually start looking at some of the plugins that exist in the Vim repository. Installing them is mostly easy – some of them can just be copied verbatim to your own .vimrc file.

Imagine being able to insert something at the end of every line in a block of code in a few keystrokes. Imagine being able to delete, find, replace, edit anything in a line without using the mouse, and without pressing the arrow keys at all. And it doesn’t end there.

Consider, for example, how helpful an automatic code/ snippet generator can be – something that enables you to, for example, press for followed by two tabs and automatically inserts for(int i = 0; i < count; i++) { // Code }. Imagine that on top of that, you place the cursor on top of the i and type another name, and the name changes in all the three places where it appears. Cool, isn’t it? Think of the time it saves, and the errors that it precludes. And, you guessed it, a plugin like this already exists, and you can even see it in action in a video before you decide to install it!
Happy Vimscripting!