Linux offers powerful command line alternatives especially when it comes to text editors. But, most of the users do not prefer command line text editors because of the steep learning curve. I had the same perception until I actually started using one of the most popular command line text editor -- Vim.
Though, I agree that there is a bit of learning curve associated with it but once you get used to this powerful text editor, you save a lot of time editing files. In this article, we will learn how to use Vim text editor for basic text editing in Linux through some practical examples.
Get Started With Vim Editor
The whole article is divided in 5 easy to understand steps. The steps are explained in a way that any newbie can read and practice them in parallel.
1. Open a new or existing file
It is very easy to open a new or existing file in Vim. Just write and execute 'vim [filename]'.
For example :
Here is a screen-shot of an existing file opened in vim editor :
2. Enter the insert-mode to edit text
To start editing the text, first enter the insert-mode (yes, vim has different modes of operation) by pressing 'i'.
Here is a snapshot of vim editor in insert-mode :
Observe that as soon as vim enters insert-mode, an indicator at the bottom of the text editor window confirms the same. In this mode, you can add and delete text just as you'd do in your favourite text editor.
3. Copy, Paste and Delete Lines
In order to copy, paste and delete lines, the vim editor should exit the insert-mode (the mode we discussed in example-2 above). You can do this by pressing the 'Esc' key.
Once the vim is out of insert-mode (i.e., back in its default mode), you can use 'yy' to copy, 'p' to paste and 'dd' to delete a line.
For example, in order to delete a line, first take the cursor to that line using arrow keys
and then press 'dd'
Observe that the line got deleted.
Also, if you want to copy or delete more than one line, press numeric key (representing the number) before 'yy' or 'dd'.
For example, to delete 3 lines, first place the cursor at the first line
and then press '3dd'
Observe that 3 lines got deleted and a message "3 fewer lines" towards the bottom of the text editor window confirms the same.
NOTE - Use 'u' to undo any of these operations. If the editor is in insert-mode, press 'Esc' so that it comes out of it and then press 'u' to undo any changes.
4. Search keywords
Just like copy, paste and delete operations, searching a keyword also requires the editor to be in the default mode. To search a keyword, just press '/', write the keyword and then press enter.
For example, to search 'stdio' in the file, first bring the cursor at the beginning of the file and then write the command:
Here is an example snapshot :
Now press enter to search the keyword.
The cursor sits at the beginning of the keyword (if found in the file). Also, the row and column numbers of the searched keyword are displayed at the bottom right corner of the vim editor window.
Use 'n' to move to move forward and 'shift+n' to move backwards among the searched instances.
5. Save and exit
These operations require the vim editor to be in command-line mode, which can be turned on by pressing ':' when the editor is in default mode.
Here is an example :
observe that ':' is displayed at the bottom left once you press it. Now, to save a file, write 'w' in front of ':'
and then hit enter.
The changes are saved and observe that a message (confirming the same) is displayed at the bottom left.
To exit the editor, write ':q' and then press enter. Make sure that the file does not contain any unsaved changes. In case it does, an error is displayed at the bottom left.
You can also use ':wq' to save and exit together. Also, if it is desired to discard the unsaved changes and quit the editor then use ':q!'.
If you went through the article carefully, you'd have understood that Vim editor operates in various modes. Though we did not cover these modes in detail but understanding these modes is important. In the next part of this ongoing series, we will cover these modes along with some more features of the Vim editor.