Learn Linux: use vi command
Vi command overview
In other post, we showed you some most useful Linux commands, you can check that article here. Today, we will show you detail about a very useful command when we working with linux. That is vi command. VI command is on almost every UNIX and Linux system. This tutorial will show you how to
- Navigate a document using vi command
- Use basic vi modes
- Insert, edit, delete, copy and find text by vi command
You must have a basic knowledge of linux and a working Linux system or you can go here to practice online. Sometimes different versions of a program will bring out differently output, so your output may not always look exactly like the example show here.
Let’s figure out vi command in detail
vi filename E.g: vi myfile.txt
Pressing Esc will leave
insert mode and return you to
command mode where these commands should work.
:q! Quit editing the file and skip all changes.
:wq! Write the file and quit whether modified or not.It's generally safer to omit the ! unless you know what you're doing.
:! Run a shell command. E.g: :! ls After hit Enter the ls result will show and hit enter again to return vi editing.
:e! This will reload the file, discard all changes you have made.
ZZ Write the file if it has beend modified. Then exit. This is very common way for normal vi exit.
1. When you type (:), the cursor will move to the bottom line of your screen then you can type in the command and parameters.
2. If you omit the (!) from the command. you may receive an error message such as saying changes have not beend saved or the output file cannot be written
3. The (:) commands have longer form are (:quit, :write, :edit). Use it if you want
These commands help you move around in a file
h Move left one character on the current line
j Move down to the next line
k Move up to the previous line
l Move right one character on the current line
w Move to the next word on the current line
e Move to the next end of word on the current line
b Move to the previous beginning of the word on the current line
Ctrl + f Scroll forward one page
Ctrl + b Scroll backward one page
If you type a number before any of these commands, then the command will be executed that number of times. E.g: 5l will move right 5 characters.
Moving to lines
G Moves to a specific line in your file. E.g: 3G will move to line 3. With no parameter, G will go to the last line of the file
H Moves relative to the top line on the screen. E.g: 3H moves to the line currently to the 3rd line from the top of your screen.
L Is like H, except that movement is relative to the last line on screen E.g: 3L moves to the line currently to the 2nd line from the bottom of your screen.
Search text and line
/search_string Use / to search forward for ocurrence of string in text
?search_string Use ? to search backward for ocurrence of string in text
n Move to the next occurrence of search string
N Move to next occurrence of search string in opposite direction
:.= Returns line number of current line at bottom of screen
:= Returns the total number of lines at bottom of screen
Ctrl + g Provides the current line number, along with the total number of lines, in the file at the bottom of the screen
Each of these commands puts the vi editor into insert mode; thus, the key must be pressed to terminate the entry of text and to put the vi editor back into command mode.
i Insert text before cursor, until hit
I Insert text at beginning of current line, until hit
a Append text after cursor, until hit
A Append text to end of current line, until hit
o Open and put text in a new line below current line, until hit
O Open and put text in a new line above current line, until hit
x Delete single character under cursor. If you type a number before x, it will delete that number of characters starting with character under the cursor
dw Delete the single word beginning with character under cursor. dNw (N is a number), this will delete N words beginning with character under cursor; E.g, d5w deletes 5 words
D Delete the remainder of the line, starting with current cursor position
dd Delete entire current line. Ndd (N is a number), this will delete N lines, beginning with the current line; E.g., 5dd deletes 5 lines