Saturday, April 23, 2011

Vim vi vim

Text editing is a common task many Linux users do day to day. Gedit, Kate, Kwrite - they are all programs which assist us in changing configuration files, or just taking notes. While today’s systems orient around newfangled graphical interfaces with mouse support, fancy sounds, and (not so) nice assistants, some have forgotten about the good old editors which were one of the foundations of the Linux OS. Before the GUI, text editors existed on the command line. Now, newer Linux users who have seldom used the terminal interface are dwarfed by the mighty command line interface and step back from using anything which has a black background with green text. While on first glance it may be scary, users of CLI based editors will tell you that it is not, and while there may be new material to learn, the result could mean efficiency and time saved for other work.

This article investigates one of the most popular editors for the CLI: Vim. This editor is distubited with most Linux distros. Created by Bram Moolnaar in 1991 for the Amiga (Yup, its that old), it was an improved version of an earlier editor, vi, hence the name Vi IMproved (which is shortened to vim). Vi, created by Bill Joy was an improvement on ex, an even older editor. Now over the generations, these tools were created to increase efficiency for text editing. Vim is no exception to this philosophy and encourages it as one of its goals.

Vim and Vi are based on a mode system. Depending on how you write and code, you may be in either insert or normal mode for the majority of your session. These modes are what throws some people off, simply because the keys they hammer away don’t appear on screen. One of the first things a vi user learns is the insert mode. In this mode, you are able to type into the document itself (which is what a text editor is supposed to do, right?) The other mode is normal mode. Now at this point, you may be thinking “Why do I need another mode when you just told me that insert mode does what I want it to do?”. This second mode is quite important for efficiency in coding, and also allows you to save the text to the drive. The rationale behind the different modes is for faster processing of the work. A user is able to get into insert mode a number of ways. To name a few, by pressing o,O, a, A, i and I, the user is placed into insert mode. The different keys and their uppercase counterpart have different functions though. While all of them put you into insert mode, they place the cursor in different locations. O and o opens a new line above and below the current position respectively. I inserts places the cursor at the beginning of the line, while i has the cursor insert before the character. A appends the line, placing the cursor at the end of the line while a puts it after the current character.

There are many commands to learn before a user is able to code faster than the average non-vi user, but don’t let new knowledge push you back. All the commands do something slightly different and can mean moving to another location in the document or perform macros. The keys which are mapped to commands are also smartly located. The more often the command is used, the closer the key is to the home row (on QWERTY keyboards). This reduces the time needed to move and insert text. Don’t believe me? Go onto Youtube and search Vi or Vim, many of the videos shown are not in time lapse, the users are merely using the full power of Vim.
With all this said, I encourage you to go out and try Vim. Much of my coding is done within this spectacular program and I have noticed an increase in the speed of my coding. Have fun with it too, forcing yourself to learn the complete dictionary of commands extremely hard in one day. If you don’t know where to start, vimtutor (just type that in) is a interactive tutorial which will bring the user up to the basics of Vim. Now, as a final note to the reader: Watch out for those sneaky EMACS users, they will try to convert you to the dark side...

No comments:

Post a Comment