29 8 月 2012

vi – Command Mode

introduction to vi text editor command mode

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-f Page down.
ctrl-b Page up.
ctrl-f Page down.
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)

introduction to vi text editor command mode and cut, copy and paste functions
Except for the basic commands to move cursor, vi also provides more complicated commands to execute other general editor functions like cut, copy, paste and etc. under Command Mode.

vi – Command Mode (Undo, Redo, Search)

vi text editor command mode undo, redo and search function
vi has 36 registers for a space to store data that was cut or copied. So ”bdd (quotation mark is a part of command) refers to cutting one line that is stored in Register “b.”

vi – Command Mode (Search and Replace Word String)

vi text editor command mode string replace function
:[< n >,< m >]s/< String 1 >/< String 2 >/[gi]
[< n >,< m >] –

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,$ >

Example:
:s/foo/Foo/
:1,10s/foo/Foo/g
:1,55s/foo/Foo/gi
:%s/tcp/TCP/g

vi – Command Mode (Set Function)

vi text editor command mode set function
Other functions:

: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)

introduction to vi text editor command mode and its display, hidden and character functions

: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.