Do you know all about vi Editor?
Tejas Gupta
Building PW??★AWS Community Builder ★ Speaker ★ AWS/Azure/Alibaba Certified ★ Redhat Certified ★Cloud & DevOps Engineer ★ AWS UG Mumbai ★ Content Creator
Vi or the Visual Editor is the default text editor that comes with most Linux systems. It is a Terminal-based text editor that users need to learn, essentially when more user-friendly text editors are not available on the system.
Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved.
How to use vi editor
vi <filename>
Creates a new file if it already does not exist, otherwise opens an existing file.
vi -R <filename>
Opens an existing file in the read-only mode
Modes of Operation in vi editor
- COMMAND MODE
The vi editor opens in this mode, and it only understands commands In this mode, you can, move the cursor and cut, copy, paste the text . This mode also saves the changes you have made to the file . Commands are case sensitive . This mode is where vi interprets any characters we type as commands and thus does not display them in the window
2. INSERT MODE
This mode is for inserting text in the file. To enter text, you must be in insert mode. To come in insert mode you simply type i. Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working. To get out of insert mode, press the Esc key, which will put you back into command mode.
Saving and Closing the file
Shift+zz - Save the file and quit
:w - Save the file but keep it open
:q - Quit without saving
:wq - Save the file and quit
Copying (Yanking)
To copy text, place the cursor in the desired location and press the y key followed by the movement command. Below are some helpful yanking commands:
yy - Yank (copy) the current line, including the newline character.
3yy - Yank (copy) three lines, starting from the line where the cursor is positioned.
y$ - Yank (copy) everything from the cursor to the end of the line.
y^ - Yank (copy) everything from the cursor to the start of the line.
yw - Yank (copy) to the start of the next word.
yiw – Yank (copy) the current word.
y% - Yank (copy) to the matching character. By default supported pairs are (), {}, and []. Useful to copy text between matching brackets.
Cutting (Deleting)
In normal mode, d is the key for cutting (deleting) text. Move the cursor to the desired position and press the d key, followed by the movement command. Here are some helpful deleting commands:
dd - Delete (cut) the current line, including the newline character.
3dd - Delete (cut) three lines, starting from the line where the cursor is positioned,
d$ - Delete (cut) everything from the cursor to the end of the line.
The movement commands that apply for yanking are also valid for deleting. For example dw, deletes to the start of the next word, and d^ deletes everything from the cursor to the start of the line.
Pasting (Putting)
To put the yanked or deleted text, move the cursor to the desired location and press p to put (paste) the text after the cursor or P to put (paste) before the cursor.
Moving within a file
k - Move cursor up
j - Move cursor down
h - Move cursor left
l - Move cursor right
Other useful commands
a - Write after cursor (goes into insert mode)
A - Write at the end of line (goes into insert mode)
ESC - Terminate insert mode
u - Undo last change
U - Undo all changes to the entire line
o - Open a new line (goes into insert mode)
cw - Change word
x - Delete character at the cursor
r - Replace character
R - Overwrite characters from cursor onward
s - Substitute one character under cursor continue to insert
S - Substitute entire line and begin to insert at the beginning of the line
~ - Change case of individual character
$ - Positions cursor at end of line.
W - Positions cursor to the next word.
B - Positions cursor to previous word.
( - Positions cursor to beginning of current sentence.
) - Positions cursor to beginning of next sentence.
H - Move to top of screen.
nH - Moves to nth line from the top of the screen.
M - Move to middle of screen.
L - Move to bottom of screen.
nL - Moves to nth line from the bottom of the screen.
colon along with x - Colon followed by a number would position the cursor on line number represented by x.
w! fileName - Overwrite to file called file Name (save as forcefully).
!cmd - Runs shell commands and returns to Command mode.
Tejas Gupta
Systems Engineer at Tata Consultancy Services | 3x AWS certified | 3x RedHat Certified | AWS Certified Developer Associate | RedHat Certified Engineer | RedHat Certified System Administrator
3 年It's quite useful
DevOps Engineer
3 年Very much helpful
AWS Community Builder | DevOps Aspirant | ARTH Learner Openshift | Ansible | Kubernetes | Docker | Jenkins | Full Stack Web Developer
3 年Thank you for sharing, turned out be actually very useful.
Actively Looking for Change | DevOps Engineer | CKA | AWS SAA-C02 | RHCSA | RHCE | Azure | Docker | Kubernetes | Terraform | Ansible
3 年Very much useful ??