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:
xTo insert or append text type:
itype inserted text <ESC> insert before the cursorAtype 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:
dwdwmeans cut from here to next wordfrom the cursor to the end of the word, type:
dedemeans cut from here to the end of the current wordfrom the cursor to the beginning of a line, type:
d0from 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 motionThe format for a change command is:
operator [number] motionoperator - is what to do, such as
dfor 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
rand then the character you want to have there.The change operator allows you to change from the cursor to where the motion takes you.
ceto 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.Gmoves to the end of the file.#Gmoves to the # line.# is a number of the target line
ggmoves to the first line of the file.
To search forward a phrase, type
/phrase.To search backward a phrase, type
?phrase.nfor find next in same direction.Nfor find next in opposite direction.CTRL-Otakes back to older positions.CTRL-Itakes 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/gfor all substitute between two line, type
:#,#s/old/new/g#, # means two line numbers, maybe need
CTRL-Gto confirm them.for all substitute in file, type
:%S/old/new/gfor 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 !lsThis command means read the output of shell command
lsinto current vim file.When you want to write,
vinto visual mode andmotionselect any text, then:w <filename>Just save you select in visual mode.
Lesson 6
- To **open **(new) a line and start insert mode:
- Type
oto open Below the cursor - Type
Oto open Above the cursor
- Type
- Combine locate and insert motion:
- using
wandi, you can move cursor to the first of word and insert before it. - using
eanda, you can move cursor to the end of word and insert (or append) after it.
- using
- To yank (copy) and puts (paste)
- Type
yandp. - you can compose them with other motion, like
ywcan copy one word.
- Type
- To enter replace mode, type
Rand you can directly cover the previous text by type in new text. - To set option, using
:set <option>icorignorecase, it ignore upper/lower case when searching.isorincsearch, it show partial matches for a search phrases.hlsorhlsearch, 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 oopens a fold at the cursor.z Shift+oopens all folds at the cursor.z ccloses a fold at the cursor.z mincreases thefoldlevelby one.z Shift+mcloses all open folds.z rdecreases thefoldlevelby one.z Shift+rdecreases thefoldlevelto zero – all folds will be open.



