我听说过很多关于Vim的事,包括优点和缺点。 (作为一名开发人员)使用Vim确实应该比使用其他任何编辑器都要快。 我用Vim来做一些基本的事情,我用Vim最多只能减少10倍的效率。

当你谈论速度时,只有两件事你应该关心(你可能不够关心,但你应该关心):

左右交替使用的 用手是最快的方法 键盘。 永远不要碰鼠标 第二种方法是尽可能快。 你要花很长时间才能移动你的手, 抓住鼠标,移动它,把它带来 回到键盘上(你经常这样做 看看键盘,确保你 将你的手正确地回到正确的位置)

下面的两个例子说明了为什么我使用Vim效率低得多。

复制/剪切和粘贴。我一直都这么做。在所有当代编辑器中,你用左手按Shift键,用右手移动光标来选择文本。然后按Ctrl+C复制,移动光标,按Ctrl+V粘贴。

Vim的情况很糟糕:

Yy复制一行(你几乎不需要整行!) [number xx]yy复制xx行到缓冲区。但你永远不知道你是否选择了你想要的。我经常要做[number xx]dd然后u来撤销!

另一个例子吗?搜索和替换。

在PSPad中:按Ctrl+f,然后输入你想要搜索的内容,然后按Enter。 在Vim: /中,然后输入你想要搜索的内容,然后如果有一些特殊字符,在每个特殊字符前放\,然后按Enter。

Vim的一切都是这样的:似乎我不知道如何正确处理它。

注:我已经读了Vim小抄:)

我的问题是:

与当代编辑器相比,您使用Vim的哪些方式使您的工作效率更高?


当前回答

提高编辑速度的第三个标准是所需的击键次数。我得说这比你的其他两个更重要。在vim中,几乎所有的操作都比我熟悉的任何其他编辑器需要更少的按键。

You mention that you are having trouble with cut & paste, but it sounds like you need more experience with general motion commands in vim. yank 3 words: y3w yank from the cursor to the next semi-colon: yf; yank to the next occurrence of your most recent search: yn All of those are much faster than trying to navigate with a mouse while holding down a modifier key. Also, as seen in some of the examples in CMS's response, vim's motion commands are highly optimized for efficient navigation in C and C++ source code.

至于“如何使用vim使我更有效率?”,我希望答案是“高效”。

其他回答

In addition to the great reply about grokking vi, it should be noted that vim does add some very vi-like features that make using vi commands nicer. The one that comes to mind first are text objects: instead of {!}fmt to reformat the current paragraph, !apfmt does the same. It works by first specifying that we want to select a text object, which is the current paragraph. Similar, to change the current string literal (foo to bar for an example), instead of T"ct"bar (move to just after the previous ", change until just before the next ", insert bar), you can say ci"bar: change inside (innermost) quotes, inserting bar.

从文本对象而不是移动命令的角度思考是非常不错的。

前几天我刚刚发现Vim的全功能完成,虽然我承认我有点模糊,但我已经获得了令人惊讶的好结果,只是混合Ctrl + x Ctrl + u或 Ctrl + n/Ctrl +p插入模式。这不是智能感知,但我还在学习。

试试吧!:帮助ins-completion

在代码中插入文本:

ctrl + v, (selecting text on multiple lines), I, (type something I want), ESC

记录一个宏来编辑文本并运行N次:

q, a (or some other letter), (do the things I want to record), q, ESC,
(type N, as in the number of times I want to run the macro), @, a

上周在工作中,我们的项目从另一个项目继承了大量Python代码。不幸的是,代码不适合我们现有的体系结构——它都是用全局变量和函数完成的,这在多线程环境中是行不通的。

我们有大约80个文件需要重做,使其成为面向对象的——所有的函数都移动到类中,参数被更改,导入语句被添加,等等。我们列出了大约20种需要对每个文件进行修复的类型。我估计一个人每天可以手工做2-4次。

所以我手工完成了第一个,然后写了一个vim脚本来自动更改。其中大部分是vim命令的列表。

" delete an un-needed function "
g/someFunction(/ d

" add wibble parameter to function foo "
%s/foo(/foo( wibble,/

" convert all function calls bar(thing) into method calls thing.bar() "
g/bar(/ normal nmaf(ldi(`aPa.

最后一个值得解释一下:

g/bar(/  executes the following command on every line that contains "bar("
normal   execute the following text as if it was typed in in normal mode
n        goes to the next match of "bar(" (since the :g command leaves the cursor position at the start of the line)
ma       saves the cursor position in mark a
f(       moves forward to the next opening bracket
l        moves right one character, so the cursor is now inside the brackets
di(      delete all the text inside the brackets
`a       go back to the position saved as mark a (i.e. the first character of "bar")
P        paste the deleted text before the current cursor position
a.       go into insert mode and add a "." 

对于一些更复杂的转换,比如生成所有import语句,我在vim脚本中嵌入了一些python。

经过几个小时的工作,我有一个脚本,将做至少95%的转换。我只是在vim中打开一个文件,然后运行:source fixit。Vim和文件转换在眨眼之间。

我们仍然需要改变剩下的5%不值得自动化的部分,并测试结果,但是通过花一天时间编写这个脚本,我估计我们已经节省了几周的工作。

当然,使用像Python或Ruby这样的脚本语言也可以实现自动化,但这将花费更长的时间来编写,并且灵活性会更低——最后一个例子将会很困难,因为regex本身无法处理嵌套的括号,例如将bar(foo(xxx))转换为foo(xxx).bar()。Vim非常适合这项任务。

实际上我总是丢失我的缓冲区,所以我通常复制一行的方式是把它写到一个临时文件,然后在适当的位置读入。

e.g.

Ka(设定标记)

”,。w !T(将标记中的所有内容复制到文件T)

. .移动你的光标。

:.rt(将t读入当前点)。

与使用缓冲区相比,这可能需要更少的击键,更容易跟踪,并且您可以拥有长期存在的粘贴文件。为了速度,我通常用一个字母来命名文件。

快速移动光标的关键原因是,你只需使用搜索函数“/”,如果你在到达你想去的地方之前遇到另一个模式,则使用“n”。