学习地址:
vimtutor
vimtutor是一个由vim官方提供的教程,在bash界面直接运行vimtutor即可查看。
Lesson 1
移动光标:
1 2 3 4 5^ k < h l > j vHINT:
- h 的键位于左边,每次按下就会向左移动。
- l 的键位于右边,每次按下就会向右移动。
- j 键看起来很象一支尖端方向朝下的箭头。
vim的退出:- 输入
:q!<Enter>会退出编辑器并且丢弃进入编辑器之后的所有改动。 - 按下
:wq<Enter>保存并退出vim
- 输入
删除光标所在位置的字符:可以按下
x键来删除光标所在位置的字符。插入文本:可以按下
i键来插入字符。添加文本:可以按下
A键来添加文本(光标会定位到行末)
Lesson 2
删除类命令:
- 删除单词:按下
dw可以从光标处删除到一个单词的末尾 - 删除到行末:输入
d$从当前光标删除到行末。
- 删除单词:按下
命令和对象:许多改变文本的命令都由一个操作符和一个动作构成。
比如:
- 以上的删除操作符
d的命令格式如下:d motion。
其中:
d:删除操作符。motion:删除符的操作对象
motion的简短动作列表:w- 从当前光标当前位置直到下一个单词起始处,不包括它的第一个字符。e- 从当前光标当前位置直到单词末尾,包括最后一个字符。(与w区别为不会删除空格)$- 从当前光标当前位置直到当前行末。
- 以上的删除操作符
使用计数指定动作:在动作前输入数字
n,会使该动作重复n次。示例:
- 输入
2w使光标向前移动两个单词。 - 输入
3e使光标向前移动到第三个单词的末尾。 - 输入
0使得光标移动到行首。
- 输入
使用操作符时,在对象前输入数字
n可以重复n次。格式:
operator [number] motion比如:
d2w可以删除光标所在位置的下两个单词删除。删除整行:连续输入两次 d ,即
dd可以删除光标所在位置的整行。撤销类命令:
- 撤销最后执行的命令:输入
u来撤销最后执行的命令。 - 撤销对整行的修改:输入
U来撤销对整行的修改。 - 重做撤销的命令:
CTRL+R
- 撤销最后执行的命令:输入
Lesson 3
Put command: Type
pto put previously deleted text after the cursor.Replace command: Type
rxto replace the character at the cursor with x.The change operator: To change until the end of a word, type
ce.This operator format:
c [motion] number, where the motions are the same.
Lesson 4
Cursor location and file status:
Type
CTRL-Gto show your location in the file and the file status.NOTICE: You may see the cursor position in the lower right corner of the screen. This happens when the ‘ruler’ option is set (see :help ‘ruler’ )
Type
Gto move to a bottom of the file.Type
ggto move you to the start of the file.Type the number of a line, and then
G. This will goto a specific line.
The search command:
- Type
/followed by a phrase to search for the phrase. - To search for a phrase in the backward direction, use
?instead of/. - To search for the same phrase again, simply type
n. - findTo search for the same phrase in the opposite direction, type
N. - To go back to where you came from press
CTRL-O,CTRL-Igoes forward.
- Type
Matching parentheses search: Type
%to find a matching),], or}.The substitute command:
- Type
:s/old/newto substitute ’new’ for the first ‘old’ in a line - Type
:s/old/new/gto substitute new for all ‘old’s on a line type - Type
:#,#s/old/new/gwhere#,#are the line numbers of the range of lines where the substitution is to be done. - Type
:%s/old/new/gto change every occurrence in the whole file. - Type
:%s/old/new/gcto find every occurrence in the whole file, with a prompt whether to substitute or not.
- Type
Lesson 5
Execute an external command: Type
:!followed by an external command to execute that command.- NOTICE: All
:commands must be finished by hitting<ENTER>.
- NOTICE: All
More on writing files: To save the changes made to the text, type
:w FILENAME.Selecting text to write: To save part of the file, type
vmotion:w FILENAMERetrieving and merging files: To insert the contents of a file, type
:r FILENAMEPostScript: You can also read the output of an external command.
For example,
:r !lsreads the output of the ls command and puts it below the cursor.
Lesson 6
The open command:
- Type
oto open a line below the cursor and place you in Insert mode. - To open up a line ABOVE the cursor, simply type a capital
O
- Type
The append command: Type
ato insert text AFTER the cursor.- NOTICE:
a,iandAall go to the same Insert mode, the only difference is where the characters are inserted.
- NOTICE:
Another way to replace: Type a capital
Rto replace more than one character.Copy and paste text:
- Use
yoperator to copy text. - Use
poperator to paste it. - PS: Move the cursor to the end of next line
j$
- Use
Set option: Set an option so a search or substitute ignores case
set the
ic(ignore case) option by entering::set icset the ‘hlsearch’ (highlight search) and ‘incsearch’ (show partial matches) options:
:set hls isto disable ignoring case by enter:
:set noicto remove the highlighting of matches enter:
:nohlsearchIf you want to ignore case for just one search command, use
\cin the phrase:/ignore\c <ENTER>
Lesson 7
Getting help: type one of these three command:
- press the
<HELP>key (if you have one) - press the
<F1>key (if you have one) - type
:help <ENTER>
You can find help on just about any subject, by giving an argument to the
:help command:help w:help c_CTRL-D:help insert-index:help user-manual
- press the
Create a startup script: editing the following file in Unix
:e ~/.vimrc- for more information, type
:help vimrc-intro
- for more information, type
Completion: command line completion with
CTRL-Dand<TAB>- NOTICE: make sure vim is not in compatible mode:
:set nocp
- NOTICE: make sure vim is not in compatible mode: