What is VIM?
VIM is a powerful editor based on vi, which exists on most platforms and comes pre-installed on most UNIX machines. On Linux platforms, vi is aliased to invoke vim.
Moving Cursor
Commands to move cursor around your terminal or window:
gg
| first line of a file
|
G
| last line of a file
|
^
| beginning of a line
|
$
| end of a line
|
h
| previous character (left)
|
l
| next character (right)
|
k
| previous line (up)
|
j
| next line (down)
|
Note that, if you enter a numerical value before typing the command letters, the action is repeated. For example, to move 10 lines up, type 10k.
If you want to go to a specific line number in your file, type line numberG.
Searching
To search for a pattern:
After typing and hitting the Enter. key, your cursor will stop at beginning of the first match from the last cursor position. To cycle forward through all of the matched patterns, type n. To cycle backward, type N.
Selecting
To select characters covered by motion:
vmotion
To select everything:
ggvG$
Here, gg moves to beginning of file, v selects, G$ motion covers last character of last line in the file.
Editing
To insert new texts at current cursor position, type i; to insert after current cursor position, type a. To append new texts at end of line, type A. Once you enter insert/append mode, you will remain in that mode until you hit the Esc key.
As an aside, ctrl + [ is also the same as the Esc key. But you don't want to use it too often, because you may get the Emacs claws; just because you can doesn't mean you have to.
To delete character at current cursor position, type x; to delete previous character type X. To delete one line type dd.
To copy one line, type Y; if you have made a selection already, type y to copy your selection. To paste what you copied, type P to insert before current cursor position; type p to append after current cursor position.
As with most vi commands, typing a number before typing commands repeats the action the number of times you specify. So to type:
blah blah blah blah blah
I just need to type the following sequence: `5iblah ` and hit Esc key twice. 5 for the number of repeats, i to enter insert mode.
Global Replace
Global replace target with replacement, both works the same way:
:%s/target/replacement/g
:g/target/s//replacement/g
Line Justification
Line justification (useful if you use vim as an editor for e-mails, such as from within pine) for texts covered by motion to fit a terminal of 80 columns:
Followed by:
gqmotion
(For example, motion here can be G$, to cover from present cursor location till end of file.)
Bookmarks
Mark current position with label (a key) and then recall it (bring cursor back to the marked position):
mlabel
:
:
`label
Block Indent
First select the text you want to indent, then use > and < (shift and . or ,) keys to indent right and left. == will indent a range of text automatically.
Folding
To enable syntax folding, use command set foldmethod=syntax. zo/zO opens one/all fold levels. zc/zC closes one/all fold levels. zm/zM folds one/all levels deeper. zr/zR reduces one/all fold levels.
Editor Configuration
When you work with terminals, you may want to tell vim to use 2 space characters are used for each tab:
To toggle line numbers, type:
Other useful commands:
-
set nu / set nonu to display/hide line numbers.
-
set list / set nolist to display/hide non-printable characters.
-
set ic / set noic to perform non-case-sensitive/case-sensitive search.
References