Vim学习指南

来源:互联网 发布:指环王 知乎 编辑:程序博客网 时间:2024/06/06 12:49

tl;dr: You want to teach yourself vim (the best text editor known to human kind) in the fastest way possible. This is my way of doing it. You start by learning the minimal to survive, then you integrate all the tricks slowly.

Vim the Six Billion Dollar editor

Better, Stronger, Faster.

Learn vim and it will be your last text editor. There isn’t any better text editor that I know of. It is hard to learn, but incredible to use.

I suggest you teach yourself Vim in 4 steps:

  1. Survive
  2. Feel comfortable
  3. Feel Better, Stronger, Faster
  4. Use superpowers of vim

By the end of this journey, you’ll become a vim superstar.

But before we start, just a warning. Learning vim will be painful at first. It will take time. It will be a lot like playing a musical instrument. Don’t expect to be more efficient with vim than with another editor in less than 3 days. In fact it will certainly take 2 weeks instead of 3 days.

译者信息

你想尽可能快地自学vim(为大家所熟知的最好的编辑器) 。这是我学习的方法:从细处入手然后慢慢掌握所有技巧。

Vim 六十亿美元的编辑器

设计优良,强壮,快速。

学习vim并把它作为你的下一个文本编辑器,据我所知没有比它更好的编辑器,学起来难但出奇的好用。

我建议你按下附4个步骤学习Vim:

  1. 适应
  2. 感觉舒适
  3. 感觉很好,强壮,快速
  4. 使用vim的高级技能

本课程结束时你将成为一个vim高手。

但在我们开始之前,先提醒一下。刚开始学习vim会有点痛苦,它很费时间,有点像玩乐器一样。不要指望能像其他编辑器一样花3天以内的时间便能很好的使用它,事实上,这肯定要花2周而不是3天。

1st Level – Survive

  1. Install vim
  2. Launch vim
  3. DO NOTHING! Read.

In a standard editor, typing on the keyboard is enough to write something and see it on the screen. Not this time. Vim is in Normal mode. Let’s go toInsert mode. Type the letteri.

You should feel a bit better. You can type letters like in a standard editor. To get back to Normal mode just press theESCkey.

You now know how to switch between Insert and Normal mode. And now, here are the commands that you need in order to survive in Normal mode:

  • i→ Insert mode. TypeESCto return to Normal mode.
  • x→ Delete the char under the cursor
  • :wq→ Save and Quit (:wsave,:qquit)
  • dd→ Delete (and copy) the current line
  • p→ Paste

Recommended:

  • hjkl(highly recommended but not mandatory) → basic cursor move (←↓↑→). Hint:jlooks like a down arrow.
  • :help <command>→ Show help about<command>. You can use:helpwithout a<command>to get general help.

译者信息

第一层次 – 适应

  1. 安装vim
  2. 运行vim
  3. 什么也别做,读!

在标准的编辑器里,写一些东西到屏幕上只需要在键盘上输入就足够了,在Vim中不是这样。Vim在普通模式里,让我们转到插入模式,键入字符i。

你应该感觉好一点了,能像在标准编辑器里输入了,回到普通模式只需按ESC键即可。

你现在知道了如何在插入和普通模式之间转换,下面是你在普通模式下需要记住的指令:

  • i→ 插入模式. 按ESC键返回普通模式
  • x→ 删除光标所在的字符
  • :wq→ 保存退出(:w 保存,:q 退出)
  • dd→ 删除(拷贝)当前行
  • p→ 粘贴

推荐:

  • hjkl(高度推荐) → 基本的光标移动 (←↓↑→), hj按行移动。
  • :help <command>→ 显示关于<command>的帮助,可以使用help不带<command>获得常规帮助。
Only 5 commands. That is all you need to get started. Once these command start to become natural (maybe after a day or so), you should move on to level 2.

But first, just a little remark about Normal mode. In standard editors, to copy you have to use theCtrlkey (Ctrl-cgenerally). In fact, when you pressCtrl, it is as if all of your keys change meaning. Using vim in normal mode is a bit like having the editor automatically press theCtrlkey for you.

A last word about notations:

  • instead of writingCtrl-λ, I’ll write<C-λ>.
  • commands starting with:end with<enter>. For example, when I write:q, I mean:q<enter>.

译者信息

只有5个命令,一开始只需掌握这些,当适应了这些命令后(大概需要一天或更多的诗句),你可以转到第二层级了。

但首先,稍回顾一下普通模式。在标准编辑器里,要复制的话你需要使用ctrl键(Ctrl-c)。实际上,当你按下ctrl键时所有的按键的意义都改变了。在vim普通模式下就像在ctrl键自动按下的情况下使用编辑器。

最后的补充:

  • 我使用<C-λ>代替Ctrl-λ
  • 命令后附带回车,比如我若是写:q,我是指:q<enter>

2nd Level – Feel comfortable

You know the commands required for survival. It’s time to learn a few more commands. These are my suggestions:

  1. Insert mode variations:

    • a→ insert after the cursor
    • o→ insert a new line after the current one
    • O→ insert a new line before the current one
    • cw→ replace from the cursor to the end of the word
  2. Basic moves

    • 0→ go to the first column
    • ^→ go to the first non-blank character of the line
    • $→ go to the end of line
    • g_→ go to the last non-blank character of line
    • /pattern→ search forpattern
  3. Copy/Paste

    • P→ paste before, rememberpis paste after current position.
    • yy→ copy the current line, easier but equivalent toddP
  4. Undo/Redo

    • u→ undo
    • <C-r>→ redo
  5. Load/Save/Quit/Change File (Buffer)

    • :e <path/to/file>→ open
    • :w→ save
    • :saveas <path/to/file>→ save to<path/to/file>
    • :x,ZZor:wq→ save and quit (:xonly save if necessary)
    • :q!→ quit without saving, also::qa!to quit even if there are modified hidden buffers.
    • :bn(resp.:bp) → show next (resp. previous) file (buffer)

Take the time to learn all of these command. Once done, you should be able to do every thing you are able to do in other editors. You may still feel a bit awkward. But follow me to the next level and you’ll see why vim is worth the extra work.

译者信息

第二层 - 爽一把

你知道,命令是由需求所定。是时候学习更多命令了。下面这些是我的建议:

  1. 插入模式变种命令:

    • a→ 在光标后插入
    • o→ 在当前行之后插入新行
    • O→ 在当前行之前插入新行
    • cw→ 替换从光标到单词结束
  2. 基本移动命令

    • 0→ 跳到第一列
    • ^→ 跳到本行第一个非空字符
    • $→ 跳到本行末尾
    • g_→ 跳到本行最后一个非空字符
    • /pattern→ 搜索pattern
  3. 复制/粘贴

    • P→ 在当前位置之前粘贴,记住 p 是在当前位置之前粘贴.
    • yy→ 复制当前行,与dd和P命令相比更简单。
  4. 取消/恢复

    • u→ 取消
    • <C-r>→ 恢复
  5. 加载/保存/退出/修改 文件(缓存)

    • :e <文件路径>→ 打开
    • :w→ 保存
    • :saveas <文件路径>→ 保存到这个文件
    • :x,ZZ或者:wq→ 保存和退出 (:x 如果可能的话,只保存)
    • :q!→ 退出但不保存,使用:qa!,即使在缓存中还有已经修改的也会退出。
    • :bn(对比:bp) → 显示下一个(上一个)文件缓存

花些时间学习所有这些命令。一旦完成后,你应该就能完成在像在其它编辑器中所有事情。也许,你可能还会觉得有点笨手笨脚。但是跟着我进入下一个层次,你就会发现为什么vim值得你额外付出的努力。

3rd Level – Better. Stronger. Faster.

Congratulation for reaching this far! Now we can start with the interesting stuff. At level 3, we’ll only talk about commands which are compatible with the old vi editor.

Better

Let’s look at how vim could help you to repeat yourself:

  1. .→ (dot) will repeat the last command,
  2. N<command> → will repeat the command N times.

Some examples, open a file and type:

  • 2dd→ will delete 2 lines
  • 3p→ will paste the text 3 times
  • 100idesu [ESC]→ will write “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu”
  • .→ Just after the last command will write again the 100 “desu”.
  • 3.→ Will write 3 “desu” (and not 300, how clever).

译者信息

第三层次——更好,更强,更快

为达到这一步表示祝贺!现在我们可以开始这有趣的东西。在第三层次,我们将只讨论命令,它兼容旧的vi编辑器。

更好

让我们看看vim是怎么帮你做重复事情的。

1. .→(点)可以重复最后一个命令,

2. N<命令>会重复命令N次。

一些例子,打开一个文件然后写入下面内容:

• 2dd→ will delete 2 lines • 3p→ will paste the text 3 times • 100idesu [ESC]→ will write “desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu desu” •.→ Just after the last command will write again the 100 “desu”. •3.→ Will write 3 “desu” (and not 300, how clever).
Stronger

Knowing how to move efficiently with vim is very important. Don’t skip this section.

  1. NG→ Go to line N
  2. gg→ shortcut for1G- go to the start of the file
  3. G→ Go to last line
  4. Word moves:

    1. w→ go to the start of the following word,
    2. e→ go to the end of this word.

    By default, words are composed of letters and the underscore character. Let’s call a WORD a group of letter separated by blank characters. If you want to consider WORDS, then just use uppercase characters:

    1. W→ go to the start of the following WORD,
    2. E→ go to the end of this WORD.
    Word moves example

Now let’s talk about very efficient moves:

  • %: Go to the corresponding(,{,[.
  • *(resp.#) : go to next (resp. previous) occurrence of the word under the cursor

Believe me, the last three commands are gold.

译者信息

更强

掌握高效率使用vim是十分重要的。请不要跳过这节。

  1. NG→ 跳到第N行
  2. gg→ 是1G的快捷方式 - 跳到文件的开始
  3. G→ 跳到最后一行
  4. 单词间移动:

    1. w→ 跳到下一个单词的开头,
    2. e→ 跳到这个单词的末尾.

    默认,单词是有字母和下划线组成。这里我们所谓的单词就是由空格分隔的。如果你只想考虑单词,就使用大写的字符吧:

    1. W→跳到下一个单词的开头,
    2. E→ 跳到这个单词的末尾.
    Word moves example

现在,我们谈论高效率移动:

  • %: 跳到对应的(, (, [ 处.
  • *(对比#) : 跳到当前光标的下一个(上一个) 相同单词的地方

信我,最后3个命令价比黄金。

Faster

Remember about the importance of vi moves? Here is the reason. Most commands can be used using the following general format:

<start position><command><end position>

For example :0y$means

  • 0→ go to the beginning of this line
  • y→ yank from here
  • $→ up to the end of this line

We also can do things likeye, yank from here to the end of the word. But alsoy2/fooyank up to the second occurrence of “foo”.

But what was true fory(yank), is also true ford(delete),v(visual select),gU(uppercase),gu(lowercase), etc…

译者信息

更快

记住了vi移动的重要性?这里就是原因。大多数命令使用下面这种通用格式:

<开始位置><命令><结束位置>

例如: 0y$意味着

  • 0→ 跳到本行开头
  • y→ 从这里开始复制
  • $→ 直到本行结束

我们也可以使用ye,从当前位置复制到单词的末尾。但是y2/foo 会一直复制到第二个foo出现的地方。

但对于y(复制), d(删除), v(visual 选择), gU(大写), gu(小写)等等命令都是真实的。

4th Level – Vim Superpowers

With all preceding commands you should be comfortable using vim. But now, here are the killer features. Some of these features were the reason I started to use vim.

Move on current line:0^$g_fFtT,;

  • 0→ go to column 0
  • ^→ go to first character on the line
  • $→ go to the last column
  • g_→ go to the last character on the line
  • fa→ go to next occurrence of the letteraon the line.,(resp.;) will find the next (resp. previous) occurrence.
  • t,→ go to just before the character,.
  • 3fa→ find the 3rd occurrence ofaon this line.
  • FandT→ likefandtbut backward.
    Line moves

A useful tip is:dt"→ remove everything until the".

译者信息

第四层次 – Vim 超能量

使用上述的命令,你就觉得很爽了。但是现在,这里才是杀手锏。正因为其中有些特性,我才使用vim的。

在当前行移动:0^$g_fFtT,;

  • 0→ 跳到第一列
  • ^→ 跳到当前行的第一个字符
  • $→ 跳到最后一列
  • g_→ 跳到这行的最后一个字符
  • fa→ 跳到这行a字母的下一个出现的地方。(对比 ; )会查找下一个(上一个)地方
  • t,→ 跳到,字符的前一个字符.
  • 3fa→ 在这行中查找a出现的第三个位置.
  • F 和 T→ 与f和t相似, 但是方向相反.
  • Line moves

一个有用的提示:dt"→删除所有的直到”。 

Zone selection<action>a<object>or<action>i<object>

These command can only be used after an operator in visual mode. But they are very powerful. Their main pattern is:

<action>a<object>and<action>i<object>

Where action can be any action, for example,d(delete),y(yank),v(select in visual mode). The object can be:wa word,Wa WORD (extended word),sa sentence,pa paragraph. But also, natural character such as",',),},].

Suppose the cursor is on the firstoof(map (+) ("foo")).

  • vi"→ will selectfoo.
  • va"→ will select"foo".
  • vi)→ will select"foo".
  • va)→ will select("foo").
  • v2i)→ will selectmap (+) ("foo")
  • v2a)→ will select(map (+) ("foo"))

Text objects selection

译者信息

区域选择 : <命令>a<对象> 或 <命令>i<对象>

这些命令仅仅能用在可视化模式的一个操作后。但是他们相当的有用。他们的主要模式有:

<命令>a<对象> 和 <命令>i<对象>

命令可以是任何命令,例如 , d(删除),y(拉取),v(进入可视化模式)。对象可以是: w一个单词,W一个单词 (扩展的),s一个句子,p一个段落。而且也可以是普通的字符, 例如",',),},]。

假设现在光标在 (map (+) ("foo")) 这个字符串的第一个o上。

  • vi"→ 将会选择 foo
  • va"→ 将会选择 "foo"
  • vi)→ 将会选择 "foo"
  • va)→ 将会选择 ("foo")
  • v2i)→ 将会选择 map (+) ("foo")
  • v2a)→ 将会选择 (map (+) ("foo"))

Text objects selection

Select rectangular blocks:<C-v>.

Rectangular blocks are very useful for commenting many lines of code. Typically:0<C-v><C-d>I-- [ESC]

  • ^→ go to the first non-blank character of the line
  • <C-v>→ Start block selection
  • <C-d>→ move down (could also bejjjor%, etc…)
  • I-- [ESC]→ write--to comment each line
Rectangular blocks

Note: in Windows you might have to use<C-q>instead of<C-v>if your clipboard is not empty.

译者信息

矩形选择块:<C-v>

矩形选择块能够方便的对多行代码进行注释。 如:0<C-v><C-d>I-- [ESC]

  • ^→ 将光标定位到这行第一个非空格字符
  • <C-v>→ 选择开始位置
  • <C-d>→ 向下移动 (也可使用 jjj 或者 % , 其他…)
  • I-- [ESC]→ 用 -- 来注释每一行  
Rectangular blocks

提示:如果你的剪贴板在Windows中是非空的话,那么你就不得不使用<C-q>来代替<C-v>。

Completion:<C-n>and<C-p>.

In Insert mode, just type the start of a word, then type<C-p>, magic…
Completion

Macros :qado somethingq,@a,@@

qarecord your actions in the register a. Then@awill replay the macro saved into the registeraas if you typed it.@@is a shortcut to replay the last executed macro.

Example

On a line containing only the number 1, type this:

  • qaYp<C-a>q→
    • qastart recording.
    • Ypduplicate this line.
    • <C-a>increment the number.
    • qstop recording.
  • @a→ write 2 under the 1
  • @@→ write 3 under the 2
  • Now do100@@will create a list of increasing numbers until 103.

Macros

译者信息

实现:<C-n>和<C-p>.

在插入模式中:只要打出单词的首字母,然后按<C-p>,神奇的事情就发生了...

Completion

宏命令 : qa 做的一些事,@a,@@

qa 把你的动作记录在了寄存器 a 中。然后@a 就会将存在寄存器中的动作重现,就好像你从新打了一边。@@是将你最后一次执行的宏命令再次执行的快捷键。

事例

在只有数字1的一行上, 像这样键入命令:

  • qaYp<C-a>q→
    • qa开始记录。
    • Yp记录这一行。
    • <C-a>增加数字大小。
    • q停止记录。
  • @a→ 在1下面写2
  • @@→ 在2下面写3
  • 现在键入 100@@ 就将会产生一个到 103 的自增列。

Macros

Visual selection:v,V,<C-v>

We saw an example with<C-v>. There is alsovandV. Once the selection has been made, you can:

  • J→ join all the lines together.
  • <(resp.>) → indent to the left (resp. to the right).
  • =→ auto indent
Autoindent

Add something at the end of all visually selected lines:

  • <C-v>
  • go to desired line (jjjor<C-d>or/patternor%etc…)
  • $go to the end of the line
  • A, write text,ESC.

Append to many lines

译者信息

可视化模式选择: v, V, <C-v>

我们已经看到过<C-v>例子,这里还有v和V。一旦已经选择好了,你可以:

  • J→ 让所有行连接在一起
  • <(对比>) → 向左(右)对齐.
  • =→ 自动对齐
Autoindent

在可视化模式所选择的行的末尾添加一些东西:

  • <C-v>
  • 跳到目的行 (jjj 或者 <C-d> 或者 /patternor%等等…)
  • $跳到行末尾
  • A, 写入一些文本,[按] ESC 键.

Append to many lines

Splits::splitandvsplit.

These are the most important commands, but you should look at:help split.

  • :split→ create a split (:vsplitcreate a vertical split)
  • <C-w><dir>: where dir is any ofhjklor ←↓↑→ to change the split.
  • <C-w>_(resp.<C-w>|) : maximise the size of the split (resp. vertical split)
  • <C-w>+(resp.<C-w>-) : Grow (resp. shrink) split

Split

译者信息

分割:: split 和 vsplit

这些是很重要的命令,所以你应该在:help split看看。

  • :split→ 创建一个水平分割窗口t (:vsplit 创建一个竖直分割窗口)
  • <C-w><dir>:  dir是hjklor←↓↑→中任何一个。用来切换分割窗口。
  • <C-w>_(对比<C-w>|) : 用来最大化水平(竖直)分割窗口的大小
  • <C-w>+(对比<C-w>-) : 增加 (减小) 分割窗口

Split

Conclusion

That was 90% of the commands I use every day. I suggest that you learn no more than one or two new commands per day. After two to three weeks you’ll start to feel the power of vim in your hands.

Learning Vim is more a matter of training than plain memorization. Fortunately vim comes with some very good tools and excellent documentation. Run vimtutor until you are familiar with most basic commands. Also, you should read this page carefully::help usr_02.txt.

Then, you will learn about!, folds, registers, plugins and many other features. Learn vim like you’d learn piano and all should be fine.

译者信息

小结

这些是我经常使用的90%的命令。我建议你每天学习一到两个命令。在两到三周之后,你将会感觉到vim在你手上使用的是如此的强大。

学习vim更多的是记忆训练。值得高兴的是vim诞生了一些非常好的工具和优秀的文档。当你非常熟悉大部分基本命令之后就可以使用vimtutor了。另外,你应该仔细的阅读这页 ::help usr_02.txt.

然后,你要去学习了解寄存器,插件已经其它的功能。学习vim就像学钢琴一样,所有的都要掌握好。


原创粉丝点击