Work With vi Editor Like a Pro

1. vi Editor

In the 90s, when visual editors were introduced, the popularity of command-line editing tools like the vi editor started declining. Then in the first decade of this century came the IDEs (Integrated Development Environment). Eclipse made IDE attractive and popular among developers. Developers of the current generation prefer an IDE to plain visual text editors and most of them do not even know about the existence of the vi editor. However, people working in system engineering in Linux and Mac still prefer vi. In this article, I’ll highlight 20 commands when practiced, will enable you to use the vi editor like a pro.



vi editor on Mac
vi editor on Mac

2. Modes

The improved version of the vi editor is vim (vi improved). All the vi commands are supported by vim as well. There are two modes in the vi editor:



  • Command mode – This mode allows to perform tasks like cut, copy, paste, delete, undo, redo, etc.
  • Insert mode – In this mode, it directly adds texts to the file.

3. Commands

To open a file in vi, use the command vi file_name.

SL#PurposeCommand
1Switch ModeEsc to switch to command mode
i to switch to insert mode and add characters before the current position
I to switch to insert mode but move the cursor to the beginning of the line
a to switch to insert mode and add characters after the current position
A to switch to insert mode but move the cursor to the end of the line
2Go to LineG to go to the last line
gg to go to the first line
nG to go to the nth line. Example: 120G
:line_number to go to a specific line. Example: :120
3Undo and Redou or :u or :undo for undoing the last change
. to repeat the last undo operation
:Nu for undoing last N changes. Example: :2u
Ctrl + r for redoing the last change
4Movehjkl to move left, down, up, and right
w to move one word forward
b to move one word backward
0 to move to the beginning of the line
$ to move to the end of the line
H to move to the top of the screen
M to move to the middle of the screen
L to move to the bottom of the screen
Ctrl + f for page down
Ctrl + b for page up
5Copy and Pasteyy or Y to copy the current line to the clipboard
Nyy to copy N lines to the clipboard. Example: 10yy
yw to copy the current word. Example: 2yw or y2w
yl to copy the current letter
p to paste clipboard contents after the current line
P to paste clipboard contents before the current line
6Deletex to delete the character at the current cursor position
X to delete the character before the current cursor position
dw to delete one word
dd to delete the current line
d$ or D to delete from the current position to the end of the line
d^ to delete from the current position to the beginning of the line
7Add a lineo to add a line after the current line
O to add a line before the current line
8Find and Replace/search_text for search forward
?search_text for search backward
n repeats the last search in the forward direction
N repeats the last search in the backward direction
:s/search_text/replace_text to replace the first occurrence of search_text by replace_text
:s/search_text/replace_text/g to replace all occurrences of search_text by replace_text in the current line
:%s/search_text/replace_text/g to replace all occurrence of search_text by replace_text in the entire file
9Save and Quit:w to save
:q to quit
! to force either saving or exiting
:wq to save and quit
:q! to force quit without saving
10Match Brackets% to match brackets ( with ) and { with }
11Shift<< Shifts line left
>> Shifts line right
12Options:set option to set an option
:set number or :set nu to show line numbers
:set autoindent for automatic line indentation
:set showmatch to show bracket matches ( with ) and { with }
:set autowrite to write before quit
:set cursorline to highlight the current line
13ConfigurationTo permanently configure options open vi ~/.vimrc and add one or more lines to it:
syntax on for syntax highlighting
:set number to show line numbers
Set other options mentioned in #12 as needed.
vi top commands
Work With vi Editor Like a Pro
Be The First

Join our list to get instant access to new articles and weekly newsletter.

Tagged on:                     
0 0 votes
Article Rating
Subscribe
Notify of
guest

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments

[…] To work with vi editor like a pro, refer this article: Work With vi Editor Like a Pro. […]

[…] In this article, you have learned how to show line numbers in the vi editor temporarily for a single session or permanently. To work with vi editor like a pro, refer to this article: Work With vi Editor Like a Pro. […]

[…] highlighting in the vim ad vi editor. To work with vi editor like a pro, refer to this article: Work With vi Editor Like a Pro. To know how to go to a specific line in vi editor refer to this article: vi Show Line […]

3
0
Would love your thoughts, please comment.x
()
x