top of page
Recent Posts
Writer's pictureAbdur Rouf

vim the powerful text editor tool for linux

Updated: Oct 3, 2021

Document Id: RHEL003


In my previous post, I've shown commonly used Linux commands, in this post I'll describe a powerful text editor which is used to edit Linux configuration text files.


Previous post: Linux basic commands


Vim is a powerful text editor used in CLI (command line interface). It’s available almost all Linux distribution. Linux uses a lot of configuration files, we’ll often use “vim” to edit configuration files.

Using vim we can open an existing text file, then, edit it and save, or we can create a newfile, edit the file and save & exit.


There are two modes in vim editor


1. Command Mode

In this mode we can apply commands like cut, copy, paste, search, undo, save, exit, etc


2. Insert mode

In this mode, we can insert/edit text.


Initially “vim” open in command mode to switch insert mode we have to press “i” from keyboard.



From command mode to insert mode press “i”

From insert mode to command mode press”Esc”


Let’s create a new file using vim


[root@RHEL82 ~]# vim fileA




The file has been created in system memory and opened in command mode, now if we want to exit from the vim we can press:q! (colon,q,! together)


:q! is used for exit from vim editor without saving the file.






Now let’s check the fileA


[root@RHEL82 ~]# ls


We see that there is no file name “fileA” because we exit from “vim” editor without saving the file, so fileA has not been created in the disk.



If we want to create a new file using “vim” we must do"save & exit" then only it will create a file in the disk.


Let’s create the fileA again, input text then save & exit


[root@RHEL82 ~]# vim fileA

It is in command mode, to change command mode to insert mode press “i”




Now, Add text “ This is RedHat Enterprise Linux 8.2”




Press” Esc” to return command mode

To save & exit command is :wq (colon,w,q together) then press "Enter" key.






Now, let see whether the fileA has been created


[root@RHEL82 ~]# ls

anaconda-ks.cfg file02 fileA initial-setup-ks.cfg newfile

File name "fileA" has been created.



Commands reference in vim command mode.

(ALL COMMANDS ARE CASE SENSITIVE)

i - Insert at cursor (goes into insert mode)

ESC - Terminate insert mode

yy-copy one line (y is for yank)

3yy-copy three lines

p-paste single time

3p-paste three times

u - Undo last change

U - Undo all changes to the entire line

dd - Delete line

3dd - Delete 3 lines.

:/(string) – to search a particular string

:line_number- to go line_number

H- go high (Top of the file)

M- go middle (Middle of the file)

L- go low (Last line of the file)


Moving within a file

k - Move cursor up

j - Move cursor down

h - Move cursor left

l - Move cursor right


Saving and Closing the file

:q! - Quit without saving

:wq - Save the file and quit


Note: You should be in the "command mode" to execute these commands. vim editor is case-sensitive so make sure you type the commands in the right letter-case.



Command reference in vim insert mode.

To change form command mode to insert mode press “i”

Now we can insert any text

Arrow key- to go left , right, up, down

Backspace & Delete key- to delete character

Esc- to return command mode



Some Examples:

1. Create a file ‘fileB” write text content “This is RHEL 82” copy the line and past 5 times then save and exit.


Solution:

Step1: Open the file “fileB” using vim editor

[root@RHEL82 ~]# vim fileB

Step2: Press ‘i” to go insert mode

Step3: Write “This is RHEL8.2”

Step4: Press “Esc” to return command

Step5: Press “yy” from keyboard

Step6: Press “5p” from keyboard

Step7: Press “:wq” from keyboard

Step8: Press "Enter"

Check file content using

[root@RHEL82 ~]# cat fileB



2. Open “fileB” copy last line past 20 times and go to line no 13 and delete line 13.

Step1: Open fileB using vim

Step2: Go to the End of the file by pressing “L”

Step3: Press yy

Step4: Press 20p

Step5: Press “:13” (colon 13)

You are in line no 13.

Step6: press “dd”

Step7: press “:wq” save and exit

Step8: press “Enter”



3. Open fileB and delete first 5 lines

Step1: open fileB using vim

Step2: press “5dd”

(5 lines deleted)

Step3: press “:wq” save and exit

Step4: press "Enter" key


4. Open fileB and add any text at the end of the file

Step1: Open fileB using vim

Step2: press “L” to go end of the file

Step3: press “i” to go insert mode

Step4: Add any text

Step5: Press “Esc” to return command mode

Step6: Press “:wq” to save and exit

Step7: Press "Enter" key

Step8: Check using “cat fileB” command



5. Search “RHEL” in fileB

Step1: Open fileB using vim

Step2: press “/RHEL”

You can see “RHEL” is yellow highlighted


Important Note:

Before editing any configuration file using vim editor better to keep a copy of the file by cp command.


To secure file form unwanted/accidental changes exit from vim editor by ":q!" command


154 views0 comments

Recent Posts

See All

Comments


Log In to Connect With Members
View and follow other members, leave comments & more.
bottom of page