Linux Editor ( VI)

The default editor that comes with the Linux operating system is called vi (visual) editor.There are many ways to edit the file but the best way is using vi editor.

The vi editor is a full screen editor and it has two modes.

1.Command Mode


This mode enables you to perform tasks such as saving files, moving the cursor, cutting, copy and pasting lines or words, and finding and replacing. In this mode, whatever you type is executed as a command.

2.Insert Mode


This mode enables you to insert text into the file. Everything that’s typed in this mode is inserted in the file .

By default it is in command mode at the time editing a file with vi editor. To insert any record press ‘i’ to insert mode. After that comeback to command mode by pressing ESC button.

How to edit sample.txt with vi editor

$ vi sample.txt

 ~

 ~

 ~

 ~

 ~

 "sample.txt" [New File]

How to quit from vi editor

Press ESC + :q whenever no changes in the file. If any modifications are done, press ESC + :q!. To quit from vi by saving changes in the file, press ESC + :wq

This is a sample text page.

 ~

 ~

 ~

 :wq
Editing Commands
Command Description
i Inserts text before current cursor location.
I Inserts text at beginning of current line.
a Inserts text after current cursor location.
A Inserts text at end of current line.
o Creates a new line for text entry below cursor location.
O Creates a new line for text entry above cursor location.
Deleting Commands
x Deletes the character under the cursor location.
X Deletes the character before the cursor location.
dw Deletes from the current cursor location to the next word.
d^ Deletes from current cursor position to the beginning of the line.
d$ Deletes from current cursor position to the end of the line.
D Deletes from the cursor position to the end of the current line.
dd Deletes the line the cursor is on.
Change Commands
cc Removes contents of the line, leaving you in insert mode.
cw Changes the word the cursor is on from the cursor to the lowercase w end of the word.
r Replaces the character under the cursor. vi returns to command mode after the replacement is entered.
R Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
s Replaces the current character with the character you type. Afterward, you are left in insert mode.
S Deletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode.
Copy and Past Commands
yy Copies the current line.
yw Copies the current word from the character the lowercase w cursor is on until the end of the word.
p Puts the copied text after the cursor.
P Puts the yanked text Before the cursor.
Moving commands
k Moves the cursor up one line.
j Moves the cursor down one line.
h Moves the cursor to the left one character position.
l Moves the cursor to the right one character position.
0 or | Positions cursor at beginning of line.
$ 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.
E Move to the end of Blank delimited word
{ Move a paragraph back
} Move a paragraph forward
[[ Move a section back
]] Move a section forward
n| Moves to the column n in the current line
1G Move to the first line of the file
G Move to the last line of the file
nG Move to nth line of the file
:n Move to nth line of the file
fc Move forward to c
Fc Move back to c
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 botton of screen
nL Moves to nth line from the bottom of the screen
: x Colon followed by a number would position the cursor on line number represented by x
Control Commands
CTRL+d Move forward 1/2 screen
CTRL+d Move forward 1/2 screen
CTRL+f Move forward one full screen
CTRL+u Move backward 1/2 screen
CTRL+b Move backward one full screen
CTRL+e Moves screen up one line
CTRL+y Moves screen down one line
CTRL+u Moves screen up 1/2 page
CTRL+d Moves screen down 1/2 page
CTRL+b Moves screen up one page
CTRL+f Moves screen down one page
CTRL+I Redraws screen
Searching commands
^ Search at the beginning of the line.
. Matches a single character.
* Matches zero or more of the previous character.
$ Search at End of the line
[ Starts a set of matching, or non-matching expressions.
< Put in an expression escaped with the backslash to find the ending or beginning of a word.
> Put in an expression escaped with the backslash to find the ending or beginning of a word
Replacing Text
:s/ replacing command
Running Commands
:! to run the command with in the vi editor.
:! ls to see the out put of the running command.
Set Commands
:set ic Ignores case when searching
:set ai Sets autoindent
:set noai To unset autoindent.
:set nu Displays lines with line numbers on the left side.
:set sw Sets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4
:set ws If wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
:set wm If this option has a value greater than zero, the editor will automatically “word wrap”. For example, to set the wrap margin to two characters, you would type this: :set wm=2
:set ro Changes file type to “read only”
:set term Prints terminal type
:set bf Discards control characters from input
Other Commands
J Join the current line with the next one. A count joins that many lines.
<< Shifts the current line to the left by one shift width.
>> Shifts the current line to the right by one shift width.
~ Switch the case of the character under the cursor.
^G Press CNTRL and G keys at the same time to show the current filename and the status.
U Restore the current line to the state it was in before the cursor entered the line.
u Undo the last change to the file. Typing ‘u’ again will re-do the change.
:f Displays current position in the file in % and file name, total number of file.
:f filename Renames current file to filename.
:w filename Write to file filename.
:e filename Opens another file with filename.
:cd dirname Changes current working directory to dirname.
:e # Use to toggle between two opened files.
:n In case you open multiple files using vi, use :n to go to next file in the series.
:p In case you open multiple files using vi, use :p to go to previous file in the series.
:N In case you open multiple files using vi, use :N to go to previous file in the series.
:r file Reads file and inserts it after current line
:nr file Reads file and inserts it after line n.

 

Advertisements