A discussion that mentions the vi editor usually fuels the fatuous quasi-religious war driving the debate between whether or not it’s better than Emacs. This post is not about which one is better and it does not even cover Emacs. Truth be told, I do not have a great deal of experience with Emacs, and I’m sure it has its own merits and appeal to an audience with specific needs. The only argument being made is that Emacs is not commonly found by default on most Linux systems, so depending on the circumstances, it’s a smart move to get comfortable with vi. The benefits of learning your way around vi is the focal point of this topic, including how to defeat the universal headaches associated with it through good old fashioned enlightenment.
“To me vi is Zen. To use vi is to practice Zen. Every command is a koan. Profound to the user, unintelligible to the uninitiated. You discover truth every time you use it.”
Satish Reddy
With vi being a ubiquitous editor that can be found across virtually every Linux distribution, even in their most basic forms, learning to do more than clumsily fumble around the funhouse of keystrokes is a crucial skillset to possess. When deploying a new operating system, having the ability to build and configure an environment from scratch using only readily available tools such as vi inarguably sets the groundwork for success. Writing bash scripts, building configuration files, even writing programs in a language like C can be performed using the seemingly primitive vi editor. There is definitely more to the vi editor than can be perceived by firing it up from the command line and making an ungainly mess or by perusing infantile “vi vs. Emacs” internet memes.
The Plight of the Oblivious Know-It-All
One of the most bitched about problems people face when using vi is exiting it. Not learning about it. Not making use of it. Exiting it. Yeah…

If you have never used vi before, open up a terminal in Linux and type “vi”, then press enter. Try to exit the editor without looking up how to do so with a search engine. Were you successful? Probably not. Neurosurgeons, helicopter pilots, and lion tamers wouldn’t be very good at their jobs without learning how to do them properly before being turned loose. I’ll wager that their early attempts would be a hell of a lot messier than a few special characters and random empty lines on a computer screen. The secrets to exiting vi haven’t been buried in a secret time capsule, kept hidden by a subsect of Freemasons, or withheld from the public by a clandestine government agency. I didn’t always know how to drive a car, but with some basic instruction and practice I figured it out, and the same can be said for picking up vi. This post isn’t meant to echo any of the tens of thousands of vi tutorials that are out there, so I’ll cut to the chase with this cheat sheet I found at cheatography.com, pick any of the four methods of exiting vi. Amazing, right?
The Practical Nature of Vim
So now that the boogie man of incompetency has been shoved back in the closet with an online cheat sheet that took me seven seconds to find and download, let’s talk about some of the more pragmatic applications of vi’s enhanced adaptation, Vim. As vi stands for ‘Visual editor’, and Vim stands for ‘Vi IMproved’, things are already looking good starting off with a tune-up of this classic and invaluable tool. The learning curve becomes a little more shallow with one awesome implementation of Vim. Remember when I asked you to try to exit vi without looking it up with a search engine? Yeah, you didn’t have to even do that, using the command ‘vimtutor’ will open up a beginner’s guide to working with Vim. Between ‘vimtutor’ and Vim’s trusty man page entry, you should at very least be able to crack open a new session, punch out a small script or configuration file, save it, and swiftly exit Vim on the first attempt (ok, at least by the second attempt).
Here are a few examples of vi/Vim features, as well as how and why they make life easier for Linux systems administrators and even programmers when operating in sparse environments:
Multiple Windows – Another handy feature to have in Vim is the ability to spawn multiple windows. One instance where this can be useful is when stitching together a Dr. Frankenstein configuration file or script from preexisting files that contain information that is unique to users, interfaces, addresses, firewall rules, etc. Otherwise things that it would be a waste of valuable time to go scrounging around for or using a barrage of commands to coax out of the system one-by-one, then trying to keep everything organized in a single terminal window. The mutliwindow editing feature used for organization and short-term data management practices relies heavily on the next feature, which has a boatload of utility behind it.
Buffers – One of the more esoteric features in vi/Vim would include the use of buffers. Confusing at first, indispensable later, buffers offer you the ability to cache working data in a contained fashion. Imagine a buffer as having several clipboards that can be accessed (and saved) when needed, providing a means of managing data as you work from within the vi/Vim editor. Taking advantage of buffers also allows you to recover data you were working with in the event of an unexpected system outage, which I know everyone has probably had to deal with at least once in their careers. Stored as files in swap space, you can check out a list of recoverable files stored on your system when starting vi/Vim with one simple command:
vi -r
And if you’re worried about permission based issues ruining your day by denying your save command after cranking out a slew of edits to a file, your buffer can be preserved using this Vim entry:
:pre
Fun stuff.
The ability to recover accidental deletions, copy (yank) lines of text, divide windows within the same buffer, save buffers under a new file name, and list stored buffers are all tricks of the trade you will more than likely find yourself needing at some point in your career when working within an editor like vi/Vim. And if not, just remember how many sugars the custodian takes in his coffee and you should be ok.
Searching for Text – Pawing your way through heaps of text isn’t any fun, I don’t care how much of a digital masochist you are. That’s why Vim’s ability to search for strings of text is just as useful as a backspace button on a keyboard. Running the ‘grep’ command from the command line is a vital tool when parsing globs of text for a single string or pattern among a group of files, but Vim offers in-editor text searching when quick a seek-and-revise operation is underway without the need for more advanced external commands using regular expressions using the ‘:/yourtexthere’ Vim entry. When it comes to stream editing and data manipulation, using tools like ‘sed’ and ‘awk’ are a whole other level of greatness, which I will cover in a future post.
Nice Text Editor, But I’m a Programmer…
Yeah? Well consider the following:
Syntax Highlighting – If you are a creature of simple convenience like I am when it comes to programming or writing scripts, you more than likely appreciate syntax highlighting. I’m not a fan of Integrated Development Environments (IDEs), and while “the simpler the better” is a personal motto I stick to, I have basic requirements while smashing the keyboard. Syntax highlighting is a great way to ensure proper format has been executed, and can help you quickly identify problems during the debugging process after having an error message barf on you. In the Vim editor, syntax highlighting can be enabled with an easy-to-remember command:
:syntax enable
Should you still not have syntax highlighting, enter a subsequent command:
:syntax on
And if that didn’t work, make sure you are saving your file with a recognized extension such as ‘.sh’. I know, it’s like completing a doctoral program in computer science, isn’t it?
Sourcecode Editing – If you have ever used the Python programming language, you know that indentation is required when writing functional code. When working in vi/Vim, an automatic indentation mechanism can be activated with:
:set autoindent
This removes the psychotic game of mowing through a document as a novice programmer ensuring you have all the indentation set to the right number of spacebar taps, especially when nested tabulation could cause an unparallel level of hell. While in “insert mode” you can use ‘CTRL-D’ and ‘CTRL-T’ until every last necessary tab is present. Fancy, modern IDEs usually have some graphically intuitive way of displaying the relationship between tags and brackets that enclose parent-child code groups. Vim possesses a similar scheme that highlights two brackets associated with one another, as well as the ability to jump to the end of a block of code by positioning the cursor while in “command mode” over an opening bracket and punching in a plain old ‘%’.
Compiling and Debugging Sourcecode in Vim – Want to compile and run sourcecode while you’re writing it for testing purposes? Yeah, Vim does that, too. The ‘make’ command is incorporated into the software development process using Vim by passing the data to the parent shell session and seizing the results. This aids in the debugging process and can allow you to gain a decent operational tempo when developing software in a thin environment when wanting for a high-speed, low-drag IDE with a fleamarket of plugins and a dozen windowpanes. The Vim ‘Quickfix List’ feature is something that warrants a post all of its own, but since I don’t aspire to the fool’s errand of attempting to explain what has already been explained well, here’s some documentation.
This is not an exhaustive overview by any means, not even close. To bring a boil down to a simmer in terms of this turning into a runaway tutorial, just keep in mind these are all little incentives for using vi/Vim. Nothing more, nothing less. Does Emacs have something comparable to many of the features in vi/Vim? Probably. Does every freshly-installed, non-graphical Linux system have Emacs? Nope. Again, that’s the point of this post, not to condemn Emacs, but to showcase the importance of learning vi/Vim. That way the next time you find yourself in a pinch, and vi/Vim is your only hope, you don’t just stand there waiting for someone to come change your diaper after realizing you lack even basic Linux system administration skills on a fundamental level.
Now go fire up Vim, and start writing some shell scripts! Happy exiting!
~ Dan
(If you have a write-up of comparable simplicity to this post that gives a quick-and-dirty walk through of reasons to use Emacs, shoot me an email, I would be glad to post it here.)