The way to move cursor
vi is a screen-oriented text editor so we can only move cursor with keyboard. We therefore must know how to move cursor and it’s only workable under Command Mode.
Warning: Do not use↑↓←→keys as possible
The usage of lowercase h, j, k, l can only move cursor one space each time, which is too inefficient, but vi also provides other convenient ways to fast move cursor. See the following table:
|Key method in command mode||Cursor Behavior|
|gg||Move to first line of file.|
|< n >G||Move to < n > line of file.|
|G||Move to last line of file.|
|H||Move to first line of current page.|
|L||Move to last line of current page.|
|b||Move to first character of last word string.|
|B||Move to first character of last word string, which is separated by space or tab.|
|e||Move to last character of next word string.|
|E||Move to last character of next word string, which is separated by space or tab.|
|w||Move to first character of next word string.|
|W||Move to first character of next word string, which is separated by space or tab.|
|ctrl-g||Display current status (cursor located line, mode and etc.)|
|0||Move to beginning of current line.|
|^||Move to first character of current line.|
|$||Move to last character of current line.|
|< n >$||Move to last character of < n > line following current line.|
|ZZ||Save & quit.|
vi – Command Mode (Cut, Copy and Paste)
vi – Command Mode (Undo, Redo, Search)
vi – Command Mode (Search and Replace Word String)
Select target line numbers:
Only the line where the cursor is located is targeted if no < n > and < m >assigned.
10,100 – from Line 10 to Line 100
$ – refers to the last line of whole content
% – whole contents
< String 1 > – Target word string
< String 2 > – Replacing word string
[gi] – Global: each line targeted; one more String 1 in the identical line will be all replaced
It really takes much time to replace one text string to another text string each by each if for massive file contents. So vi provides a quite efficient method to replace text strings.
Word string replacement:
:[< n >,< m >]s/< String 1 >/< String 2 >/[gi]
< String 1 > – target word string
< String 2 > – replacing word string
If no < n > and < m > assigned, Only the line where the cursor resides is targeted.
s – substitute
g – global; only first matching word string is replaced if no “g” assigned.
i – ignore capital and lowercase
< n > – starting line
< m > – ending line; $ can refer to the last line of whole content
% – whole content, the same as < 1,$ >
vi – Command Mode (Set Function)
:set< function >
We can type in “:set all” to demonstrate all set functions.
:set number – display line number; use “:set no number” if for canceling line number display.
vi – Command Mode (Display Hidden Characters)
:set list – Display hidden character (tab or ending symbol); use “:set nolist” if for canceling hidden character display.
:set nohlsearch – The target strings are highlighted when proceeding search under Command Mode. Using such command can cancel highlight function.