Sunday, April 21, 2013

Vi - editor

Vi is a display oriented text editor based on ex.
We can start vi with following syntax :
vi [-c command] [-r filename] [-w size] [file 1] [file 2] [file n]
With -c command we can execute command when vi start.
-r filename is specified for recover the modify that we didn't save (for example if system crash).
-w size : specify the size of the editing window for visual mode.
We can open a file in read-only mode using -R option.
We can specify more than one file. Vi open all file and display the first specified file. We can go to following file with command :n
Vi has three mode:
-COMMAND MODE : we can only specify the commands.
-INPUT MODE
-DIRECTIVE MODE : in this mode we can asd to vi all commands.
When we launch vi we are in COMMAND MODE. We can switch from COMMAND MODE to INPUT MODE typing : o -> insert characters under current line
O -> insert characters above current line
R -> replace mode
i -> insert characters to the left of cursor
I -> insert characters at the start of line
a -> insert characters to the right of cursor
A -> insert characters at the end of line
Instead we can switch from INPUT MODE to COMMAND MODE with
We can switch from COMMAND MODE to DETECTIVE MODE typing :
?
/
:
Instead we can switch from DETECTIVE MODE to COMMAND MODE with

Moving :
h -> left
j -> under
k -> above
l -> right
G -> move to last line
nG -> move to n line (ex : 2 G -> move to line 2)
What is a word for vi?
A word is any sequence of characters.
What is a phrase for vi?
A phrase is any sequence of characters that end with ".", "!","?", double space.
What is a paragraph for vi?
A paragrapth is delimited by blank line.
{ -> Move cursor to the start of paragraph
} -> Move the cursor to the end of paragraph
( -> Move the cursor to the start of current phrase
) -> Move the cursor to the end of current phrase
$ -> Move the cursor to the end of line
^ -> Move the cursor to the first non-blank character of current line
0 -> Move the cursor to the start of current line
n| -> Move the cursor to n character of current line (ex : 3| -> move to third character of current line)
w -> Move the cursor to the start of next word
e -> Move the cursor to the end of next word
b -> Move the cursor to the start of last word
+ -> Move the cursor to the first non-blank character of next line
- -> Move the cursor to the first non-blank character of last line

EDIT
^D -> Remove single tab
^^D -> Remove all tab
^W -> Delete last word we insert
^U -> Delete last line we insert
^H -> Backspace
ZZ -> Update file and exit
:q[!] -> Force quit
:wq -> Update and exit
u -> undo the last modify
U -> undo all modify done to current line
ndd -> Delete n lines (default current line)
d/str -> Delete while find str (going forward)
d?str -> Delete while find str (going back)
D -> Delete from current position to end of current line
P -> paste buffer before the cursor
p -> paste buffer after cursor
f char -> find the next occurrence of char
F char -> find the occurence of char before the current position
t char -> move the cursor to the next occurrence of char
T char -> move the cursor to the occurrence of char before current position
We can repeat the last command typing n
Double ">" -> orizontal tab
Double "<" -> remove orizontal tab

Commands in directive mode
With this commands we can control the files.
:args -> show files that we're editing (we can open more than one file!)
:n -> edit next file
:n filename -> edit filename
If we specified [!] we force the commands so vi don't update file.
:rew -> restart to edit first file (we can specify [!])
:st[op] -> Vi goes in backgroud and we return to shell. If we want return in vi type fg vi

For more information see the manual of vi (man vi)

If you have any problem or you need some explanation just write under this post!

No comments:

Post a Comment