Linux Text Editor
Everything is file in Linux operating system. So, anyone who wants to work with Linux operating system (specially, Red Hat or CentOS) must learn how to edit files in Linux operating system. Because, it is always prefer to avoid graphical user interface (GUI) for changing system configuration parameters in 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 with command line editor such vi, vim, nano etc or graphical user interface (GUI) editor such as gedit. But Red Hat Linux administrators always prefer to work with command line editor. So, this article is designed to show you how to work with Red Hat Linux command line editor (vi/vim text editor) properly.
Vi / Vim Text Editor
If you are familiar enough with Windows operating system, you may see 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.
- Insert Mode: 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.
- Exit Mode: this is the default mode of vi/vim text editor that means when you will open any file with vi/vim editor, the file will be opened with exit mode. In this mode, you will be able to apply various instructions to your vi or vim editor such as delete any line, save any file, quit your file and so on.
Vim Package installation
As I said before, vim package must be installed to get advanced and colorful vi text editor. If you are new in Red Hat or CentOS Linux and you have not yet configured your network, feel free to study my previous article about Red Hat/CentOS 7 network configuration and configure your system network according to that article and then follow below section to install vim package.
To install vim package, run bellow command from your command prompt.
As soon as you run this command, your vim package will be started to install and within few seconds it will complete installation. Now you are ready to use this advanced and colorful editor to edit your Linux files.
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.
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.
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.
Some Important Vi/Vim Instructions in Exit Mode
Besides above three basic instructions in vim editor, you can do more instructions which will help you to edit your file more quickly and efficiently in exit mode. A list of these instructions is given in below table.
Instruction | Description |
:set nu | It will show file’s line number |
ctrl + r | Redo |
:q | Quit normally |
:q! | Quit forcibly without saving |
u | Undo |
dd | Delete a line from current cursor position |
ndd | Delete n lines from cursor position |
dw | Delete or cut a word at cursor position |
dl | Delete a letter at cursor position |
yy | Copy one line |
nyy | Copy n lines |
yw | Copy a word |
yl | Copy a letter |
2yl | Two letter copy |
p | Paste copied text below cursor position |
P (Capital) | Paste copied text above cursor position |
1+shift+g | move cursor at 1st line |
shift+g | move cursor at last line |
25+shift+g | move cursor at 25th line |
shift + d | Cut lines from current cursor position |
o | writing (below cursor position) |
O | writing (above cursor position) |
/search word | Search pattern i.e: /root, n for next search word and N for previous search word. |
:%s/old_keyword/new_keyword/g | replace old with new word |
:%s/old_keyword/new_newkeyword/gc | replace old with new word confirmation |
Working with Linux vi or vim editor has been described with example in this article. I hope you are now able to work with Linux vi or vim editor so efficiently. However, if you face any problem to work with Linux vi or vim editor, feel free to ask me or contact with me from contact page. I will try my best to stay with you.
Why not a Cup of COFFEE if the solution?
yes i like your share your experience so you can other thing is you know