Let's talk about Vim!
Vim is a terminal-based modal text editor. What does this mean? "Terminal-based" means it doesn't have a graphical user interface. Hence, it is an application to be used in the terminal. Then, it is a "modal" editor which doesn't mean it consists of modules but it has several operating modes which have different purposes. Lastly, it is a text editor, sou it is used to edit plain text files ranging from simple configuration files to C++ source code. Vim has been developed on top of Vi, the "visual" text editor. Vim stands for"Vi improved". There is also a newer version called "neovim" which is a rewritten version of Vim with many optimizations with less source code.
As already mentioned, Vim has several modes that can be used for editing text files. The standard mode is called "Normal Mode" in which you can navigate around the file. You can delete lines or words, you can copy them select them or change them. To select words or lines, you need to enter the "Visual Mode" in which you can select words or lines and then you can delete them, copy them or even modify several lines at once. To modify the lines, you need to enter the "Insert Mode" in which you can type text, similarly to any other text editor.
Note: In Normal Mode and in Visual Mode, you will not be able to type characters as you hit them on your keyboard. This is only the case in Insert Mode. You can enter Normal Mode from any other mode by pressing the ESC key on your keyboard. From there, you can enter Visual Mode by pressing the v key and Insert Mode by pressing the i key.
Saving and closing the file
Before you'll learn how to manipulate the text file or anything else, you should know how to save and quit the file. For all these operations, you need to be in the Normal Mode. Press :w to save the file. The w stands for write. To Close Vim, you can use :q which stands for quit. As this is not very intuitive, you'll need to get used to this.
Some people tend to get stuck in Vim and don't know how to exit the program. Simply closing the terminal could be a bad idea as Vim automatically creates a copy of the file as backup to keep your changes intact in case there is a disruption. The next time you open that file again with Vim, it might give you a warning.
There are two more things you should know. You can combine the write and quit commands with :wq to save and close the file in one command. :x does the exact same thing. Lastly, if you don't want to save the file and simply quit it, even though you have unsaved changes, Vim will not let you leave with :q. Instead, you need to explicitly tell vim that you don't want to save the file with :q! which then exits Vim without saving the file.
You can navigate with the cursor through your file by using the arrow keys. In addition to that, in Normal Mode and Visual Mode, you can also use the keys h, j, k and l to navigate where h and l are used to go left and right and j and k are used to go up and down. This feature has the advantage that you can keep your hands on the keyboard at the default position like you were typing without moving your right hand to the arrow keys.
When you want to go to the next word in a line, you could either shift your cursor character by character, or you can use the w key in Normal Mode or Visual Mode. This will bring your cursor to the beginning of the next word. e brings the cursor to the end of the next word and b will bring you back one word. These shortcuts can save you many keystrokes and make you navigate quicker to the text file.
When you use the letters w, e or b, the cursor will bring you to the next word where a word is defined by a continuous sequence of letters. This means that punctuation mark the end of a word. An expression such as "long-term" will be seen as 3 words. When using W, E or B instead, the cursor will jump to the next word where a word is defined as everything that is separated by a blank space. As such, "long-term" will be seen as one single word. This can help you to navigate even faster through the lines.
When you want to go to the beginning of a line, you can use the 0 key (the number zero). And to go to the end of a line, you can use the $ key.
The last two commands are gg and G which will bring you to the first line of the document and the last line of the document respectively. This is often faster then taking the mouse and scrolling to the respecting lines.
Using Normal Mode
Now that you know how to navigate through your lines, the following commands will explain how to use the Normal Mode more effectively.
When you are in Normal Mode and you want to enter Insert Mode, then you can use the command i, like "insert". This will allow you to type in front of the current cursor position. If you like to start writing after the current cursor position, you can use a for "append". The commands I and A will bring you to Insert Mode at the beginning of the line or end of the line respectively.
The command dd deleted an entire row. With dw, you can delete an entire word. With db you can delete a word backwards. Note that with dw, you will only delete the letters of this word after the cursor position. If you want to delete the entire word while the cursor is in the middle of the word, you can use diw. The letter d stands for delete. In other words, you just need to think "delete word" to remember how the command looks like. Similarly, diw stands for "delete inner word" which means you delete everything within the borders of the word. One last command related to deleting is D which deletes everything from the current cursor position to the end of the line.
If you want to delete a word or a line and then you want to write something else instead, you could for example use dd and then i. Instead, you can also use cc which deletes the row and brings you ti Insert Mode right away. The c can be seen as "change" and it also works with cw to change a word or C to change the rest of the line in one keystroke. Another command that you might find useful is ci" which will delete everything within a pair of double-quotes and bring you into Insert Mode inside the quotes. This also works with ci( for parentheses. You can be creative.
If you want to change a single character, you can move the cursor to the character you want to change and then press r followed by the character you want to have instead of the current one. The r command "replaces" that single character without that you enter Insert Mode. With the command x, you can delete a single character under the cursor.
You can copy and paste words or lines as well. You can copy a word with yw and copy an entire line with yy. The letter y stands for "yank" which means to copy. To paste, you can use the command p.
You can also delete or copy several words by adding a number in front of the command such as 3dw which will delete three words or 22cw which will delete 22 words and brings you into Insert Mode. To delete 5 lines, you can use 5dd.
Using Visual Mode
Visual Mode can be entered from Normal Mode by using the v command. This lets you move the cursor around like in Normal Mode and then you can delete, copy or change the text. When you use V instead, you will select entire lines of text. This is ideal to select several lines when you want to delete them or copy them.
The third option to select text is the block selection that can be used with ctrl+v which allows you to navigate left and right like with the v command but when you go up and down, you select a block of characters. This is especially useful when you want to change a configuration file or a block of similar source code as you can then use c to make changes to the text and these changes are applied to each line of the selected block.
Macros are a sequence of commands that you can execute in Vim. If you want, you could say that you can program Vim to do changes in your text. This is done by recording what you do and then replay that sequence again when you use that specific macro. This is useful if you need to format multiple lines in the exact same way, but in different spots in the text file. You could create a macro that switches around the order of two words, or if you want to add quotes around a string of text. The possibilities are endless.
You can start the recording of a macro by pressing q followed by another key such as a or b. This letter will be used to store the macro as you can create several different macros and use the one you need. The lower left corner will indicate that you are recording a macro. Every keystroke that you will now do is being recorded. When you are done with your task, you can end the recording with q again. You can now press @ followed by the letter that stores the macro that you want to use. If you stored your macro in p with qp, you can now activate that macro with @ p.
Of course, you can also apply a macro multiple times by adding a number in front such as 20@a. This is very powerful when you program at the end of your macro to go to the next line so you can easily modify multiple lines with one macro and one command applying that macro multiple times.
As you can see, Vim is different than most other text editors. Once you get used to the special keybindings, you can become very fast in editing text and especially the macros and the block selection can save you a lot of time when you need to bring text into a certain formatting. Besides that Vim is lightweight and fast, it can also be used through an ssh session. On top of that, there are many plug-ins that can be used to add functionality to Vim such as code completion, file browser or syntax highlighting for different file types.
I recommend you to try Vim yourself and use if for a few weeks until you start understanding the very basics. If you feel like this is not the right tool for you, this is fine, but it never hurts to know how to open a file, edit it and save it as many systems have it installed. When logging in to a robot or server where there are no graphical programs, Vim could be the only text editor.