iThinks

Tag Archives: vim

The other day I wanted to open a scratch buffer to write some temporary code and the pass it on to a repl to test it out, the idea was that I could modify it as needed and resend it back to the repl and see the changes, helps to debug easily.

So I just wrote out this simple code here that allows me to launch a scratch buffer in different ways, but the difference is I can set various options for it while creating it. First lets have a look at the code :

Now this allows me to open a scratch buffer with various options, like I could call :Sedit ft=javascript and that would open a scratch buffer with filetype javascript (and hence show the right syntax highlighting). The cool part is all these options are set local to the buffer and can just be space separated to the command. The buffer will not be listed in :ls and after you :q it will be wiped out completely from vim. Couldn’t be any simpler!

If you are used to using the quickfix list in VIM there are times when you want to filter certain things out of the quickfix list to narrow it down to relevant results that you need. Rather than firing the quickfix command all over again with a refined expression, it is easier & often faster to simply remove items from the list. I wrote this simple vimscript to do just that.

Using the above now allows you to filter the quickfix list by calling :QFilter pattern. By default it filters the list to keep results matching the pattern but if you call it :QFilter! pattern. it will remove results that match the pattern.

Ruby 1.9 introduced a new json style ruby hash syntax. Although we can use this to define static hash objects and puts a limitation on the key we can use, it’s a fairly neat syntax nevertheless. However I work on several projects, both new and old and hence the new syntax is not always readily available.

Ruby

1

2

3

4

5

# Old style hash

hash={:a=>'test'}

# New style hash

hash={a:'test'}

To demonstrate the sheer power of regular expressions & VIM, here’s a tiny snippet that allows you to quickly toggle a ruby hash syntax from one form to other.

My development environment setup consists of several tools, however the 2 most used and essential tools that have a great impact on my workflow are tmux & vim. Since I often work on several projects, I find it best to work on different vim sessions in separate tmux windows.

However tmux by default only shows the name of the currently running process in the title and that can often lead to confusion as to which tmux window is which project. To solve this issue I came up with a very simple vim script that utilizes tmux’s powerful API to automatically rename the tmux window appropriately and hence make it incredibly easy for me to know which tmux window is for which project.

Here is the vimscript, that you can put anywhere within your ~/.vimrc :

Recently I picked up some minor configs for ruby from Tim Pope’svimrc. Here’s the code :

Vim

1

2

3

4

5

6

7

8

autocmdFileTyperuby

\ifexpand('%')=~#'_test\.rb$'|

\compiler rubyunit|setl makeprg=testrb\\"%:p\" |

\elseifexpand('%')=~#'_spec\.rb$'|

\compiler rspec|setl makeprg=rspec\\"%:p\" |

\else|

\compiler ruby|setl makeprg=ruby\-wc\\"%:p\" |

\endif

What this does is given the filetype is Ruby, it correctly sets up the compiler and makeprg such that it allows you to run your tests via :make! This is pretty neat I must say :), because it correctly loads the quickfix list with the errors and I can actually traverse them in order and fix them. All this without needing any plugin whatsoever :).

Ever since I started using this, I always wanted to mark the lines with errors / warnings with nice signs to highlight lines with errors instantly after I ran :make or :lmake, something similar to what Syntastic does but not just for syntax errors, actual test run errors. As I searched for similar plugins, although I found a bunch of them, I didn’t find them to be very intuitive or simple enough.

Hence I started exploring through vim’s help in an attempt to trying and understanding how I can tap into quickfix & location lists and process them. As it turns out, it’s quite simple and I ended up developing vim-markify overnight.

I’ve made it quite flexible and it works quite well with all quickfix commands – :make, :lmake, :grep, :lgrep, :grepadd, :lgrepadd, :vimgrep, :lvimgrep, :vimgrepadd, :lvimgrepadd, :helpgrep, :lhelpgrep, by tapping into the QuickFixCmdPost autocmd event which triggers right after you run any of the previously mentioned quickfix commands. Although you can choose to disable the autocmd by putting let g:markify_autocmd=0 in your vimrc so that it doesn’t do this automatically and you can trigger :Markify, :MarkifyClear or :MarkifyToggle commands or their corresponding mappings <Leader>mm, <Leader>mc or <Leader>M which can of course be changed. And I was able to achieve all this under 170 sloc :). A lot more things are configurable, I would encourage you to check :h markify.txt within vim or refer to the code at Github.

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.

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 Pope – cucumbertables.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 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 :

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 →

I have been an ardent VIM user for almost 4 years now, and it’s been a while since I have been wanting to write this Blog. My motivation is not to showoff or to start any flame war against any other popular text editors that may or may not be better ;), but to simply showcase the things I like and have come to appreciate in VIM, and show why you should seriously consider it when deciding which programming editor you should use. Hopefully I will be able to do justice to VIM – the Ultimate Programmer’s Editor.

The beauty about using VIM is that it converts the task of text editing / manipulation into a programmable activity. It provides you with a framework to work with your text in a way which allows you to think of it in terms of simple patterns, patterns which you can repeat, store (macros), copy & even combine together. This also makes the process of learning VIM interesting & fun! Add to that the fact that you can easily extend your VIM with a wide variety of plugins & themes contributed and shared openly by a vibrant community is just icing on the cake!

Initially when you start out with VIM it feels quite alien and ancient to say the least, also because the default configurations in VIM aren’t that optimal, so it might take a few days to get a hang of things, but once you do, it will motivate you to do things faster. The more you do, the more you obsess on doing things faster and with fewer and fewer keystrokes as possible (you might want to have a look at http://vimgolf.com for a bit of fun in that respect :)), the more you discover what VIM has to offer.

Over a prolonged period of usage, slowly and steadily VIM commands creep into your muscle memory and the vastness of the abilities and features of VIM which overwhelm you in the beginning, come to you as second nature. This is my attempt to describe various elements of VIM that are now in my muscle memory and use them on a daily basis.Continue reading →