Wouldn’t it be nice if your text editor just did what you said instead of making you slowly and manually add and delete characters? Vim doesn’t speak English, but it has a language of its own, built out of composable commands, that is much more efficient than the simple movement and editing commands you’ll find in other editors. In my last post, I took an initial look at Vim as a language. I’m going to dive deeper into that here.

如果你的文本编辑器只管按你说的做,而不是让你慢慢地手动增删字符,那不是很好吗?Vim不会说英语,但它有自己的语言,由可组合的命令组成, 比你在其他编辑器中找到的简单的移动和编辑命令要高效得多。在上一篇文章中,我初步了解了Vim作为一种语言。我将在这里更深入的了解一下。

Vim动词:你能做什么?

Vim’s “verbs” mostly fall into 2 main categories. Some of them act on a single character, and others act on a “motion” or “text object”. We’ll look at motions in a second, but lets start by looking at the verbs.

Vim的 "动词 "主要分为2大类。其中一些是作用于单个字符,另一些是作用于 "运动" 或 "文本对象"。但让我们先来看看动词。

Single character verbs 单字动词

So like I said, there are a few vim actions that act on a single character. They act as shortcuts for actions that you can also perform with motions, and allow you to save a few keystrokes.

所以就像我说的那样,有几个vim动作,作用在一个字符上。它们作为动作的快捷方式,你也可以用动作来执行,并允许你节省一些按键。

命令 动作

  • x 删除光标下的字符 Delete character under the cursor
  • r 用另一个字符替换光标下的字符 Replace character under cursor with another character
  • s 删除光标下的字符,并移动到插入模式 Delete character under cursor and move to insert mode

These are great commands to know, and things that I use daily, but they act as a bit of an island. Let’s look at some verbs with more power.

这些都是很好的命令,也是我每天都会用到的东西,但它们的作用有点像一个个孤岛。让我们来看看一些更强大的动词。

命令 动作

  • d 删除动议中指定的案文 Delete text specified by motion
  • c 删除动议指定的文本,进入插入模式 Delete text specified by motion and go into insert mode
  • y 动议中指定的文字被抄录下来 Yank (copy) text specified by motion

These aren’t the only 3 actions that you can use with motions, but they’re probably the most important. The first 2 are roughly the same as deleting or cutting in most instances, with the option to choose what mode you end in. The 3rd is Vim’s version of copying.

这3个动作并不是唯一的 3 个动作,但可能是最重要的。前2个动作在大多数情况下与删除或剪切大致相同,可以选择以什么模式结束。 第3个是Vim版本的复制。

動作 Motion

So how do we use these actions? You’ll notice that if you type any of the above characters into Vim by themselves, nothing happens. That’s because they’re expecting something to act on. If you’re a grammar nerd or remember your 8th grade english classes, you can think of these as transitive verbs that need to act on a direct object. These “direct objects” come in 2 forms, motions and text objects. Motions are the motion commands that you can use at any time to move around Vim. Some examples:

那么我们该如何使用这些操作呢?你会发现,如果你在Vim中输入上述任何一个字符,都不会发生任何事情。 这是因为它们在期待着什么动作。如果你是个语法书呆子,或者还记得八年级的英语课,你可以把这些词看作是需要对直接对象起作用的动词。 这些 "直接客体 "有两种形式,动词和文本对象。Motions是你可以在任何时候使用的运动命令在Vim中移动。以下是一些例子。

命令 运动 $ 去到行的尽头 Go to the end of the line G 转到文件的最后 Go to the end of the file f. 转到当前行中下一个出现的`.`的地方 Go to the next occurrence of . on the current line

All of these commands work as motions on their own, but when you combine them with actions you can get powerful effects. So d$ means “delete to the end of the line”. cf) means “change through the next closing parentheses” and yG means copy everything through the end of the file. The list above is only a small subset of the motions available. I’d suggest learning 1 or 2 to start and using them when appropriate. As you get more comfortable with Vim you can continue to add more, and each one will unlock a set of commands for all of the actions you know.

所有这些命令都可以作为动作单独工作,但是当你把它们和动作结合起来,你可以得到强大的效果。 所以 d$ 的意思是 "删除到行的末尾",cf) 的意思是 "通过下一个闭合的括号进行更改", yG 的意思是将所有的东西复制到文件的末尾。以上所列举的只是可用动作的一小部分。 我建议你先学习1到2个动作,然后在适当的时候使用它们。 随着你对Vim越来越熟悉,你可以继续增加更多的动作,每一个动作都会解锁一套你所知道的所有动作的命令。

The second type of “direct object” that Vim verbs can take is a text object. You can think of text objects as a “defined chunk of text.” Some examples include selecting words, html tag contents, or the contents of a function.

Vim动词可以采取的第二种 "直接对象 "是文本对象。你可以把文本对象看成是一个 "定义的文本块"。 一些例子包括选择单词、html 标签内容或函数的内容。

命令 文本对象 iw 适用于当前单词中的所有内容 Applies to everything in the current word it 它适用于当前xml/html标签中的所有内容 Applies to everything in current xml/html tag i{ 适用于最接近的卷曲方括号内的所有内容 Applies to everything inside nearest curly brackets

So using text objects you can change the contents of a word with the easily memorizable ciw (change in word), or copy the contents of an html <a> tag with yit. It’s worth mentioning here that one of the most powerful text objects is a bit of an oddball. A really common thing people do is applying an action to the current line. Vi’s language makes this easy by allowing the shortcut for this text object to be a simple repeat of the action. So yy yanks the current line, dd deletes it, and cc changes it.

所以使用文本对象,你可以用易记的 ciw(change in word)改变一个词的内容,或者用 yit 复制一个 HTML <a> 标签的内容。 这里值得一提的是,最强大的文本对象中,有一个有点奇葩。人们经常做的一件事就是对当前行应用一个动作。 Vi的语言允许这个文本对象的快捷方式是一个简单的重复动作,从而使这一点变得简单。因此,yy将当前行删除,dd将其删除,cc将其更改。

一点一点学

The key here is that learning Vim isn’t something you do in a weekend. It’s an iterative process. Since the actions, motions, and text objects build on themselves, you can start to grow your toolbox exponentially as time goes on. But you’ll want to start with a few commands that you find make you productive. I’ve personally found ciw, dd and ci( to be incredibly useful commands, and a great place to start as you learn to use these commands instead of the slower “mouse and select” methods that you may be used to. When we learn languages we all start with basic sentences, learn the grammar rules and grow from there. Vim is no different. Start small and add to your toolbox as you build great things with it.

这里的关键是,学习Vim并不是一个周末就能完成的事情。它是一个迭代的过程。 由于动作、动作和文本对象都是建立在自己的基础上的,所以随着时间的推移,你的工具箱可以开始成倍增长。 但是,你会想从几个你觉得能让你有成就感的命令开始。我个人发现 ciw、dd 和 ci(都是非常有用的命令, 也是你学习使用这些命令的好地方,而不是你可能习惯的 "鼠标和选择 "的慢速方法。当我们学习语言的时候, 我们都会从基本的句子开始,学习语法规则,然后在此基础上发展。Vim也不例外。从小事做起,在你用它来构建伟大的东西时, 你的工具箱里会有更多的东西。

More Resources

https://benmccormick.org/2014/07/02/learning-vim-in-2014-vim-as-language

https://www.youtube.com/watch?v=wlR5gYd6um0&list=PLWCQKA0C_ES9Q58WebiCKEaC6QjyyATJ8&index=2&t=0s

通过www.DeepL.com/Translator(免费版)翻译