If you’re starting to manage your own VPS server for the first time, you’ll know that you’re going to have to modify a lot of text files. Just basic steps like disabling root login require you to open up files and make changes. And in Linux, there is one text editor that reigns supreme – vi. It’s specially designed for use in a command line environment, and doesn’t rely on any GUI design elements to operate. In fact, it’s a great illustration of the “command line vs GUI” argument.
But like it or not, you can’t escape vi. So you might as well learn how to navigate it. In this article, I’ll show you the basics of vi, how to write, and save your changes.
The Idea Behind vi
The philosophy behind vi is simple. You should be able to perform all editing and file management tasks without removing your fingers from the “home” row of the keyboard. So say you need to go do the end of a line on a normal computer, you’ll have to press the “End” key. And the “Home” key to go to the beginning.
But in vi, you can press “0” to go to the beginning of the line. You can type “de” to delete everything till the end of the current word. Or “Shift + D” to delete till the end of the current line.
But if we can do all this using regular keys, how do we type?
Two Modes – INSERT and COMMAND
The vi editor, allows you to be in two distinct modes. The INSERT mode means that whatever you type appears in the text editor as you would expect. So if you type “d”, it would show up on the screen. The COMMAND mode on the other hand, is used for other types of editing – navigation, find and replace, advanced deletion, finding instances of words etc.
When you start up vi, it’s in COMMAND mode by default. To start typing, press “i”, or the “Insert” button. This will change the mode and you can see that you’re in the right mode by looking at the bottom left of the window as shown here:
Once you’re in INSERT mode, you can start typing and the words will appear on the screen like in a normal text editor.
Exiting COMMAND Mode and Saving your Changes
Once you’re done with your editing, press the “ESC” key. This will exit the INSERT mode and revert back to COMMAND. To save your changes and exit, press “Shift + ZZ” to exit without saving, press “Shift + ZQ”.
Multiple Ways to Do the Same Thing
One aspect of vi, is that there are multiple ways to accomplish the same goal. For example, in the situation above, you can also save your changes and exit by typing “:wq” when in command mode. Or even “:x”. Sometimes, there will be minor differences between these commands, but often they will be exact duplicates of one another.
Documentation of vi is Scattered – Google is Best
It’s not easy to find a single comprehensive list of all vi commands. The closest I’ve been able to come to is vimhelp maintained by Carlo Teubner. But even in that, it’s segmented into several sections that can make it hard to find the exact command you want. For example, the list of commands for saving and exiting vi are in the “quickref” section.
The most efficient way is to simply Google. You will almost certainly find someone who has asked the same question that you have on a forum like StackExchange for example. So fire up vi, and use Google to get specific tasks done. After a while, you’ll start remembering the most commonly used commands and go from there! A complicated tool like vi is best learned in an “ad-hoc” manner instead of “top-down”. Which is why I’ve avoided giving too many instructions in this article. Find what you want by using a search engine, rinse and repeat, and soon you’ll have what you need at your fingertips.
Using vi can be a bit counterintuitive at first. But once you realize its most common use case – quickly editing configuration files, you’ll see that you spend most of your time in COMMAND mode. It’s not meant to write your latest blog post or maintain a diary! For its purpose, vi is a fantastically efficient text editor that you’ll get used to in no time!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply