Linux下vi/vim的操作,当你不熟悉时,不会愿意用他,一旦你习惯了,他的高效就无人能敌,用久了就会有种,相逢恨晚的感觉,这也是一部分人,习惯使用Mac而不是Windows的原因。
vi操作,可以说基本上,围绕着键盘的每一个键位,都有他的意义。
首先说一些我常用的,
(1) 打开vi如果未输入i,则复制-粘贴字符,会导致前面少几个字符。
例如,
vi test.txt
this is a test file.
此时复制”this is a test file”这几个字符,在文件开始处,按”shift“和”insert”,文件test.txt显示,可以看出,this截了,
s is a test file.this is a test file.
如果输入i,再次执行”shift“和”insert”,这次就正常了,就是说如果需要复制-粘贴,保险起见,就要输入i,再执行粘贴操作,
this is a test file.this is a test file.
(2) vi编辑完成,两种退出模式。
保存退出,可以输入ZZ。
不保存退出,可以输入ZQ。
为了提高效率,可以右手shift,左手Z/Q,一气呵成。
(3) 批量编辑
如果需要为文件每行,头部加字符串“sqlplus“,可以执行
:%s/^/sqlplus
如果要将下列文字中,第一个a字符变为b,
this is a test file.this is a test file.
可以执行,
:%s@a@b
如果要将所有a,变为b,可以执行,
:%s@a@b@g
如果需要统计,a字符出现的次数,可以执行,
:%s/a//gn
提示,
this is a test file.this is a test file.
2 matches on 1 line
同样支持字符串,统计test出现的次数,可以执行,
:%s/test//gn
提示,
this is a test file.this is a test file.
2 matches on 1 line
(4) 查找定位字符
一行文字,若要找出“a”字符的位置,
this is a test file.this is a test file.
可以执行,
fa
其中f表示find,a表示要检索的字符信息,同理要找b,就可以用fb,
fb
除了字符,像“(”这种也支持,
f(
当然,vi的快捷操作方法,上面只是些皮毛,网上有一些vi操作图表,也许有人看过,下面汇总了一下,引自卢大侠(卢钧轶,cenalulu)的博客(http://cenalulu.github.io/linux/all-vim-cheatsheat/),重要的不是记住所有键位,而是根据自己的实际需求,找出自己需要的,熟练掌握,变为自己的技能,这才是最重要的能力。
经典版本(http://cenalulu.github.io/images/linux/vim_cheatsheet/classic.gif):

入门版本(http://cenalulu.github.io/images/linux/vim_cheatsheet/entry.png):

进阶版本(http://cenalulu.github.io/images/linux/vim_cheatsheet/advanced.png):

增强版本(http://cenalulu.github.io/images/linux/vim_cheatsheet/morden.png):

文字版本(http://cenalulu.github.io/images/linux/vim_cheatsheet/text.png):

(http://cenalulu.github.io/images/linux/vim_cheatsheet/text2.png)

如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)

