Friday, 1 March 2013

Why use Vim?

Originally published: 2010-03-21 on my old blog

Why use Vim?


Some of you may have stumbled upon posts saying something like "real programmers use Vi or Emacs", so let's have a look at why anyone today would want to use a 30 year old editor.

For the purposes of this article, and personal preference I'll concentrate on Vi rather than Emacs - the war between those two factions is another book on its own.

History:


Vi (pronounced vee-eye) was born in 1976, by Bill Joy, who was looking to create a useable multi-line editor for the Unix operating system and its main successor, Vim (Vi Improved) was born in 1992, written by Bram Moolenaar. Vim improves on Vi by adding many new features, especially those to support programmers (such as syntax highlighting), as well as being almost completely backwards-compatible with Vi. Vim is free, and open source software, although users are encouraged to donate money to Uganda by registering/sponsoring Vim - a model known as "CharityWare".


Why does it matter?


If you spend any significant time at a computer dealing with text, and you are a good typist, you will almost certainly benefit from using a more advanced text editor. Many of the people reading this article are likely to be using a computer and editing text around 8 hours a day. In other words even if you only get 10% better at manipulating text, you'll gain nearly 300 hours more spare time every year just by improving your editing skills. Vim is also completely keyboard driven, so you will save time and risk of injury by not constantly swapping between keyboard and mouse for selecting text and navigating the cursor. It's this improvement that I think is worth the steep learning curve of an editor like Vi.


What's wrong with my normal editor?


I'm not going to stand up and say that whatever you're using now is useless and terrible, because it probably isn't (unless you're still using Notepad of course! - in which case at least try NotePad++). Most modern editors also support syntax highlighting and autocompletion for many programming languages, however Vi has matured over many years, and is almost infinitely extendable and customisable. You can also almost guarantee that whatever system you use (or are forced to use), it'll either have Vi or Vim installed, or it's a short download away. One of my main reasons for learning it was wanting a common set of skills I could guarantee i'd be able to use anywhere, on Windows at work, on Linux at home, and over SSH to my hosting server. Vi has also taught me more about regular expressions, which have helped me many times in my professional life.


What does Vim do?


Among many other things:


  • Supported on almost any operating system
  • Syntax highlighting for hundreds of different programming languages
  • Repeat any action using the '.' key
  • Powerful regular expression support
  • Excellent integrated help system
  • Ability to interact with the command line without leaving the editor
  • Multiple cut and paste registers
  • Infinitely extendable and customisable

If you want to know more, take a look at Vim in 6 Kilobytes, Why oh why and the Vi lovers page


So how does it work?


Vim is different to most other editors because it uses a concept called 'Modal editing'. This basically means that keys on the keyboard carry out different functions depending on what mode you are in. The two main modes are 'Insert Mode' (allowing you to enter text like any other editor), and 'Normal Mode'. Normal mode is the default mode which Vim will start in, and it allows the keys on the keyboard to carry out various cursor movements and functions without having to resort to the mouse and to toolbars and dropdown menus. From normal mode you get get into insert mode by pressing the 'i' key, typing text and then pressing Esc to get back to normal mode. This was done for two main reasons, firstly a lot of early keyboards didn't have separate cursor keys (and even if they did you have to take your hands off the keyboard to press them), and secondly the recognition that when editing text, especially source code, you will tend to spend over 80% of the time reading and navigating through the code, and only a short amount of time actually entering new text. I'm not going to type thousands of words teaching you every command there is, as many people (not least of all the integrated help - type :help to get to it) have already done it.


You won't be able to pick up these commands and be productive with Vi after just a few hours, or even master it in a few weeks, but the pay-off is huge once you have mastered the basics. For some examples, see the videos here, here and here, the quickstart / cheat sheets here and a whole book here.


Integration:


Once you get used to using Vi/Vim you will find it quite difficult and frustrating if you need to edit text without it. Several products have been made to address this, including ViEmu for Visual Studio, and even a Vi Mode (Vimperator) for Firefox. These allow you to interact with other applications using most (but not all) of the niceties you're used to in Vim. The person who wrote ViEmu also offers products to integrate with Microsoft Office and Outlook. You can also use the "It's all text" plugin for firefox to allow you to edit in Vim straight from the web.


Emacs vs Vi


I won't go into it too much, but Vi is typically smaller and faster than Emacs, and is installed by default on more systems. Emacs has more of a "kitchen sink" feel to it, and I feel some of its keystrokes are particularly difficult (for example the long stretch to press Control-Y to paste text). However having said that, Emacs is also very powerful, and if you can't get on with Vim it's definitely worth a try.


Summary


These are the reasons why I have chosen to invest time in learning this editor, and hopefully I'll have convinced a few of you to do the same, or at least give it a try. You will only really appreciate the power of Vim when you watch an expert editing with it, see the videos linked above for a good example. Oh, and if you can't yet touch type, you'll be far better off learning that first! Enjoy.

No comments:

Post a comment