Introduction to Vi - Vi is a standard text editor for Unix-like operating systems, but is now available on other platforms like Windows too. It is not one of those text editors that most people use because it's a bit tough to learn.
Wait! Did i just say that you would have to LEARN how to use this text editor? Well, you read it right! With great power comes great responsibility, but to get both of them you have to devote some time and effort. ;)
Vim stands for Vi IMproved, and provides more features than Vi. Okay, so there are really LOTS and LOTS and LOTS of features in Vi, and people come to know about its new functionalities even after decades of using it. So, learning it thoroughly would probably take a lifetime, still, we can begin to learn it right now!
Opening a file in Vi: Type vi filename.txt (or any other file name that may or may not exist). If it's an empty file, you will see something like this:

The tilde ~ sign means that the line is empty or unused. You can open an existing file in the same way. It will display its contents. If you want to open a file from a specific line, type:
vi +10 try.cpp
And it will open the file try.cpp and position the cursor at line 10.

If you want to open a file in Read-Only mode, type:
vi -R abc.txt
Editing: Now, Vi is a MODAL EDITOR. This means that there are various modes in Vi that you can work in. By default, it is in Command Mode. This means that whatever you type in, will NOT be displayed as text! The characters you type will be interpreted as Vi Commands. To insert the text like a regular editor, press i. It changes the mode to Insert mode. Now, type in whatever you want, scroll up, down, left, right using the arrow keys, and press Esc key when done. This will take you back to the Command Mode.

Now, you might think that Ok, that wasn't tough, but tricky. Why should I use two modes and switch between them and all that stuff. But, one thing to note is that most of the time you have to be in the Command mode to take full advantage of Vi. It is NOT meant to create files and edit them the way we did just now. So how to do it correctly? We'll see that a bit later.
Saving: Type :w when you are in Command Mode. It will appear at the bottom of the screen. Press enter.

Quiting: To save and exit, type :wq in Command Mode. To exit without saving, type :q!.
Scrolling through the text: Now, here's where Vi starts to differ from your regular IDEs and text editors. In Command Mode, you can either use the arrow keys to scroll through the text, or use the keys h (for moving the cursor left), j (for moving cursor to the line below current line), k (for moving to the line above the current ine) and l (for moving the cursor to the right). These alphabets and their usage don't make sense, right? But, they are carefully chosen. These four letters are present at the right side, in the middle row of the keyboard, and are the easiest to reach. Once you get hold of them, they can save precious time, especially if you are coding or debugging for long hours!
To move to the beginning of the file, press gg. To go to the end of file, press G. To go to line 2, press 2G. Similarly, for nth line, press nG (n is the line number).
To go to the next word in the current line, press w. To go to previous word, press b.
For placing the cursor at the beginning of the line, press 0.
Now, there are a lot of commands that probably no one can cram in the first go. I would suggest you to practice them and then master them.
Two features that I would like to discuss are indentation and repeat last change command. I usually program in C or C++. It is generally a good practice to properly indent your code, so that it becomes readable. Some IDEs do that by default, like CodeBlocks, while text editors usually don't support it. So, let's say I want to indent the following program:

To do it, i can position my cursor at the beginning of a bracket, and press >%. This will move the block of code towards right. Similarly, I can do it for other blocks too. For single line we can use >> command.

Next is the repeat command. Suppose I inserted one line in the Insert Mode:

Now, if I want to repeat the previous command, I can simply press . and it will be repeated at the new position!

I can also use yanking (or copying) and that will produce the same result. Just go to the line, press v$y, move the cursor to the new position and press p.
In the long run, these small commands can make a big difference in the amount of time required to edit your code. So if you think it's worth learning, go ahead and experiment yourself! Also, you can use the vimtutor to teach yourself. Just type in vimtutor at the terminal and it will open up.
Feel free to Comment below for queries!
[…] Cmus is a Command Line Music Player for Linux Operating System. Though it looks a bit geeky (and cool at the same time ? ), it can perform almost all basic operations that a GUI Music Player can. It’s default keys (called “keybindings”, which can be changed according to the convenience of the user) are similar to those used in Vim, our beloved text editor. Click here to know more about Vim. […]
ReplyDelete