Working With Vi
Working With Vi
Working With Vi
General Commands
vi [filename]
Starts the vi editor (if filename is given open that file into the editor).
vi -R <filename>
Open a file in read-only mode.
view <filename>
[Esc]
Return to the command mode.
:!<command>Execute shell commands within vi (Press to return to vi).
:sh
Create a shell within vi. Type [Ctrl]+D or exit to return to vi.
^Z
Suspend vi and return shell prompt. Type fg to return to vi.
:pre / :preserve
Force the system to save current buffer to disk.
Keystroke
Action
ZZ / :wq / :x
:w [filename] *
:w!
:q
:q!
:e!
Movement
h j
k
l
n{h,j,k,l} where n=1,2,3,...
:set wm=n
:set nu!
G
nG
Action
d*
Delete text
w word after the current cursor position (including the space before next word)
e to end of the word
E to end of word including punctuation
b word before the current cursor position
d current line
0 to the beginning of the line from previous character
$ - to the end of line from current cursor position
D (d$)
Delete text to end of line from current cursor position (multiply by n also work)
c*
Change text
w word
$ - to the end of line from current cursor position
0 from previous character to beginning of the line
l current letter
c current line
C (c$)
Replace character 3
Enters the overstrike mode (Press to add a new line or [Esc] to exit the
overstrike mode)
s / c[SAPCE]
S / cc
Y (yy)
Paste text
- for words/letters paste after cursor position
- for lines of text paste below the current line
Paste text
- for words/letters paste before cursor position
2 of 5
Repeat commands
Restore current line to original state before changed (if the cursor is on that line)
^@
^R
^B
^D
^U
^E
^Y
z.
z-
^L
nH
nL
^ (hat)
n|
[[
]]
* Numeric prefix has no meaning in this context. If specified it is considered as a line number.
e.g.: nz move line n to the top of screen
** Ignore spaces or tabs
*** Vi find end of a sentence by . ? !. It locates end of sentence when the punctuation is followed by
at least two spaces or when it appears as the last non-blank character on a line.
*** A paragraph is defined as text up-to the next blank line or default paragraph macros. Sections
are defined as default section macros.
e.g.: 3) moves ahead three paragraphs
d) delete to end of current sentence
2y} yank two paragraphs ahead
Movement with Searches
/text
?text
* :set nowrapspan disable wrap around file when the search fails
* c/d commands can combined with searches to delete/change text on character basis.
e.g.: d?text delete from before the cursor up-to word text
fx
Fx
tx
Tx
Go to nth line
Go to last line
``
Return to original position where G command issued. If any editing done return to
the last edit.
For / or ? return to where the search started.
5 of 5