Vimtutor is all you need
How to learn Vim? Vimtutor!
Configure vim
Learn Vim
For learning vim editor, you need:
1 | vimtutor |
It has seven lession and may take 25 ~ 30 minutes for finish the exercise in vim tutor.
Actually you may need nearly 2 hours for the first exercise, but it is worthy.
Lesson 1
The cursor is moved using either the arrow keys or the hjkl keys.
h
(left)j
(down)k
(up)l
(right)
To start Vim from the shell prompt type:
1
vim filename
To exit Vim:
type: <ESC>
:q!
<ENTER> to trash all changes.type: <ESC>
:wq
<ENTER> to save the changes.To delete the character at the cursor type:
x
To insert or append text type:
i
type inserted text <ESC> insert before the cursorA
type appended text <ESC> append after the cursor
NOTE: Pressing <ESC> will place you in normal mode or will cancel an unwanted and partially completed command.
Lesson 2
To delete:
from the cursor up to the next word, type:
dw
dw
means cut from here to next wordfrom the cursor to the end of the word, type:
de
de
means cut from here to the end of the current wordfrom the cursor to the beginning of a line, type:
d0
from the cursor to the **end **of a line, type:
d$
the whole line, type:
dd
To move the cursor:
w
,e
,0
,$
these four motion preformance as “delete” described above.
To repeat a motion prepend it with a number:
number motion
The format for a change command is:
operator [number] motion
operator - is what to do, such as
d
for delete
[number] - is an optional count to repeat the motion
motion - moves over the text to operate on, such asw
(word),$
(to the end of line), etc.To undo:
- a previous actions, type:
u
(lowercase u) - all the changes on a line, type:
U
(capital U) - the undo’s, type:
CTRL-R
- a previous actions, type:
Lesson 3
To put back text that has just been deleted, type
p
.This puts the deleted text after the cursor.
If a line was deleted it will go on the line below the cursor.
To replace the character under the cursor, type
r
and then the character you want to have there.The change operator allows you to change from the cursor to where the motion takes you.
ce
to change from the cursor to the end of the wordc$
to change to the end of the line.
The format for change:
c [number] motion
Lesson 4
To show location in the file, type
CTRL-G
.G
moves to the end of the file.#G
moves to the # line.# is a number of the target line
gg
moves to the first line of the file.
To search forward a phrase, type
/phrase
.To search backward a phrase, type
?phrase
.n
for find next in same direction.N
for find next in opposite direction.CTRL-O
takes back to older positions.CTRL-I
takes to newer positions.
To goes to the (), [], {} matched, type
%
.To substitute the phrase, type
:s/old/new
.For all
:
commands must be finished by hitting<ENTER>
for all substitute on a line, type
:s/old/new/g
for all substitute between two line, type
:#,#s/old/new/g
#, # means two line numbers, maybe need
CTRL-G
to confirm them.for all substitute in file, type
:%S/old/new/g
for ask for confirmation eachtime add ‘c’
:%s/old/new
Lesson 5
To executes an external command, type
:!<command>
Some file IO command:
To read a external disk file into current file, type
:r <filename>
To write a external disk file from current file, type
:w <filename>
This command would write the whole file into target disk file.
When you want to read, <filename> also can be redirected, like
:r !ls
This command means read the output of shell command
ls
into current vim file.When you want to write,
v
into visual mode andmotion
select any text, then:w <filename>
Just save you select in visual mode.
Lesson 6
- To **open **(new) a line and start insert mode:
- Type
o
to open Below the cursor - Type
O
to open Above the cursor
- Type
- Combine locate and insert motion:
- using
w
andi
, you can move cursor to the first of word and insert before it. - using
e
anda
, you can move cursor to the end of word and insert (or append) after it.
- using
- To yank (copy) and puts (paste)
- Type
y
andp
. - you can compose them with other motion, like
yw
can copy one word.
- Type
- To enter replace mode, type
R
and you can directly cover the previous text by type in new text. - To set option, using
:set <option>
ic
orignorecase
, it ignore upper/lower case when searching.is
orincsearch
, it show partial matches for a search phrases.hls
orhlsearch
, it highlight all matching phrases.
- To unset option, just prepend “no”, like
:set noic
Lesson 7
- To Get help, type
:help
- Jump between two windows using
CTRL-W CTRL-W
- Quit by
:q
- Jump between two windows using
- To Configure Vim, you need to create a .vimrc file
- The example vimrc file:
$VIMRUNTIME/vimrc_example.vim
- The example vimrc file:
- When typing a
:
command, press CTRL-D to see possible completions.- Press <TAB> to use one completion.
Other Feature
Fold by syntax
In ~/.vimrc
1 | set foldmethod=syntax |
The most useful commands for working with folds are:
z o
opens a fold at the cursor.z Shift+o
opens all folds at the cursor.z c
closes a fold at the cursor.z m
increases thefoldlevel
by one.z Shift+m
closes all open folds.z r
decreases thefoldlevel
by one.z Shift+r
decreases thefoldlevel
to zero – all folds will be open.