vim常用命令
注意事项:
修改完代码之后,一定要保存,再重新编译。
一、光标移动
1、向后移动
w:
使得光标向前移到一个单词;
b:
向后移动一个单词;
e:
也是向前移到一个单词,但是将光标定位在单词的结尾处;
ge:
则是向后移到一个单词并到达前一个单词的结尾处。
二、跳转
1、跳至文件头尾
gg:文件头
G:文件尾
2、返回之前的位置
CTRL-O takes you back to older positions, CTRL-I to newer positions.
3、翻页
整页翻页 ctrl-f, ctrl-b 注释:forward,backward
zz 让光标所在的行居屏幕中央
zt 让光标所在的行居屏幕最上一行 t=top
zb 让光标所在的行居屏幕最下一行 b=bottom
4、行首、行尾
0:行首
$:行尾
^:第一个非空字符
5、go to line
An easy way to to get to a line (whose number we know) faster is to combine some existing methods:
:123<CR>
or
123G
or
123gg
The solution is to map in normal mode the enter to G.
)
Jump forward one sentence.
(
Jump backward one sentence.
}
Jump forward one paragraph.
{
Jump backward one paragraph.
H
Jump to the top of the screen.
M
Jump to the middle of the screen.
L
Jump to the bottom of the screen
三、复制,粘贴
1、粘贴
yy:复制游标所在行整行。或大写一个Y。
yw:复制一个word。
nyy:复制光标所在行向下n行。
y: copy
d: cut
p: paste after cursor
P: paste before cursor
Cut and paste:
Position the cursor where you want to begin cutting.
Press v (or upper case V if you want to cut whole lines).
Move the cursor to the end of what you want to cut.
Press d to cut or y to copy.
Move to where you would like to paste.
Press P to paste before the cursor, or p to paste after.
http://vim.wikia.com/wiki/Copy,_cut_and_paste
四、查找、替换
1、查找
命令:/ 向下查找
命令:? 向上查找
命令:n 下一个
2、替换
语法为 :[addr]s/源字符串/目的字符串/[option]
全局替换命令为::%s/源字符串/目的字符串/g
[addr] : 表示检索范围,省略时表示当前行。如:“1,20”:表示从第1行到20行;“%”:表示整个文件,同“1,$”;“. ,$”:从当前行到文件尾;
s : 表示替换操作
[option] : 表示操作类型如:g表示全局替换; c表示进行确认;p表示替代结果逐行显示(Ctrl + L恢复屏幕);省略option时仅对每行第一个匹配串进行替换;
如果在源字符串和目的字符串中出现特殊字符,需要用”\”转义
五、撤销、恢复
u: 撤销
ctrl + r: 恢复
六、ctags, taglist
1、Ctags
1)基本用法
生成tag文件的命令:ctags–R (此命令只链接当前目录)
注意:在vim使用命令时使用:!命令。
2)为标准库文件生成tag文件
ctags -R /usr/include (但此命令又没有链接当前目录)
3)为解决上述缺陷,可以在.vimrc文件里进行配置
在.vimrc文件里配置所使用到的tags文件,包括标准c库目录和当前目录的,在.vimrc里添加:
set tags=/usr/include/tags
set tags=tags
2、Taglist:打开taglist的命令:TlistOpen (在vim下执行:TlistOpen)
:tag{ident} "跳转到指定的标签
:tags "显示标签栈
CTRL-] "跳转到当前光标下的标签
CTRL-T "跳到标签栈中较早的标签
七、vim中查看编译信息
首先,你要预先编写好编译文件makefile。如果不想编写makefile,也可以直接在vim中设置参数,例如:setmakeprg=gcc\ -Wall\ -o\ hello\ hello.c 回车。编译命令的空格需要加上反斜杠来转义。
然后,键入:make,回车即开始编译。注意,编译之前要现保存文件!
接着,编译完毕后,回到vim主界面,也就是源代码界面(可能还要按回车回到主界面)
最后,键入:cope回车,vim的窗口就被分成两半,上面的那一半是源代码,下面的那一半(叫做“Quickfix窗口”)是编译结果。如下图所示(这里故意遗漏一个分号,以便在“Quickfix窗口”显示错误信息):
上下切换窗口可以按Ctrl+w Ctrl+w (就是连续按两下 Ctrl+w)。
在Quickfix窗口中,用上下方向键或j、k按键上下选择错误。然后按下回车键即可让光标跳转到相应的源代码错误的那一行。
八、打开标准库文件
1、The following commands open the file name under the cursor:
gf open in the same window ("gotofile")
<c-w>f openin a new window (Ctrl-w f)
2、Place the cursor on anyvariable in your program.
gd will take you to the local declaration.
gD will take you to the global declaration.
g* search for the word under the cursor (like*, but g* on 'rain' will find words like 'rainbow').
g# same as g* but in backward direction.
九、shell: cd切换目录
1. cd - 当前目录和之前所在的目录之间的切换
2. cd +Alt . 用上次命令的最后一个目录路径要用上上次命令的最后一个目录,就Alt+.两次就可以了
十、文件间切换
1.打开多个文件:
1).vim还没有启动的时候:
在终端里输入
vim file1 file2 ... filen便可以打开所有想要打开的文件
2).vim已经启动
输入
:open file
可以再打开一个文件,并且此时vim里会显示出file文件的内容。
2.同时显示多个文件:
:split
:vsplit
3.在文件之间切换:
1).文件间切换
Ctrl+6—下一个文件
:bn—下一个文件
:bp—上一个文件
对于用(v)split在多个窗格中打开的文件,这种方法只会在当前窗格中切换不同的文件。
2).在窗格间切换的方法
Ctrl+w+方向键——切换到前/下/上/后一个窗格
Ctrl+w+h/j/k/l ——同上
Ctrl+ww——依次向后切换到下一个窗格中
删除
x - delete current character
dw - delete current word
dd - delete current line
5dd - delete five lines
d$ - delete to end of line
d0 - delete to beginning of line
:1,.d
delete to beginning of file
:.,$d
delete to end of file
插入
i 在光标之前插入
a 在光标之后追加
A 在一行的结尾处追加
I 在一行的开头处插入
o 在光标所在位置的下一行打开新行插入
O 在光标所在位置的上一行打开新行插入
选择
V - selects entire lines
v - selects range of text
Shifting blocks
In normal mode, type >> to indent the current line, or << to unindent. Each command can be used with a count. The operators > and < do the same for motions, text objects and visual selections. For all commands, pressing . repeats the operation.
For example, typing 5>>.. shifts five lines to the right, and then repeats the operation twice so that the five lines are shifted three times.
In insert mode, Ctrl-T indents the current line, and Ctrl-D unindents.
When indenting or unindenting, lines are shifted one 'shiftwidth' to the right or left
http://vim.wikia.com/wiki/Shifting_blocks_visually
其他
v: 进入v模式
set number:显示行号