编程小贴士

给你的编程提供小点子


一些不起眼但非常有用的 Vim 命令

保存文件并退出

说起来有些惭愧,我也是最近才学到这个命令

:x

和下面的命令是等价的:

:wq

都是保存当前文件并退出。

(译者注:这两个命令实际上并不完全等价,当文件被修改时两个命令时相同的。但如果未被修改,使用 :x 不会更改文件的修改时间,而使用 :wq 会改变文件的修改时间。)

基本计算器

在插入模式下,你可以使用 Ctrl+r 键然后输入 =,再输入一个简单的算式。按 Enter 键,计算结果就会插入到文件中。例如,尝试输入:

1
Ctrl+r '=2+2' ENTER

tips1000_2015-03-06_05-42-121

然后计算结果“4 ”会被插入到文件中。

查找重复的连续的单词

当你很快地打字时,很有可能会连续输入同一个单词两次,就像 this this。这种错误可能骗过任何一个人,即使是你自己重新阅读一遍也不可避免。幸运的是,有一个简单的正则表达式可以用来预防这个错误。使用搜索命令(默认是 /)然后输入:

tips1000_2015-03-06_05-42-11

这会显示所有重复的单词。要达到最好的效果,不要忘记把下面的命令:

1
set hlsearch

放到你的 .vimrc 文件中高亮所有的匹配。

tips1000_2015-03-06_05-42-122

缩写

一个很可能是最令人印象深刻的窍门是你可以在 Vim 中定义缩写,它可以实时地把你输入的东西替换为另外的东西。语法格式如下:

1
:ab [缩写] [要替换的文字]

一个通用的例子是:

1
:ab asap as soon as possible

会把你输入的 “asap” 替换为 “as soon as possible”。

在你忘记用 root 方式打开文件时的文件保存

这可能是一个在论坛中一直受欢迎的命令。每当你打开一个你没有写入权限的文件(比如系统配置文件)并做了一些修改,Vim 无法通过普通的 “:w” 命令来保存。

你不需要重新以 root 方式打开文件再进行修改,只需要运行:

1
:w !sudo tee %

这会直接以 root 方式保存。

实时加密文本

如果你不想让别人看懂你的屏幕上的内容,你可以使用一个内置的选项,通过下面的命令使用 ROT13 来对文本进行编码:

1
ggVGg?

tips1000_2015-03-06_05-42-123

gg 把光标移动到 Vim 缓冲区的第一行,V 进入可视模式,G 把光标移动到缓冲区的最后一行。因此,ggVG 使可视模式覆盖这个当前缓冲区。最后 g? 使用 ROT13 对整个区域进行编码。

注意它可以被映射到一个最常使用的键。它对字母符号也可以很好地工作。要对它进行撤销,最好的方法就是使用撤销命令:u。

自动补全

这是另外一个令我感到惭愧的功能,但我发现周围很多人并不知道。Vim 默认有自动补全的功能。的确这个功能是很基本的,并且可以通过插件来增强,但它也很有帮助。方法很简单。Vim 尝试通过已经输入的单词来预测单词的结尾。比如当你在同一个文件中第二次输入 “compiler” 时,仅仅输入 “com” 然后保持在插入模式,按 Ctrl+n 键就可以看到 Vim 为你补全了单词。很简单,但也很有用。

比较两个文件的不同

你们中的大多数很可能都知道 vimdiff 命令,它可以使用分离模式打开 Vim 并比较两个文件的不同。语法如下:

1
$ vimdiff [文件1] [文件2]

但同样的结果也可以通过下面的 Vim 命令来获得:

1
:diffthis

首先在 Vim 中打开原始文件。然后使用分离模式带来第二个文件:

1
:vsp [文件2]

最后在第一个缓冲区里输入:

1
:diffthis

通过 Ctrl+w 来切换缓冲区并再次输入:

1
:diffthis

这样两个文件中不同的部分就会被高亮。

(译者注:可以直接在一个缓冲区里使用命令 :windo diffthis,而不用输入 :diffthis 两次)

要停止比较,使用:

1
:diffoff

按时间回退文件

Vim 会记录文件的更改,你很容易可以回退到之前某个时间。该命令是相当直观的。比如:

1
:earlier 1m

会把文件回退到 1 分钟以前的状态。

注意,你可以使用下面的命令进行相反的转换:

1
:later

删除标记内部的文字

当我开始使用 Vim 时,一件我总是想很方便做的事情是如何轻松的删除方括号或圆括号里的内容。转到开始的标记,然后使用下面的语法:

1
di[标记]

比如,把光标放在开始的圆括号上,使用下面的命令来删除圆括号内的文字:

1
di(

如果是方括号或者是引号,则使用:

1
di{

和:

1
di"

删除指定标记前的内容

和删除标记内部有些相似,但目的不同。命令如下:

1
dt[标记]

会删除所有光标和标记之间的内容(保持标记不动),如果在同一行有这个标记的话。例如

1
dt.

会删除至句子的末尾,但保持 ‘.’ 不动。

把 Vim 变为十六进制编辑器

这不是我最喜欢的窍门,但有时会很有趣。你可以把 Vim 和 xxd 功能连起来来把文件转换为十六进制模式。命令如下:

1
:%!xxd

tips1000_2015-03-06_05-42-12

类似的,你可以通过下面的命令恢复原来的状态:

1
:%!xxd -r

把光标下的文字置于屏幕中央

我们所要做的事情如标题所示。如果你想强制滚动屏幕来把光标下的文字置于屏幕的中央,在可视模式中使用命令(译者注:在普通模式中也可以):

zz

跳到上一个/下一个位置

当你编辑一个很大的文件时,经常要做的事是在某处进行修改,然后跳到另外一处。如果你想跳回之前修改的地方,使用命令:

1
Ctrl+o

来回到之前修改的地方

类似的:

1
Ctrl+i

会回退上面的跳动。

把当前文件转化为网页

这会生成一个 HTML 文件来显示文本,并在分开的窗口显示源代码:

1
:%TOhtml

(译者注:原文是 :%Tohtml,但在我的电脑上是 :%TOhtml)

tips1000_2015-03-06_05-42-124

很基本但很不错。

总的来说,这一系列命令是在我读了许多论坛主题和 Vim Tips wiki(如果你想学习更多关于编辑器的知识,我非常推荐这篇文章) 之后收集起来的。

如果你还知道哪些非常有用但你认为大多数人并不知道的命令,可以随意在评论中分享出来。就像引言中所说的,一个“鲜为人知但很有用的”命令也许只是你自己的看法,但分享出来总是好的

177 Responses to “ 一些不起眼但非常有用的 Vim 命令 ”

  1. chocopie说道:

    XdrYr2 Way cool! Some very valid points! I appreciate you penning this write-up and the rest of the site is also really good.

  2. I think other web-site proprietors should take this site as an model, very clean and great user genial style and design, let alone the content. You are an expert in this topic!

  3. Im obliged for the blog post.Really looking forward to read more. Fantastic.

  4. painting说道:

    logbook loan What is the best site to start a blog on?

  5. Smokers Lungs说道:

    It as onerous to search out educated people on this matter, but you sound like you recognize what you are talking about! Thanks

  6. Very nice info and straight to the point. I don at know if this is actually the best place to ask but do you folks have any thoughts on where to hire some professional writers? Thank you

  7. This page definitely has all of the info I needed about this subject and didn at know who to ask.

  8. dau sa be tho说道:

    You have mentioned very interesting details! ps decent site.

  9. Click to read说道:

    Muchos Gracias for your post.Really looking forward to read more. Really Great.

  10. go说道:

    I value the article.Really thank you! Fantastic.

  11. my site说道:

    Spot on with this write-up, I absolutely believe that this web site needs far more attention. I all probably be returning to see more, thanks for the info!

  12. we came across a cool site that you simply could possibly love. Take a search for those who want

  13. Wow, marvelous blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your site is great, as well as the content!. Thanks For Your article about sex.

  14. click说道:

    If you are not willing to risk the usual you will have to settle for the ordinary.

  15. You ave made some really good points there. I looked on the internet for more information about the issue and found most people will go along with your views on this web site.

  16. I visited a lot of website but I think this one contains something special in it in it

  17. Really enjoyed this article post.Really thank you! Really Great.

  18. Looking forward to reading more. Great post.Much thanks again. Great.

  19. Trap an hoi说道:

    I value the post.Much thanks again. Want more.

  20. cheap taxi说道:

    I truly appreciate this post. I?аАТ’аЂа†ve been looking everywhere for this! Thank goodness I found it on Bing. You ave made my day! Thx again

  21. Very good blog post.Really looking forward to read more.

  22. Real good information can be found on blog.

  23. microblog说道:

    You can not believe simply how a lot time I had spent for this information!

  24. This excellent website definitely has all of the information I needed concerning this subject and didn at know who to ask.

  25. Some truly prime articles on this internet site , saved to fav.

  26. Souls in the Waves Great Morning, I just stopped in to go to your web site and thought I ad say I liked myself.

  27. Asuquo说道:

    You, my pal, ROCK! I found exactly the information I already searched all over the place and just could not locate it. What a perfect web-site.

  28. Thanks again for the blog post.Much thanks again. Much obliged.

  29. Thank you for this impressive report. I am refreshed following reading this. Thank you!

  30. Nwokolo说道:

    Thanks again for the blog.Much thanks again. Want more.

  31. trang tri noel说道:

    Thanks again for the blog post.Really thank you! Want more.

  32. Very nice post and right to the point. I am not sure if this is actually the best place to ask but do you people have any thoughts on where to employ some professional writers? Thx

  33. It as not that I want to copy your web site, but I really like the layout. Could you let me know which design are you using? Or was it especially designed?

  34. Looking for me, I came here for important information. The information is so incredible that I have to check it out. Nevertheless, thanks.

  35. mp4 download说道:

    I will also like to express that most individuals that find themselves without having health insurance can be students, self-employed and those that are not working.

  36. Chong tham说道:

    Wow, amazing blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your website is fantastic, let alone the content!

  37. Wow, great blog post.Thanks Again. Will read on

  38. It as a very easy on the eyes which makes it much more enjoyable for me

  39. click here说道:

    their motive, and that is also happening with this piece of

  40. click here说道:

    When i open your Supply it appears to be a ton of rubbish, could be the matter in my portion?

  41. sales tool说道:

    you are stating and the best way in which you say it.

  42. You got a very wonderful website, Gladiola I found it through yahoo.

  43. Your positions continually have got a lot of really up to date info. Where do you come up with this? Just saying you are very resourceful. Thanks again

  44. Spot on with this write-up, I actually feel this web site needs a great deal more attention. I all probably be back again to read more, thanks for the information!

  45. online loans说道:

    You made some clear points there. I did a search on the subject and found most persons will agree with your website.

  46. sua hat huu co说道:

    very few web sites that occur to be detailed beneath, from our point of view are undoubtedly very well worth checking out

  47. sim mobifone说道:

    pretty valuable material, overall I consider this is worth a bookmark, thanks

  48. sim so dep说道:

    thank you for this post, I am a big fan of this site would like to go along updated.

  49. Online说道:

    or fashionable and useful, you will easily find your Id Nike Blazers sandals at a discount price to fit your budget.

  50. Elena Matei说道:

    I truly appreciate this article.Really thank you! Cool.

  51. David说道:

    Thanks for sharing, this is a fantastic article.Really looking forward to read more. Much obliged.

  52. girls说道:

    I was recommended this web site by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my problem. You are incredible! Thanks!

  53. Muchos Gracias for your blog.Much thanks again. Want more.

  54. Todays 365说道:

    You ought to be a part of a contest for one of the best websites on the net. I am going to recommend this web site!

  55. Successful tips说道:

    wow, awesome blog post.Much thanks again. Will read on

  56. Hosting说道:

    This blog is definitely interesting and also diverting. I have picked a bunch of useful tips out of this blog. I ad love to go back again and again. Thanks!

  57. aliexpress说道:

    I truly appreciate this post. I ave been seeking everywhere for this! Thank goodness I found it on Google. You have created my day! Thx once again..

  58. diets说道:

    Wow, amazing blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is fantastic, let alone the content!. Thanks For Your article about sex.

  59. pretty beneficial stuff, overall I believe this is well worth a bookmark, thanks

  60. Crochet basico说道:

    You have brought up a very excellent details, appreciate it for the post.

  61. Crochet facil说道:

    I?аАТ’аЂа†ll right away grasp your rss as I can at find your email subscription link or e-newsletter service. Do you have any? Kindly let me know so that I may subscribe. Thanks.

  62. value说道:

    Wow! This can be one particular of the most useful blogs We have ever arrive across on this subject. Actually Wonderful. I am also a specialist in this topic so I can understand your effort.

  63. You have noted very interesting details! ps nice internet site.

  64. Thank you for your blog article.Really thank you! Fantastic.

  65. Waec说道:

    Way cool! Some extremely valid points! I appreciate you penning this article and the rest of the website is extremely good.

  66. Exams说道:

    Thanks for the article.Much thanks again. Keep writing.

  67. I’а†ve read numerous excellent stuff here. Unquestionably worth bookmarking for revisiting. I surprise how lots attempt you set to create this sort of good informative website.

  68. I truly appreciate this blog post. Cool.

  69. types of love说道:

    What kind of digicam did you use? That is certainly a decent premium quality.

  70. I value the article.Much thanks again. Want more.

  71. Moskito说道:

    Many thanks for sharing this great write-up. Very inspiring! (as always, btw)

  72. Open heavens说道:

    time just for this fantastic read!! I definitely liked every little bit of

  73. The new Zune browser is surprisingly good, but not as good as the iPod as. It works well, but isn at as fast as Safari, and has a clunkier interface.

  74. Very informative blog article.Much thanks again. Awesome.

  75. publish upper! Come on over and consult with my website.

  76. Looking forward to reading more. Great article.Really thank you! Fantastic.

  77. Thanks for sharing this great write-up. Very interesting ideas! (as always, btw)

  78. ventas说道:

    Looking forward to reading more. Great blog post.Thanks Again. Will read on

  79. tips说道:

    Informative article, just what I needed.

  80. Judi Tembak Ikan说道:

    It as wonderful that you are getting thoughts from this paragraph as well as from our discussion made here.

  81. I see something truly special in this site.

  82. Thanks for sharing, this is a fantastic post.Really thank you! Fantastic.

  83. girl games说道:

    Thanks so much for the blog article.Thanks Again. Cool.

  84. Thanks for writing such a good article, I stumbled onto your website and read a few articles. I like your way of writing

  85. now punta cana说道:

    Thank you for sharing this excellent write-up. Very interesting ideas! (as always, btw)

  86. the same niche. Your blog provided us valuable information to

  87. Thank you ever so for you blog post.Really looking forward to read more. Awesome.

  88. Really appreciate you sharing this blog post.Much thanks again. Really Great.

  89. payday loan online no fax quick and easy payday loan

  90. Wow, fantastic blog layout! How long have you been blogging for? you made running a blog glance easy. The total glance of your website is excellent, let alone the content material!

  91. Thanks for all the answers:) In fact, learned a lot of new information. Dut I just didn`t figure out what is what till the end!.

  92. devotionals说道:

    It as rather a great along with handy part of details. I will be satisfied that you simply contributed this convenient info about. Remember to keep us informed this way. Appreciate your spreading.

  93. know more说道:

    This site has got some extremely helpful stuff on it! Cheers for helping me!

  94. this website说道:

    I simply could not go away your web site prior to suggesting that I extremely loved the standard information an individual provide for your guests? Is gonna be again regularly to check out new posts.

  95. Elmer说道:

    This unique blog is really entertaining additionally diverting. I have picked a lot of interesting things out of this source. I ad love to go back again and again. Thanks a bunch!

  96. So说道:

    There is apparently a bunch to know about this. I think you made certain nice points in features also.

  97. I truly appreciate this post.Really thank you! Keep writing.

  98. Muchos Gracias for your article.Really looking forward to read more. Really Cool.

  99. I truly appreciate this post.Thanks Again. Will read on

  100. Thanks for sharing, this is a fantastic post.Really thank you! Much obliged.

  101. see more说道:

    Thanks so much for the blog post.Much thanks again. Awesome.

  102. this website说道:

    Very informative blog.Really looking forward to read more. Great.

  103. Really informative blog.Really thank you! Fantastic.

  104. Thanks so much for the blog post. Fantastic.

  105. Thanks so much for the article post.Really looking forward to read more. Much obliged.

  106. I really liked your article post.Really thank you! Really Great.

  107. Share posts说道:

    Im thankful for the article post.Thanks Again. Really Great.

  108. click here说道:

    wow, awesome post.Much thanks again. Really Cool.

  109. I think other web site proprietors should take this site as an model, very clean and magnificent user friendly style and design, let alone the content. You are an expert in this topic!

  110. Thanks-a-mundo for the blog article.Much thanks again. Keep writing.

  111. to read more说道:

    Thanks for sharing this great piece. Very inspiring! (as always, btw)

  112. Super-Duper website! I am loving it!! Will be back later to read some more. I am taking your feeds also.

  113. There is clearly a lot to identify about this. I assume you made some nice points in features also.

  114. Very good article post.Really thank you! Want more.

  115. There as certainly a lot to learn about this topic. I really like all the points you made.

  116. click说道:

    Well I really enjoyed studying it. This subject provided by you is very helpful for proper planning.

  117. Perfectly indited subject matter, thanks for information.

  118. Muchos Gracias for your blog article. Really Great.

  119. It’а†s really a great and useful piece of info. I’а†m glad that you just shared this helpful info with us. Please keep us informed like this. Thank you for sharing.

  120. check here说道:

    Wow, awesome blog layout! How long have you been blogging for? you make blogging look easy. The overall look of your site is great, let alone the content!

  121. more info说道:

    you may have an ideal blog here! would you prefer to make some invite posts on my blog?

  122. Say, you got a nice post.Really thank you!

  123. orlando seo说道:

    Thanks so much for the blog post.Thanks Again. Much obliged.

  124. You made some decent points there. I checked on the internet to find out more about the issue and found most people will go along with your views on this web site.

  125. genuine parts说道:

    Really appreciate you sharing this blog.Really thank you! Much obliged.

  126. click here说道:

    just beneath, are quite a few totally not connected web-sites to ours, even so, they may be surely really worth going over

  127. sunday说道:

    Your style is unique compared to other people I ave read stuff from. Many thanks for posting when you ave got the opportunity, Guess I will just book mark this page.

  128. Really enjoyed this article post.Thanks Again. Really Great.

  129. Thanks so much for the blog article.Really looking forward to read more. Really Cool.

  130. Very interesting info !Perfect just what I was searching for! Justice delayed is justice denied. by William Gladstone.

  131. uniform store说道:

    This is one awesome blog article. Awesome.

  132. I think this is a real great post.Thanks Again. Cool.

  133. You made some really good points there. I checked on the internet for additional information about the issue and found most individuals will go along with your views on this site.|

  134. Thank you ever so for you blog.Much thanks again. Fantastic. click here

  135. Your style is very unique in comparison to other folks I ave read stuff from. Many thanks for posting when you ave got the opportunity, Guess I all just book mark this site.

  136. Very neat blog post.Really thank you! Really Great.

  137. Wow, great blog article.Thanks Again. Want more.

  138. Game Online说道:

    Wow, great blog.Really looking forward to read more. Will read on

  139. Im obliged for the blog post.Really thank you! Really Cool.

  140. Say, you got a nice blog post.Thanks Again. Fantastic.

  141. Im obliged for the article post.Thanks Again. Fantastic.

  142. Bagdad Barcelona说道:

    You have brought up a very fantastic points, appreciate it for the post.

  143. check out说道:

    Major thankies for the blog post. Much obliged.

  144. So pleased to have located this submit.. My personal internet searching seem complete.. thank you. Wonderful feelings you have here.. I value you sharing your point of view..

  145. visit website说道:

    This blog was how do I say it? Relevant!! Finally I have found something which helped me. Thanks a lot!

  146. hand craft说道:

    Sex video arabe gratuit meilleure film porno

  147. I think this is a real great post.Much thanks again. Fantastic.

  148. pretty practical material, overall I think this is really worth a bookmark, thanks

  149. Zebra说道:

    Utterly pent articles, appreciate it for information. He who establishes his argument by noise and command shows that his reason is weak. by Michel de Montaigne.

  150. visit website说道:

    Very good information. Lucky me I found your website by accident (stumbleupon). I ave book-marked it for later!

  151. Very informative article post.Really thank you! Fantastic.

  152. Right now it seems like BlogEngine is the preferred blogging platform out there right now. (from what I ave read) Is that what you are using on your blog?

  153. Find out more说道:

    Thank you for sharing this great piece. Very interesting ideas! (as always, btw)

  154. may bo dam说道:

    Thanks for sharing, this is a fantastic article.Really looking forward to read more. Awesome.

  155. Mobile Computer说道:

    Thanks-a-mundo for the article.Thanks Again. Want more.

  156. This is a topic that as near to my heart Cheers! Where are your contact details though?

  157. my sources说道:

    pretty valuable material, overall I feel this is really worth a bookmark, thanks

  158. Very interesting details you have noted , thanks for posting.

  159. khoa dien tu说道:

    Loving the info on this web site, you have done outstanding job on the articles.

  160. Loving the weblog.. thanks! So pleased to possess located this submit.. Truly appreciate the posting you made available.. Take pleasure in the admission you delivered..

  161. This is one awesome article post.Thanks Again. Really Cool.

  162. You made some clear points there. I did a search on the topic and found most people will approve with your site.

  163. mealcoupons说道:

    right here, certainly like what you are stating and the way wherein you assert it.

  164. chocopie说道:

    nxG7nI your site. You have some really great posts and I feel I would be

  165. me suba说道:

    VgT0D9 That is a beautiful shot with very good light-weight -)

  166. Really informative blog article.Much thanks again. Really Great.

  167. read stuff from. Thanks for posting when you

  168. It’а†s really a great and helpful piece of info. I’а†m happy that you simply shared this helpful info with us. Please keep us informed like this. Thanks for sharing.

  169. Say, you got a nice article.Thanks Again. Want more.

  170. Really appreciate you sharing this blog.Really looking forward to read more. Keep writing.

  171. Really appreciate you sharing this article post.Thanks Again. Really Great.

  172. Wow, awesome blog structure! How long have you ever been blogging for? you make blogging glance easy. The whole look of your web site is fantastic, as well as the content material!

  173. Wow! Thank you! I continuously wanted to write on my website something like that. Can I include a fragment of your post to my site?

  174. check out说道:

    Wow! This could be one particular of the most useful blogs We have ever arrive across on this subject. Actually Wonderful. I am also an expert in this topic so I can understand your effort.

  175. Thanks a lot for the blog article.Much thanks again. Great.

  176. bistro masa说道:

    I think this site holds some very fantastic info for everyone . аЂа‹аЂ The public will believe anything, so long as it is not founded on truth.аЂ аЂа› by Edith Sitwell.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>