Understanding VI editor
VI editor is a screen-oriented text editor written by Bill Joy in 1976. This is the most commonly used editor for editing files in Linux.
VI Editor Modes
Three modes are commonly using in VI editor. They are:
- Insert Mode
- Command Mode
- Ex-Mode (Extended Command Mode)
Insert Mode
In Insert Mode, we can perform the operations like adding, deleting and editing the existing text of a file.
i Inserts the text at the current cursor position
I Inserts the text in beginning of the line
a Adds the text after the current cursor position
A Adds the text at the end of a line
o Inserts the text one line below current cursor position
O Inserts the text one line above current cursor position
Command Mode
This is the default mode of VI editor in which we can perform the operations like Copy, Paste and Delete the lines.
dd Deletes a line
ndd Deletes 'n' lines
dw Deleted a word next to the cursor
yy Copies a line
nyy Copies 'n' lines
yw Copies a word next to the cursor
p Pastes the deleted or copied text
u Undo (you can undo 1000 times)
Ctrl+r Redo
G Move cursor to the last line of the file
/<word> Finds a word (Press 'n' for next)
Ex-Mode (Extended Command Mode)
The Ex mode is similar to the command mode as it also allows you to enter Ex commands. It is optimized for batch processing.
:q Quit without saving
:q! Quit forcefully without saving
:w Write (Save)
:wq Save and quit
:wq! Save and quit forcefully
:se nu Sets the line numbers
:se nonu Removes line numbers
:24 Cursor goes to line 24
:wn Save and switch to next file
:rew Change last file (rewind)
That’s it!