Why you should care about Vim

Recently I have started to use Vim for general text editing tasks, and I'd like to share my initial experiences on why would someone should try this editor.

Recently I have started to use Vim for general text editing tasks, such as blogging and in certain cases, programming. For the latter, as most of the time I spend looking at Java code, previously I have used Eclipse for almost every task, which is a really powerful editor, but sometimes felt sluggish for small, non-Java related tasks. So I started looking for lightweight alternatives to find a suitable general text editing tool.

I am still new to Vim, but I'd like to share my initial experiences on why would someone should try this editor.

What is Vim?

Another post about Vim... Why should anybody even care about Vim? Probably everyone is using a somewhat comfortable text editor already, and text editing might not seem like a really challenging task, or something that could or should be further optimized. But if someone edit text files a lot, there is a huge chance that parts of this work can be speed up by automation.

Imagine something that you are doing frequently if a given common task arises, and it requires huge amount of perception and accuracy with the keyboard and the mouse to achieve, repeatedly.

There are multiple ways to handle this situation:

  • Do manually the whole thing.
  • Write a script (or a substitution based on a complicated regular expression) that does it for you.
  • Look around how Your editor can support this situation with shortcuts, or even plug-ins.

The first version is the simplest but the slowest in the long term. The second one is good, but it usually requires a lot of testing to make the script produce the good results, and it's hard to use again on a similar problem. With the third approach one have to check if commands or plug-ins can help in the situation. Most of the time a command will not solve the entire problem, but some subset of it, so you either need to combine these functions or do some manual work to achieve the desired result. Small functions can be combined and easily applied to other problems too, and excessive testing is usually not required, as you can try everything on the fly, line by line if it's necessary.

This third approach is where Vim can help the user to automatize boring, error prone and repetitive tasks.

Powerful functions at your fingertips

Vim is modal. By default it accepts commands in Normal mode to execute functions, and the user can enter text in Insert mode. Users typically navigate and edit in Normal mode, and enter to Insert mode periodically to type chunks of text.

It has powerful functions and modifiers bound to almost every key, and these can be composed as desired making them even more powerful. If you learn a new function, you can not only use it on it's own, but in composition with the other functions. Steve Losh wrote a post about that using these commands feels like Vim is a programmable text editor with it's own (extendable) language that has different types of elements that allow to compose meaningful commands.

The language has verbs, adjectives and nouns like delete, around and word. (Vim calls them motions and operators.) Placing a cursor over a word, and pressing daw deletes the word with the whitespace around it, making the word disappear without leaving a scar in the sentence. Another noun is the sentence. The command das does the same thing with the currently selected sentence. The verb change indicates to not only delete the referred noun, but immediately switch to insert mode to let the user enter a new version of it. Combining it with the adjective inside, one is easily able to fix typos in a text with ciw. In the previous commands in the place of the w or s the user can specify delimiter characters too. For example, placing a cursor in the body of a Javascript function and issuing di{ means to delete the whole function body. It's like speaking in abbreviations to the editor.

By adding new elements to the language one can use these elements in combination with the previous ones. This is really powerful and makes learning new things much easier. If I say v is responsible to indicate visual selection, you can easily guess that vas creates a visual selection around the sentence that the cursor is in.

Of course, custom elements to the language can be added by plug-ins, for example, the Ruby plug-in allows to express Ruby related terms with the language, like methods and classes.

Custom key bindings and macros can join multiple things to make issuing complicated commands easily accessible, while the dot key makes really simple to issue the last complete command again.

One can certainly live without these features, but doing everything manually is usually much slower. If you stumble into a new problem, there is probably a way to help in the situation with the use of functions and custom key bindings.

##Turning a text editor into an IDE Also, Vim is not just a thing from the past: it has a vibrant and active community. It has continuously growing user base and actively developed plug-ins to extend its functionality. There are plug-in managers like Vundle or Pathogen to ease the organization of these extensions.

With the right set of plug-ins, Vim can be language-aware, like someone would expect it from an IDE, providing not only the above mentioned navigation and syntax related functions, but context dependent auto completion, navigation, documentation and more.

Vim

Things that you need

As I am writing this post in Vim, I really appreciate Markdown syntax highlight and English spell checker to catch errors as I write, features that Vim gives by default. Some other features are handy too, like word highlight to spot unnecessary repetition, or easy way to jump through sentences. Coding usually requires somewhat different set of tools. Copy-pasting or deleting whole lines is really common, as well as doing these operations around code blocks or function-argument lists. Besides the above mentioned context aware navigation and code completion, using version control is also essential.

It's up to the user to find the right features required for the job.

Summary

Vim has a lot of built in functionality and a large number of plug-ins to manage text and code. This makes Vim to be a great candidate to be someone's primary text editor. In addition, it can be used via terminal, which is great for using the same editor to manage files on remote servers too. Accessing complex functions in only a few keystrokes in this case can greatly improve productivity, as administering or coding can be annoying on low bandwidth if you have to type a lot.

Vim can be challenging as it works differently than most other editors, but although it requires some dedication and effort, finding new ways to use it can always be fun.

March 31, 2015
In this article