我听说过很多关于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的哪些方式使您的工作效率更高?


当前回答

我是美国密码协会的成员。这本双月刊杂志包含了超过100种不同类型的密码。其中大约有15个是“密码学”——用字母代替数字的各种类型的算术问题。其中两三个是数独游戏,只不过是字母而不是数字。当网格完成后,九个不同的字母将在网格中的某个地方以某条线、对角线、螺旋线等方式拼出一个或多个单词。

我不用铅笔,也不用手写,而是从他们网站的会员区下载问题。

在处理这些数独游戏时,我使用vi,只是因为我使用了vi所具有的其他编辑器所没有的功能。主要是在把字母网格转换成有编号的网格,因为我觉得这样更容易解决,然后把完成的有编号的网格转换回字母网格中,找到解单词或单词。

这个问题被格式化为9组,每组9个字母,-s代表空格,写在两行中。第一步是将它们格式化成每行9个字符的9行。这没有什么特别的,只是在适当的位置插入8个换行符。

结果如下所示:

T-O-----C
-E-----S-
--AT--N-L
---NASO--
---E-T---
--SPCL---
E-T--OS--
-A-----P-
S-----C-T

So, first step in converting this into numbers is to make a list of the distinct letters. First, I make a copy of the block. I position the cursor at the top of the block, then type :y}}p. : puts me in command mode, y yanks the next movement command. Since } is a move to the end of the next paragraph, y} yanks the paragraph. } then moves the cursor to the end of the paragraph, and p pastes what we had yanked just after the cursor. So y}}p creates a copy of the next paragraph, and ends up with the cursor between the two copies.

接下来,我要把其中一个副本变成一个不同字母的列表。这个命令有点复杂:

:!}tr -cd A-Z | sed 's/\(.\)/\1\n/g' | sort -u | tr -d '\n'

: again puts me in command mode. ! indicates that the content of the next yank should be piped through a command line. } yanks the next paragraph, and the command line then uses the tr command to strip out everything except for upper-case letters, the sed command to print each letter on a single line, and the sort command to sort those lines, removing duplicates, and then tr strips out the newlines, leaving the nine distinct letters in a single line, replacing the nine lines that had made up the paragraph originally. In this case, the letters are: ACELNOPST.

下一步是制作网格的另一个副本。然后用我刚刚确定的字母把这些字母替换成1到9之间的数字。很简单::!}tr ACELNOPST 0-9。结果是:

8-5-----1
-2-----7-
--08--4-3
---4075--
---2-8---
--7613---
2-8--57--
-0-----6-
7-----1-8

这可以用通常的方式解决,或者输入任何你喜欢的数独求解器。然后,完成的解决方案可以转换回字母:!}tr 1-9 ACELNOPST。

vi的强大是很少有其他工具能比拟的。最大的问题是,只有极少数的vi教程书籍、网站、帮助文件等,几乎触及了可能的表面。

其他回答

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

在搜索的任何地方使用\c来忽略大小写(覆盖你的ignorecase或smartcase设置)。 例如/\cfoo或/foo\c将匹配foo, foo, foo, foo等。

在搜索的任何地方使用\C强制大小写匹配。 例如/\Cfoo或/foo\C只匹配foo。

使用内置的文件资源管理器!这个命令是:Explore,它允许你非常非常快地浏览源代码。我在我的。vimrc中有这些映射:

map <silent> <F8>   :Explore<CR>
map <silent> <S-F8> :sp +Explore<CR>

资源管理器还允许您修改文件。我将发布一些我最喜欢的键,按<F1>将给你完整的列表:

-:最有用的:更改到上目录(cd ..) mf:标记文件 D:如果没有标记,删除标记文件或光标所在的文件。 R:重命名光标所在的文件。 d:在当前目录下新建目录 %:在当前目录下创建一个新文件

在代码中插入文本:

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

多个缓冲区,特别是在它们之间快速跳转,以比较:bp和:bn的两个文件(正确地重新映射到a Shift + p或Shift + n)

Vimdiff模式(在两个垂直缓冲区中分割,用颜色显示差异)

区域复制Ctrl + v

最后,标签完成标识符(搜索“mosh_tab_or_complete”)。这改变了我的生活。