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.

‫ vim text editor
‫ vim text editor

Vi / Vim Text Editor Mode

There are two types of vi or vim editor modes.

  1. 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.
  2. 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.

[root@localhost ~]# yum install vim -y

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.

[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
Insert text with vim editor
Insert text with vim editor

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.

Save and quit text with vim editor
Save and quit text with vim editor

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.

InstructionDescription
:set nuIt will show file’s line number
ctrl + rRedo
:qQuit normally
:q!Quit forcibly without saving
uUndo
ddDelete a line from current cursor position
nddDelete n lines from cursor position
dwDelete or cut a word at cursor position
dlDelete a letter at cursor position
yyCopy one line
nyyCopy n lines
ywCopy a word
ylCopy a letter
2ylTwo letter copy
pPaste copied text below cursor position
P (Capital)Paste copied text above cursor position
1+shift+gmove cursor at 1st line
shift+gmove cursor at last line
25+shift+gmove cursor at 25th line
shift + dCut lines from current cursor position
owriting (below cursor position)
Owriting (above cursor position)
/search wordSearch pattern i.e: /root, n for next search word and N for previous search word.
:%s/old_keyword/new_keyword/greplace old with new word
:%s/old_keyword/new_newkeyword/gcreplace 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?

linux-text-editor

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a System Zone author.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*