Getting Started with Vi/Vim Editor – Advanced

Vim is a text editor which is improved version of vi editor. Vi is an important text editor because it is usually available in all linux distributions. Full version of vi i.e. vim editor can be installed with apt-get install vim (Debian Based Systems) or yum install vim (RHEL Based systems). So let’s get started with vi/vim editor.

Vi editor contains three modes.

  • Command Mode
  • Insert Mode
  • Ex Mode

Command Mode

Command mode accepts command which are in form of individual characters such as a, i and o. To navigate into files, you can use h, j, k and l in similar way as left, down, up and right arrow keys works respectively in keyboard. below options we can use with vim command mode.

Options Description
h Moves cursor one character to left
j Moves cursor down one line
k Moves cursor up one line
l Moves cursor one character to right
  yank – term used to copy text. Use yy to copy a line of text.
yy Copy entire line the cursor is currently on
2yy Copy the current line of text as well as the line underneath it (2lines)
3yy Copy three lines, 4yy – 4 lines of text .. 5yy .. so on
p Will paste the contents from yanked yy text, starting on the line after your cursor
P Uppercase P will paste the yanked line(s) starting on the line before the cursor (Note: case sensitive)
5G Moves cursor to line 5
5gg Moves cursor to line 5 (Note: case sensitive)
G Moves cursor to the beginning of the last line in the file
1G Moves cursor to the first line of the file
H Moves cursor to the first line on the terminal screen
L Also moves the cursor to the beginning of the last line on the terminal screen
w Jump by start of words (punctuation considered words)
W Jump by lines (downward lines)
e Jump to end of words (punctuation considered words)
E Jump to end of words (no punctuation)
b Jump backward by words (punctuation considered words)
B Jump backward by words (no punctuation)
/pattern Searching forward, /ravi will search the file for ravi you can navigate to the next occurrence using the n key
?pattern Searching backward for pattern
0 (zero) start of line
^ First non-blank character of line
$ End of line
u Undo

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves 4 lines down.

Insert Mode

Insert mode allows you to edit text inside a file. Inserting into insert mode from command mode, you can use below options:

Options Description
i Start insert mode at cursor
I Insert at the beginning of the line
a Append after the cursor
A Append at the end of the line
o Open (append) blank line below current line (no need to press return) (opens to insert text, creates a new line below your current cursor position)
O Open blank line above current line
ea Append at end of word
R Over write existing text when new text added
cw Remove current word cursor is on and insert into insert mode to add text
cc Removes the entire line and places you into insert mode
Esc Exit insert mode

Ex Mode

You can enter into ex mode by typing a : [Colon]. This will open on the bottom of the screen, a command prompt starting with a : . Ex mode is used for saving and manipulating files. It is very powerful mode, you can even browse the linux command line from this mode, loads files into your file, and many more. You must be in command mode before entering into Ex mode.

Options Description
:w Save changes
:wq Save changes and quits
:q Quits without saving changes
:e /file.txt Will load the new file.txt into the vi editor to edit it. This will only occur if the current
changes to the file being edited are saved
:r Allows to bring the content of an old file into a new one.
:! Allows you to run shell commands from within vi. For instance, !ls /etc

Searching and Replacing Text – Vi/Vim allows to search and replace text as well.

Options Description
:%s/ravi/root Replaces the first occurrence of “ravi” on each line of the file with “root”.
:%s/ravi/root/g Replaces all occurrences of “ravi” with “root”; adding g means “global”.
:%s/ravi/root/gc Replaces all occurrences of “ravi” with “root”; throughout file with confirmation
:2,35s/old/new/g Replace all occurrences between lines 2 and 35
:5,$s/old/new/g Replace all occurrences from line 5 to EOF
:%s/^/hello/g Replace the beginning of each line by “hello”
:%s/$/Honey/g Replace the end of each line by “Honey”
:%s/onward/forward/gi Replace “onward” by “forward” , case insensitive
:%s/ $//g Delete all white spaces
:g/string/d Delete all lines containing “string”
:v/string/d Delete all lines containing which didn’t contain “string”
:s/Jack/Jill/ Replace the first occurrence of “Jack” by “Jill” in current line
:s/Jack/Jill/g Replace “Jack” by “Jill” in current line
:%s/Jack/Jill/g Replace “Jack” by “Jill” in all the file
:%s/\r//g Delete DOS carriage returns (^M)
:%s/\r/\r/g Transform DOS carriage returns in returns
:%s#]+>##g Delete HTML tags but keeps text
:%s/^(.)\n\1$/\1/ Delete lines which appears twice
Ctrl+a Increment number under the cursor
Ctrl+x Decrement number under cursor
ggVGg? Change text to Rot13

Other Important Options

Options Description
:set all Display all settings
:set Display settings different than default
:set nu Display line numbers
:set nonu Hide line numbers
:!pwd Execute the “pwd” unix command, then returns to Vi
:set ai sets autoindent on
:set noai sets autoindent off
/jo[ha]n Search “john” or “joan”
/\< the Search “the”, “theatre” or “then”
/the\> Search “the” or “breathe
/Jack\|Jill Search “Jack” or “Jill”
/^\n{3} Find 3 empty lines
/\(\<\w\+\>\)\_s*\<\1\> Finding duplicate words
\<fred\> Search “fred” but not “alfred” or “frederick”


Hence, you got proper understanding the options usage in vim editor. Stay tuned for next tutorial. Thanks!!

Read Also : How to Install CentOS 7 (Step by Step with Screenshots)

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Recommended Reading..

1 thought on “Getting Started with Vi/Vim Editor – Advanced”

Leave a Comment