In this post I will cover the usage of a terminal-based text editor, such as vim or nano, to modify text files.
Nano is one of the basic text editors in GNU/Linux. It is available on most of the distributions by default because it’s easy to use and one of the best choices for basic operations with text files.
In order to open a file for editing you just type:
This will open the file for editing. It’s best to use the -w option when editing configuration files, this option turns off word wrapping.
nano -w /etc/fstab
Unlike vim, nano is a “mode-less” text editor, meaning that once you opened a file, you may just type to edit it. The available commands can be seen on the bottom of the screen:
In order to save any change you’ve made, press CTRL + O – (the “^” sigh represents the CTRL key).
If you want to exit the editor, type CTRL + X.
You may also cut a single line by using CTRL +K and then paste it anywhere you want (just navigate with the arrow keys wherever you want) and then press CTRL +U.
Finally another useful shortcut is the shortcut for the search function, which is CTRL + W. Enter the string to search and press enter. The cursor will go to the first match found. In order to go to the next match, press ALT + W.
These are the basic functions of the nano text editor. For more advanced functions and usage, feel free to visit the Nano Project webpage.
VIM (which stands for VI Improved)
It’s an an advanced text editor that can do a lot of wonderful stuff. Fortunately just for RHCE/RHCSA exam you will only need to know the basics.
There are six different vim modes, but we are only interested in the Normal, Command Line and Insert modes.
In order to enter vim just type:
At first you go into the Normal mode. Here you may select the insert mode (by pressing i).
In insert mode you are able to edit the file, you can type and move with the arrow keys. In simple vi (not vim) you have to go back to Normal mode in order to “move around” using h,j,k,l keys.
To go back to Normal mode press ESC key.
In order to go to command line, you have to type the colon sign (:). This can be done only from the Normal mode. So if you are in Insert mode, go back to Normal mode by pressing ESC key.
In command line mode you way quit (press :q), write changes to file (:wq), force quit without saving any changes (:q!)
If you want to search for something in the file, go to Normal mode and type /somethingyouwanttosearchfor
So here is the sum up:
Create or edit file:
Quit vim (:q)
Move cursor: Use the arrow keys or j, k, h, l (down, up, left, right)
Insert mode (:i)
Save file (:w)
Save and quit (:wq)
Abort and quit (:q!)