Posted by: macoy | May 3, 2008

To vi Or Not To vi

I dunno with you guys, but I’m more comfortable coding using a text editor, whether it be shell scripts, or PERL, PHP, HTML, JS, or CSS files. Despite the proliferation of IDEs in every platform, I usually end up writing code in vi or vim.

It’s also encouraging to know that a lot of developers also share my preference of coding from scratch using a text editor instead of using IDEs. Read more in this article by Lifehacker, which has featured the Best Text Editors in one of its articles. It’s also interesting to note that vi turned out 2nd in the article’s poll results. The first placer as of press time is Notepad++ which is a Windows-only application. So for cross-platform text editing, vi/vim is still the popular choice.

My adventure with vi began when I became a full-time staff at the University Computer Center. I was coding in PERL then (PHP was still in its infancy) and doing some shell scripts. As a Linux sysad, you’ll eventually start writing shell scripts to make your life a little bit easier. But at that time, I was starting to get used to pico, which I have grown accustomed to when UP was still using pine for its emails (that was the pre-UP WebMail era).

I knew then that using vi is one of the basic things a sysad has to learn because vi is one of the programs that is present in every Unix-like operating system no matter how bare-bones the installation is. I was also inspired by how a senior staff uses it so effortlessly and even running shell commands from within vi.

One thing about vi is that looks deceivingly like any other editor but when you first try it out, you?ll be at a loss trying to type “normally” as you would. That’s really the biggest hindrance one has to hurdle if he/she is to learn using the program. But this is also the good side of it – if you already know the commands and shortcuts, you can put them to good use and take advantage of vi’s very powerful features. IMHO, vi is still the most powerful text editor in any platform. Below is just one of the nice things about it that I’ve grown to love and use very frequently:

  1. highly customizable (you can set it to your personal preferences, and save it to .vimrc or some similar config file, depending on the platform or distribution) and at the same time you can edit settings on-the-fly (such as colorscheme, tab width, toggle auto-indent or line numbers) without using the menus – just use the ex commands.
  2. you won’t be needing a mouse virtually so coding and typing is unobstructed. the assumption is, you have to know the most common shortcuts to get around the editor
  3. 26 named buffers – this means you can store up to 26 selected text, aside from the default unnamed buffer. no other editor could do that, not until just recently. i’ve seen other gui editors like Smultron that implement “drawers” to store snippets of code on the side.
  4. highly customizable search and replace using Unix regular expressions. that in itself is powerful enough. this is a time-saver for me especially when I migrate databases from CSVs and parsing files, without using a single line of code, just plain vi.
  5. quick auto-jump to line numbers. if you’re a developer, this is very useful when debugging scripts (“error in line number”) imagine what a timesaver this is.
  6. mouse-less operation. the mouse is a good tool especially when dealing with graphical interfaces. but when dealing with text, and just using the text editor for coding, using a mouse to select text, to jump from one point to another, access the text-editor’s menu bar/icons, etc. can drastically slow down typing/coding.
  7. vertical text selection (using visual block), deleting, cutting and pasting. again, when formatting or transforming text, this is very useful.
  8. delete, jump, copy, paste, insert characters or text in multiple instances. say you want to put exactly 120 dashes (and you don’t want to count them after, just to double-check), or delete the next 5 words or copy the next 30 lines. that can be easily achieved using vi without much effort counting and double-checking.
  9. easy pairing of code block markers. short of code-folding (which will be very kewl if implemented in vim), finding the closing or opening bracket or parenthesis of a given code block is a snap in vi.
  10. syntax highlighting. actually implemented in vim, and there are various color schemes to choose from.
  11. Split screen into multiple windows. This was very useful back when there were no tabbed ssh sessions, and is still very useful when you’re accessing servers via SSH clients.

That, and a lot more can be done using vi. That’s why in every platform (Windows, Mac and of course Linux and *nix platforms) I always install vi and use it instead of Notepad in Windows, Textedit.app or Smultron in Mac, or pico or joe in Linux, and even instead of IDEs/WYSiWYG editors such as NVU, Dreamweaver or Eclipse.

Over the years, I’ve found myself using vi even in writing non-technical notes, such as meeting agenda, list of things to do, or just drafts of blog entries. I’ve also installed in a previous Mac machine that I was using a global plugin to enable vi features from within other Mac applications such as Smultron. Because mind you, once you’ve grown accustomed to vi’s powerful features, you’ll always be finding them in other text editors. Now I don’t have to install Smultron anymore because I use vi 90% of the time when typing or editing text (the 10% is when I’m using OpenOffice.Org writer for formatted documents).

To those curious about the vi program, you can try out these vi flavors in the Mac and Windows platform which I have been using myself:
- MacVim
- gVim
- gVim Portable

Here are some vi online tutorials to get you started with:
- Editing with gVim
- Mastering the vi Editor (University of Hawaii College of Engineering)
- Introduction to Vi (Arizona State University Department of Mathematics and Statistics)

And here are some vi online reference:
- vi Reference
- vi Reference Card (PDF)
- VI (Visual) Editor Reference manual
- Unix Manuals – vi reference


Leave a response

Your response:

Categories