我听说过很多关于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的问题:

:map <C-F> /\V

这将使Ctrl-F开始搜索,搜索的第一个字符是\V,这将关闭所有的“魔法”,所以你不需要转义任何东西(就像PsPad)。

" CTRL-X and SHIFT-Del are Cut
vnoremap <C-X> "+x
vnoremap <S-Del> "+x

" CTRL-C and CTRL-Insert are Copy
vnoremap <C-C> "+y
vnoremap <C-Insert> "+y

" CTRL-V and SHIFT-Insert are Paste
map <C-V>       "+gP
map <S-Insert>      "+gP

cmap <C-V>      <C-R>+
cmap <S-Insert>     <C-R>+

(直接从mswin中取出。来自vim发行版的vim)。这将给你Ctrl-C, Ctrl-V等复制和粘贴。

Personally, I find the copying and pasting much better with the standard Vim. In Vim I can have the cursor on a line, type yy to copy it, 10p to paste 10 copies of it. With the Align plugin, I can then use Ctrl-V (assuming the windows mappings above aren't used) to visual-block select a column of numbers that has been created; :II then auto-increments that column of numbers. Similarly, with YankRing, I can copy 10 (or more) lines one after another and then paste them back out one after another.

至于Vim让我更有效率的方式,我想说标签文件是我再也离不开的东西。当光标在标签上时,我可以按Ctrl-]去函数(和Ctrl- t返回,尽可能多的级别)或,p在预览窗口中打开函数或宏定义(或任何东西)(然后可以用:pcl快速关闭)或[I只是在状态栏上显示宏定义。对于浏览复杂的源代码,这些工具是无价的。

预览窗口依赖于.vimrc中的映射,但是:

:map ,p :ptag <C-R><C-W><CR>

标签文件还允许使用一些(我的)插件,这些插件提供语法高亮显示,更清楚地显示错误(通过高亮识别的标签,而不是高亮未识别的标签)和标签签名,当你将鼠标移动到关键字上时。

其他回答

上周在工作中,我们的项目从另一个项目继承了大量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非常适合这项任务。

. 重复最后一个修改文本的命令

我用这个节省了很多时间。

前面提到了可视化模式,但是块可视化模式在编辑文本文件中固定大小的列时为我节省了大量时间。(用Ctrl-V访问)。

我最有成效的vi/Vim技巧是:

:%s

我喜欢能够使用正则表达式来替换文件中我想要的任何内容。恕我直言,每个文本编辑器都应该支持正则表达式。

在代码中插入文本:

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

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.

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