15 C
Texas
Muhammad Nabeel
Network and System Administrator

How to edit files with vi or vim command

What is vim?

Vim is a clone, with additions, of Bill Joy’s vi text editor program for Unix. vi/vim used to edit or modify a text files in CLI (Command Line Interface).

It is most popular text editor in Linux. Today, we will discus its usage and option.

Vi or Vim has three modes

Escape or Normal mode
Insert mode press i to insert
Replace mode

- Advertisement -

In this example i will use /etc/hosts file for vim command. Simply, run below command and you will get into normal mode.

vim /etc/hosts

Now, if you want to edit your file. You need to go on insert mode. Press “i” for insert mode and now you can add text in the file.

After adding your text, Press “Esc” key to switch back to normal mode.
Now save and exit file by typing : x OR :wq and press “enter” key

So, you can move around with the arrow keys, or by using the vi movement keys, as follows:
h left
j down
k up
l right

vim options for Escape/Normal mode

:w save changes to file.
:wq save and exit from file.
: x save and exit from file.
:q! Exit without saving.
i so i use to insert where is the cursor.
o cursor will move to next line to insert.
x Delete the character the cursor is on.
Shift+x Delete the character before the cursor.
dd Delete the current line, also used for delete.


Shift+d Delete everything from the cursor to the end of the line.
u Undo a line you just deleted.
Shift+u Undo all changes to the current line.
yy Copy current line.
p past copied line or text.
y4 Copy multiple lines. 4 is the number of lines we want to copy.
/string-name to search desired text. String name is the text we want to search.
n Move to next searched string.
shft+n move to to previous searched string.


:%s/old-string/new-string/g Replace old text with new text.
:9 To go line no. 9
gg Go on the top of file.
shft+g Go to the end of file.
Ctrl+b Go back one page.
Ctrl+f Go forward one page.

That’s it. Practice all above options to get grip on vim command.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article