Vi is a text editor that is usually found under Unix, Linux and other open-source OS environments. “Vi is one of the greatest text editors ever created by the human hand“. Just for fun, go scream the quoted line to a crowd of Emacs fans and watch as all hell breaks loose! I do not vouch for your safety!
But just like Emacs, Vi too is a great text editor. Each has its advantages, and disadvantages (Vi – High learning curve, Emacs – Pinky Syndrome). But once you have mastered them, they are really powerful editors. Choose one of them great editors (whichever you like), and master it.
To help you get started with Vi, I have written this small tutorial, because most beginners are usually stopped in their tracks by their first encounter with Vi. Now, this article will deal with Vim, which is actually Vi Improved. In my opinion, it is a bit friendlier than the traditional Vi, while including all the greatness of Vi.
So let’s start, shall we?
Why Vim over Vi?
Vim has more features than Vi, such as code completion, syntax highlighting, folding and extended undo/redo facilities. So if you have to choose, give Vim preference. It just makes life easier. There are not many differences in terms of usability (both have a relatively high learning curve! :D), and Vim has a Vi-compatibility mode if you want the old Vi flavors.
Obtaining Vim
Usually, if you’re using Linux, you won’t have anything to do. Vi would probably be installed by default. Nowadays, Vim is included too.
If you don’t have it, that is, typing “vi” or “vim” in a terminal doesn’t give you anything except an error about package missing or something like that, then you can install it.
The easiest way would be via your package manager, so open Synaptic, Apt or whatever you use to get packages, and install it. There are too many distributions out there for me to list instructions for all. Just check your distro’s docs on how to get software installed. It usually involves clicking on “Package Manager” or similar in the main menu, typing the root password and just searching and installing. After that, follow the rest of the tutorial.
If you are using Windows, don’t despair. Vim is also available for your OS, and it’s called “gVim”. Just download it from the provided link, install it and you’re off. The difference is that instead of running in a terminal, gVim has its own GUI (hence, graphical Vim), though it comes with a console version if you want that.
Getting Started
Simple enough. Open a terminal on Linux. Then type: “vim afilename” (afilename: a path to a file that already exists, which you want to edit) or just “vim” which will create a blank session.
On Windows, just open gVim. Double-click on its icon or whatever, just like you run a program on Windows. The icon should usually be on the desktop. That’s the place where instructions diverged. As from now, it’s the same. Heh, that’s one thing at least that’s similar on Windows and Linux. 🙂
You will be presented with this screen if you just typed “vim”:
That’s your main Vim window. This is where all the typing takes place. If you wanted to edit a file and typed the first command, your file’s contents would be displayed here. For this lesson, let’s just assume you want to create a new file, so you just typed “vim” at the terminal or opened gVim directly.
So you see your main (blank) window. If you try typing, you’ll probably see nothing appear. This is kind of strange for new users, because they think that when they type, it should appear, and so Vim is not working. Don’t worry, it’s perfectly normal. It’s just that you’re not in the right mode.
Modes
Vim has two main modes: insert mode and command mode. When you start Vim, you’re in the latter “command mode”. This mode allows you to execute program-related commands, such as saving the file, or quitting the editor. “Insert mode” allows you to do typing-related operations. However, as you’ll soon see, there are some overlaps. You can do some modifying directly from “command mode”. But that’s for later.
When you just started Vim, you’re in “command mode”. To go into “insert mode”, press the letter i key on your keyboard. Now you can start typing. To return to the “command mode”, press the Esc key. Now you know how to move between the two modes. Go to “insert mode” (it’ll indicate so in the status bar), then type a few letters, or maybe a sentence. Then, press “Esc” to return to “command mode”.
Vim commands are usually prefixed with a “:“, a semi-colon, for example “:wq“. So after typing a few letters, we wish to save the file. We’ll now use the “command mode”. Ensure you are not in Insert mode (there won’t be “INSERT” written in the status bar). Type the command “:w somefilename.txt”, including the semi-colon but without the quotes. Then press enter. You can now find your file in the location where you started Vim, which is often your Home directory on Linux. Just go there and find your file.
Navigation
Vi was designed to greatly facilitate navigation through a body of text without having to resort to a mouse. Using Vi or Vim, your hands will rarely leave the keyboard when you need to move through a file, or execute a command; it’s all done from the keyboard itself.
Navigation is usually done in “command mode”, so if you were inserting text, return to “command mode” by pressing escape. Okay, what will follow is weird, I know. Whenever I say, like, X-key, it means the letter X key on your keyboard. Lowercase X, unless otherwise mentioned.
To move one character left, press the H-key. To move one character right, press the L-key. Up and down are K-key and J-key respectively. As you can see, all those keys are on the home-row of your keyboard, so navigation is done with minimum finger movements. You can also use the arrow keys to navigate while being in “command mode”. This implies moving the arm a bit, so it’s not as efficient as using H-L-K-J keys.
To move one word left or right, press the B-key or W-key respectively.
To move to the start or end of a line, press the ^-key (6-key on most keyb0ard) or the $-key (4-key on most keyboards).
Editing in Command Mode
You have to perform these commands while in “command mode”. So, to be on the safe side, just press Esc key. It will return you to “command mode”.
Pressing the X-key will delete the character under the cursor.
Pressing the C-key will change the character under the cursor, and drop into “insert mode” to allow you to type a new character.
Pressing the D-key twice (“dd”) will delete the current line.
Undo / Redo: Press U-key / Press Ctrl+R-key. (Note, Ctrl is often abbreviated as ^ in cheat-sheets, so Redo would be denoted as ^r)
Copy-paste: Press the Y-key twice (yy) to copy current line. Press the P-key to paste the contents after the cursor. Note “copy” is called “yank” in Vi terminology.
Some Command Mode Commands
In “command mode”, you can use the following to perform file/editor related operations. Here are some commands to get you started.
/wordtosearch (note, no : at the start), will allow you to search for “wordtosearch”. Press n-key for next occurrence, and N-key (Shift+N) for previous occurrence. Note that searching is case-sensitive. You can enable/disable case-sensitivity with :set ignorecase/noignorecase when you are in “command mode”. Search and replace is more complex, so I’ll leave that out because it involves the concept of ranges.
:w is used to write to file. It is similar to the Save command. It can be followed by a filename e.g. :w afile
:wq will write to the file and exit the editor afterward. :wq! will forcefully write and exit, even if there might be problems.
:q will quit the editor.
:q! will forcefully quit the editor.
That’s it for this tutorial. I hope it will help you get started in your journey to knowing how to use Vi and Vim. Vim is a very advanced editor with an enormous amount of features. What I have listed here is just a small fraction of what you can do with Vim. But since this is a beginner’s tutorial and since I am not a Vim guru myself, this is where I’ll stop this tutorial. If you have any comments or tricks for Vim beginners, do leave a comment. Thank you for reading!