Vim on a whim

December 20, 2018 - 8 min read

Until a year ago, I was an ardent user of Sublime Text, my favourite feature being multiple cursors. When it released, Sublime Text was one of the first modern editors which was fast, feature-rich, and extensible. The multiple cursor feature was so cool that, sublime text became an instant favorite.

Things were fine, until I started working on remote machines, especially a bunch of Raspberry Pis, via SSH. The sublime text setup quickly became cumbersome and the whole saving and re-uploading after every change didn’t scale well. Following suggestions from a lab mate, I started exploring Vim and was pleasantly surprised by its abilities.

Why is Vim awesome

Vim is ubiquitous. Open up a terminal on any linux distro or a Mac, vi and there it is. Most people avoid Vim, due to the initial learning curve it features. Why the hell should I hit ‘i’ before typing anything ? How do I exit this? Weird right ?? Most people are also unaware that Vim has pretty much all the features you want out of the box:split screens, auto-complete, run commands without exiting the file etc.

Cross the initial learning curve, and you will have at your disposal a powerful editor. Vim is also highly extensible. There are several plugins and themes you can install on Vim, to customize and add features as you like.

Speed, customizability, features are common to a lot of other editors. What sets Vim apart is that, it does all this, while being extremely lightweight on resources. The rest of the article will focus on this aspect specifically.

Performance comparison

I decided to compare how vim fares against Sublime Text and Visual Studio code, the 2 popular editor counterparts. I test the editors on some very basic fronts:

  • Memory used for opening a file
  • Time taken to open a large file
  • Memory used & time taken for a large number of find and replace operations

Opening a File

Like me, if you have never bothered to check how much memory an editor uses to load a file of few lines, you’re in for a surprise. How much memory does the editor use for opening the below code ?

def main():
    print ("Hello world")

if __name__ == '__main__':
    main()

Opening a file

Vim takes 3.3 MB, Sublime text 75 MB, Visual Studio Code takes a whopping 298 MB. I’m running Sublime Text and Visual Studio Code stock, without any extensions. That’s a lot of memory for loading a file with 5 lines of code! What happens when I open a larger file ? A 33 MB IMDB dataset only makes it worse. Sublime Text shoots up to 430 MB and Code takes 590 MB, while Vim just increases by 1 MB. That is a lot of RAM for just loading a file. While, Vim doesn’t consume much RAM, it takes the longest to open the file with 23 seconds. Sublime Text takes 13 seconds and Code opens it in just 9 seconds !

Find & Replace

Next, I performed a find and replace with a large number of matches and check time taken and the memory used for the same. I conduct 2 sets of tests, the first one involves 65986 substitutions and the second has 4872237 subsitutions. I also undo the changes and see what happens.

Time for find & replace

Memory for find & replace

For the first set of find & replace, both Vim & Sublime Text take a meagre 3 seconds, while Visual Studio Code takes 20 seconds. However, the difference in RAM usage is huge. Vim does this while using no extra RAM ! However, Sublime Text takes an additional 40 odd MB bringing it’s total usage to 474 MB while Code takes a solid 2GB of RAM for this !!!

The second set of replaces tell a similar story. Vim takes 9 seconds while using only 4.3 MB, Sublime Text takes a humongous 43 seconds while using 924 MB. Code uses 2.2 Gigs and takes 18 seconds to finish, but kept crashing and took me several attempts to get it to work. Let’s set aside the time taken for a bit here, the memory used for a simple find and replace is enormous and Code outright crashing is not acceptable at all !!

When I try to undo the last set of replaces, things get worse. Sublime Text crashes !! It simply hangs and the memory consumption keeps increasing, with the butterfly wheel of death, and I had to force stop it when the RAM usage went past 4 GB. After several failed attempts to get Sublime Text to do this, I gave up. Code also posed a similar story, crashing very frequently (not as bad as Sublime) and after a couple tries, managed to finish undoing in 18 seconds which is a second faster than Vim which took 19 seconds.

Vim is light

Conclusion, Vim is very fast, while using very low resources. To be fair, editors are designed for editing small source code files, and are not exactly meant for handling large datasets. However, it’s a neat little test to show how the editors fare in comparison. I must add that once in a while, I do find myself opening a big CSV file or txt file to make small changes and I would definitely like my editor to be responsive.

Note For resource usage, I’m tracking all child processes used. I find vim’s usage ridiculously low, which makes me wonder if I’m missing some background process. If anyone knows if vim spawns some other process please drop a comment below.

Preview of some cool features

For those of you who shied away from Vim, you probably thought it lacked a lot of basic features. I certainly didn’t know of the capabilities until recently. Below, I show a few of the nice things that vim can do out of the box.

Split panes & Tabs

split panes

For me, split panes is a must have feature for any editor, as often, I’m editing multiple dependant source files at once. Vim supports both split screens and tabs, along with sessions. In normal mode :split <filename> opens the file split horizontally with the current file and :vsplit <filename> splits it vertically. You can adjust the split size, maximize current pane and much more.

Vim supports tabs as well. To open files in tabs, in normal mode enter :tabedit <filename> To switch between tabs enter <# of tab>gt.

Multiple Cursors equivalent

split panes

Multiple cursors is one of the coolest features that Sublime Text introduced. Although you can’t have multiple cursors in Vim without using plugins, you can perform the equivalent action in vim using cgn and . commands.

The cgn command allows you to search for a specific text and change it. Hitting n shows the next instance. To edit all occurrences of a specific word, follow the following sequence.

  • Search for the word using /<word> and hit enter.
  • Next hit cgn to change the word while instructing vim to look for other instances. cgn puts vim in edit mode. Type the edit you want to perform on the word.
  • Now, switch to normal mode, and hit n to find next matches and if you want to apply the change, press . or keep pressing n to skip instances.

While this seems cumbersome, it’s roughly the same as sublime text, but in reverse. In sublime, you hit <ctrl-d> to insert a cursor at each occurrence of the selected word and <ctrl-k> to skip selection. Once you have the selections, you then perform the edit.

Bookmarks

When editing a long source file I often need to go back and forth between code blocks. This is a nifty feature which allows you to do this without remembering line numbers.

Simply go to the line that needs to be marked and in normal mode, enter m<a-z>. The lower case letter is the bookmark for that line. When you need to jump to this line, from anywhere, simply press `<letter> to jump to the corresponding line. Using upper case letters creates global bookmarks which allows you to jump across files. To see a list of all your bookmarks, type :marks in normal mode.

Shell commands

Vim allows you to run shell commands without having to close the file. Simply enter :!<command> in normal mode. So if you want to run make for example, you can type :!make and check the output without closing and re-opening your files.

With Vim 8, these commands can be run in the background without blocking your work using this plugin. The asyncrun is a cool plugin which runs commands in the background and shows you the output in a quickfix window, without interrupting your editing workflow.

Remote editing

I work with Raspberry Pis a lot, and this feature is a killer one for me. Vim supports remote editing of files out of the box. I can simply open up a terminal and type

vim scp://<user>@<host-ip>/<path to file>

To make things even more convenient, create an ssh config file with the host and rsa information and voila.

Auto complete

split panes

This one is a bonus I didn’t know about. In insert mode, hit <Ctrl-n> to see a list of auto complete suggestions. The auto complete is pretty basic, and provides results only from libraries or header files you included in your source code. For more advanced auto complete, there are several awesome vim plugins that do the job.

Should you use Vim ?

Maybe. It’s up to you. This article is not for convincing everybody to switch to Vim. Choosing an editor is based on personal taste, and several people might prefer a WYSIWYG editor which allows you to move your mouse around. I like not having to take my hands of the keyboard, and found out that Vim had all the features I need and if not plugins which do the job.

Both Sublime Text and Code are wonderful editors and I myself use them occasionally. The conclusion is it’s worthwhile to learn Vim and if you were hesitant to try it because of the learning curve, you should give it a try. Vim commands are grammar oriented and once you get accustomed to the basic commands, it gets super easy and natural.

If you enjoyed this post, feel free to tweet about it or share it on Facebook.

Comments