Menu Home

vim 使用技巧

多行注释

增加注释

1. ctrl + v 进入可视块选择,使用上下左右键选择需要注释的行
2. shift + i 进入插入模式
3. 输入注释符号 eg. //
4. ESC

取消注释

1. ctrl + v 进入可视块选择,使用上下左右键选择需要取消注释行的注释符
2. d 删除注释符号

折叠代码

vim 提供 6中折叠方式(set fmd=****):
manual 手工定义折叠
indent 更多的缩进表示更高级别的折叠
expr 用表达式来定义折叠
syntax 用语法高亮来定义折叠
diff 对没有更改的文本进行折叠
marker 对文中的标志折叠
vim折叠命令:
zc 折叠
zC 对所在范围内所有嵌套的折叠点进行折叠
zo 展开折叠
zO 对所在范围内所有嵌套的折叠点展开
[z 到当前打开的折叠的开始处。
]z 到当前打开的折叠的末尾处。
zj 向下移动。到达下一个折叠的开始处。关闭的折叠也被计入。
zk 向上移动到前一折叠的结束处。关闭的折叠也被计入。
使用时在大括号中间输入以上命令。
可以使用下面的命令来创建和删除折叠:
zf 创建折叠,比如在marker方式下:
zf56G,创建从当前行起到56行的代码折叠;
10zf或10zf+或zf10↓,创建从当前行起到后10行的代码折叠。
10zf-或zf10↑,创建从当前行起到之前10行的代码折叠。
在括号处zf%,创建从当前行起到对应的匹配的括号上去((),{},[],<>等)。
zd 删除 (delete) 在光标下的折叠。仅当 ‘foldmethod’ 设为 “manual” 或 “marker” 时有效。
zD 循环删除 (Delete) 光标下的折叠,即嵌套删除折叠。
仅当 ‘foldmethod’ 设为 “manual” 或 “marker” 时有效。
zE 除去 (Eliminate) 窗口里“所有”的折叠。
仅当 ‘foldmethod’ 设为 “manual” 或 “marker” 时有效。

在vi中执行编译程序的make命令

cnext : 下一个出错误的地方
cprevious : 回到上一个错误的地方
clast : 最后一个出错的地方
crewind : 第一个出现错误的地方
cnfile :
cc : 显示出错信息
clist : 错误的列表
显示一定范围内的错误:
:clist3,5 显示第三行到第五行的错误
:clsit,5 显示第一行到第五行的错误
:clsist5, 显示第五行到最后一行的错误

vim 调整窗口高度和宽度

CTRL-W = 使得所有窗口 (几乎) 等宽、等高,但当前窗口使用 ‘winheight’ 和 ‘winwidth’。

:res[ize] -N
CTRL-W – 使得当前窗口高度减 N (默认值是 1)。如果在 ‘vertical’ 之后使用,则使得宽度减 N。

:res[ize] +N
CTRL-W + 使得当前窗口高度加 N (默认值是 1)。如果在 ‘vertical’ 之后使用,则使得宽度加 N。

:res[ize] [N]
CTRL-W _ 设置当前窗口的高度为 N (默认值为最大可能高度)。

CTRL-W <; 使得当前窗口宽度减 N (默认值是 1)。
CTRL-W >; 使得当前窗口宽度加 N (默认值是 1)。

整个窗口的移动
CTRL-W-H 将窗口移到最左边
CTRL-W-L 将窗口移到最右边
CTRL-W-J 将窗口移到底端
CTRL-W-K 将窗口移到顶端

插入文本类命令

i :在光标前
I :在当前行首
a:光标后
A:在当前行尾
o:在当前行之下新开一行
O:在当前行之上新开一行
r:替换当前字符
R:替换当前字符及其后的字符,直至按ESC键
s:从当前光标位置处开始,以输入的文本替代指定数目的字符
S:删除指定数目的行,并以所输入文本代替之
ncw或nCW:修改指定数目的字
nCC:修改指定数目的行

删除命令

ndw或ndW:删除光标处开始及其后的n-1个字
do:删至行首
d$:删至行尾
ndd:删除当前行及其后n-1行
x或X:删除一个字符,x删除光标后的,而X删除光标前的
Ctrl+u:删除输入方式下所输入的文本

搜索及替换命令

/pattern:从光标开始处向文件尾搜索pattern
?pattern:从光标开始处向文件首搜索pattern
n:在同一方向重复上一次搜索命令
N:在反方向上重复上一次搜索命令
:s/p1/p2/g:将当前行中所有p1均用p2替代
:n1,n2s/p1/p2/g:将第n1至n2行中所有p1均用p2替代
:g/p1/s//p2/g:将文件中所有p1均用p2替换

选项设置

all:列出所有选项设置情况
term:设置终端类型
ignorance:在搜索中忽略大小写
list:显示制表位(Ctrl+I)和行尾标志($)
number:显示行号
report:显示由面向行的命令修改过的数目
terse:显示简短的警告信息
warn:在转到别的文件时若没保存当前文件则显示NO write信息
nomagic:允许在搜索模式中,使用前面不带“\”的特殊字符
nowrapscan:禁止vi在搜索到达文件两端时,又从另一端开始
mesg:允许vi显示其他用户用write写到自己终端上的信息

最后行方式命令

:n1,n2 co n3:将n1行到n2行之间的内容拷贝到第n3行下
:n1,n2 m n3:将n1行到n2行之间的内容移至到第n3行下
:n1,n2 d :将n1行到n2行之间的内容删除
:w :保存当前文件
:e filename:打开文件filename进行编辑
:x:保存当前文件并退出
:q:退出vi
:q!:不保存文件并退出vi
:!command:执行shell命令command
:n1,n2 w!command:将文件中n1行至n2行的内容作为command的输入并执行之,若不指定n1,n2,则表示将整个文件内容作为command的输入
:r!command:将命令command的输出结果放到当前行
:pwd : 查看当前编辑文件的路径

寄存器操作

“?nyy:将当前行及其下n行的内容保存到寄存器?中,其中?为一个字母,n为一个数字
“?nyw:将当前行及其下n个字保存到寄存器?中,其中?为一个字母,n为一个数字
“?nyl:将当前行及其下n个字符保存到寄存器?中,其中?为一个字母,n为一个数字
“?p:取出寄存器?中的内容并将其放到光标位置处。这里?可以是一个字母,也可以是一个数字
ndd:将当前行及其下共n行文本删除,并将所删内容放到1号删除寄存器中。

格式化代码

格式化代码: gg=G

查看打开文件的绝对路径

先按下数字 1 , 然后按下组合键 CTRL + shift + g

Vim正则

使用最小匹配

Vim默认是贪婪匹配,使用:h non-greedy查看帮助文档会发现可以使用下面的方法来使用最小匹配:

使用“\{-}”来代替“*”

12 replies

  1. My spouse and i ended up being absolutely peaceful that Peter could finish off his studies because of the ideas he received out of the web page. It is now and again perplexing to simply be handing out tactics which people might have been trying to sell. And we also know we have the writer to give thanks to for this. All of the explanations you made, the straightforward blog menu, the friendships your site assist to instill – it’s most incredible, and it’s really aiding our son in addition to the family recognize that this concept is excellent, and that is extraordinarily essential. Thank you for the whole lot!

  2. Thank you for your whole work on this blog. Kim take interest in getting into investigation and it’s simple to grasp why. We hear all of the dynamic mode you offer very helpful secrets by means of the website and encourage participation from others on that subject while our girl is in fact discovering a lot of things. Take pleasure in the remaining portion of the year. You’re performing a pretty cool job.

  3. I actually wanted to construct a brief word in order to say thanks to you for some of the fantastic tips and hints you are giving out here. My time intensive internet investigation has finally been paid with wonderful facts and strategies to talk about with my two friends. I ‘d tell you that we website visitors are undoubtedly endowed to be in a really good site with so many wonderful professionals with very helpful opinions. I feel extremely happy to have encountered your website and look forward to really more entertaining times reading here. Thanks once again for everything.

  4. I would like to convey my gratitude for your generosity in support of persons who should have help with that concept. Your special dedication to getting the solution around ended up being really invaluable and have constantly helped professionals just like me to get to their endeavors. Your new valuable guide means a great deal a person like me and somewhat more to my fellow workers. Regards; from all of us.

  5. I wanted to write down a simple note so as to say thanks to you for the awesome solutions you are placing at this website. My incredibly long internet lookup has finally been paid with brilliant concept to exchange with my best friends. I would claim that most of us readers are truly fortunate to live in a magnificent site with so many marvellous individuals with helpful tricks. I feel very much fortunate to have seen the website and look forward to so many more entertaining moments reading here. Thank you once again for all the details.

  6. I simply wanted to make a simple comment to be able to appreciate you for all of the fantastic solutions you are giving out at this site. My prolonged internet investigation has at the end been rewarded with excellent knowledge to write about with my close friends. I ‘d claim that most of us website visitors are undoubtedly lucky to live in a useful site with very many brilliant people with beneficial opinions. I feel quite privileged to have discovered your webpage and look forward to tons of more entertaining minutes reading here. Thank you once again for all the details.

  7. I must show my thanks to the writer for rescuing me from such a predicament. Just after researching through the search engines and getting suggestions which are not powerful, I believed my life was well over. Existing minus the answers to the difficulties you’ve resolved through your main review is a crucial case, as well as those that would have negatively damaged my career if I hadn’t discovered your web site. Your own personal ability and kindness in maneuvering all the stuff was crucial. I’m not sure what I would have done if I hadn’t encountered such a step like this. I can at this time look ahead to my future. Thanks a lot so much for your impressive and effective help. I won’t be reluctant to recommend the website to anybody who will need direction on this issue.

  8. I as well as my pals were actually taking note of the great advice from your web blog and so suddenly came up with a terrible feeling I had not thanked the web blog owner for them. All the young men ended up as a consequence warmed to read them and now have really been taking pleasure in them. Thank you for truly being indeed kind and then for selecting varieties of fabulous issues millions of individuals are really needing to discover. Our own honest regret for not expressing appreciation to you sooner.

  9. I definitely wanted to construct a note to say thanks to you for some of the fantastic points you are showing on this website. My prolonged internet look up has at the end of the day been honored with pleasant concept to write about with my visitors. I would suppose that many of us site visitors actually are very much blessed to live in a decent website with so many brilliant people with valuable hints. I feel very much blessed to have come across your entire webpage and look forward to many more enjoyable times reading here. Thank you once again for a lot of things.

  10. I simply desired to say thanks all over again. I am not sure what I would have implemented without the actual tricks shown by you regarding such theme. This has been a very daunting dilemma for me personally, nevertheless being able to see this expert approach you treated that made me to weep with contentment. Now i am happy for your information and pray you are aware of a powerful job you are always providing educating people through your webpage. I know that you’ve never encountered all of us.

  11. Thanks for all your valuable effort on this site. Debby really likes working on research and it’s simple to grasp why. We all hear all of the compelling form you give both interesting and useful ideas by means of the web site and therefore invigorate participation from other ones about this area of interest plus our princess is starting to learn a lot. Have fun with the remaining portion of the new year. You are always performing a superb job.

  12. Thanks a lot for providing individuals with remarkably splendid chance to read critical reviews from this blog. It’s usually so sweet plus stuffed with amusement for me personally and my office friends to visit your website nearly 3 times every week to study the latest secrets you have. And indeed, I’m also certainly impressed with the very good creative concepts you serve. Certain 3 points in this post are in fact the simplest we have had.

Leave a Reply

Your email address will not be published.