Linux File Editor

Linux File Editor

We understand the text processor commands in the previous blogs i.e how to extract the data from the file with the help of commands but how to write the data in the file ??

We can write or add the data in the file with the help of the "cat" command but the problem with the cat command is we have to use the append operator (>>) whenever we have to add new text again and again.

That's why we are going to learn about vi editor in Linux. There are several standard text editors in the market like ex, emacs, vim, etc.

vi Editor :

A text editor is a program that enables you to create and manipulate data (text) in a file.

vi editor is a visual editor, a default editor which is mostly available in all Linux/Unix distributed versions. Before vi editor in Unix systems, the ed editor i.e standard line editor was used in Linux.

The difference between an ed editor and a vi editor is ed editor is a line-oriented editor and a vi editor is an improved visual editor in Linux.

There are two types of modes in the vi editor:

1) Command Mode: In command mode, when you type anything in the editor or you pressed any keys it will not write anything it will work as a command mode.

2) Insert Mode: In Insert mode, you can type anything in the editor by pressing just 'i' i.e insert the data in the file.

As vi supplies command for :

1) Inserting and deleting text

2) Replacing text

3) Moving around the file

4) Finding and substituting strings

5) Cutting and pasting text

Most Common Keys used in vi editor:

KeysDescription
escEscape Out of Any Mode
iInsert Mode
rReplace the character in Command Mode
ddDeleting the line in command mode
:q!Quit the editor without saving
:wq!Quit the editor and save the contents
uGet back the deleted line
xRemoving character from a word
esc+/(word)After pressing Escape button type / and the word which you want to find. Ex (/word)
GWill take you to the last line of the file

This the first look when you enter in vi editor :

$ vi <filename>

1) When you press "i" the it will go in insert mode, it will show in bottom :

2) Replacing the character in word (Linus -> Linux) by entering in command mode by pressing Escape Button and pressing r to replace the character by typing the character which you want to change.

So, these are the common keys which are used in vi editor. In next blog, we will be discussing about the difference between vi and vim editor i.e the improved version of vi editor.

I hope you"all have learned something from this blog. Follow me on Twitter and Hashnode for more Linux and upcoming DevOps tools blogs.

THANK YOU :)