Red Hat or CentOS deals mainly with files, to be an expert in this field you have to learn how to edit and manage files in Linux operating system.
Hence, it is always preferred to avoid graphical user interface (GUI) for changing system settings while using Linux operating system unlike Windows operating system.
You should be expert enough to edit any file in Red hat Linux operating system otherwise you will face a lot of difficulty to maintain your Red Hat Linux operating system.
Red Hat Linux files can be edited from its command line interface such vi, vim, nano etc or user (GUI) editor like gedit.
But it is obvious that those of us who with Red Hat Linux, always prefer to work with command line editor.
And here in this article am going to break down bit by bit how to Edit Text on Linux using the command line interface.
Vi / Vim Text Editor
If you are familiar enough with Windows operating system, may be you have come across s notepad or WordPad which is default file editor in Windows operating system.
Similarly vim, nano, emac, pico and gedit are various file editors in Red Hat Linux operating system. Among these, vi and vim are the most popular and advanced command line text editor to Red Hat Linux system administrators. Vi is default text editor in Red Hat Linux and vim is advanced and colorful version of vi editor but vim package must be installed after fresh Red Hat Linux installation.
Vi / Vim Text Editor Mode
There are two types of vi or vim editor modes.
First is the Insert Mode: The insert mode will allow you to insert characters or text in a file.
To enable insert mode, you have to press ‘i’ key from your keyboard and then you will be able to insert any text in your desired file.
Vim Package installation
To have your vim package installed , run bellow command from your command prompt.
[root@localhost ~]# yum install vim -y
As soon as this command is run, your vim package will automatically start to install and within few seconds it will complete installation.
Working with Linux Vi/Vim Text Editor
After successful vim package installation, we will now learn how to work with this text editor.
With a text editor, we mainly open a file, insert some text in this file or delete any text from this file and then save our changes. Now we will do these basic jobs with our vim text editor step by step.
Opening a File with Vi/Vim Editor
The command format to open any file with vim editor is vim filename file path where vim is command to open a file, filename is name of your desired file and file path is file location of your file.
File path is an optional parameter and if you do not mention file path, it will use your current working directory as its file path. An example is given below.
[root@localhost ~]# vim file1
With above command, a file named file1 will be opened with vim editor.
If file1 do not exist, a new and blank file named file1 will be created first and then opened with vim editor.
Inserting Text in a File with Vi/Vim Editor
When any file is opened with text editor, by default it will have in exit mode. So, if you want to insert any text, you must turn exit mode to insert mode. As I said before, you have to press ‘i’ key to turn exit mode to insert mode. So, press ‘i’ and you see your editor is now changed to insert mode.
Now you will be able to insert any text in your file. Insert any text or change any text in your file like below example.
[root@localhost ~]# vim file1.
Save and Quit Any File with Vi/Vim Editor
After inserting text in a file, you need to save and quit this file. To save your file, you have to again turn insert mode to exit mode. Press ‘esc’ key from your keyboard to turn insert mode to exit mode and press :wq key combination where w stands for write and q stands for quit and then hit enter key.
Your file will be saved and quit now.