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


当前回答

维姆插件

这里有很多很好的答案,其中一个关于vi的禅意的答案令人惊讶。我没有看到提到的一件事是vim可以通过插件进行极大的扩展。有脚本和插件可以让它做原作者从未考虑过的各种疯狂的事情。下面是一些非常好用的vim插件的例子:

rails.vim

Rails。Vim是tpope编写的插件。对于从事rails开发的人来说,这是一个不可思议的工具。它做了一些神奇的上下文敏感的事情,允许您轻松地从控制器中的方法跳转到相关视图,再跳转到模型,再向下跳转到该模型的单元测试。它为rails开发人员节省了数十甚至数百个小时的时间。

gist.vim

这个插件允许你在视觉模式下选择一个文本区域,然后输入一个快速命令将其发布到gi.github.com。这允许简单的pastebin访问,如果你通过IRC或IM与某人合作,这是非常方便的。

space.vim

这个插件为空格键提供了特殊的功能。它把空格键变成类似于周期的东西,但它不是重复动作,而是重复运动。这对于以动态定义的方式快速浏览文件非常方便。

surround.vim

这个插件使您能够处理以某种方式分隔的文本。它给出的对象表示括号内的东西,引号内的东西,等等。它可以在操作带分隔符的文本时派上用场。

supertab.vim

这个脚本为vim带来了花哨的选项卡补全功能。在vim的核心中已经有了自动完成的东西,但是这使它变成了一个快速的选项卡,而不是多个不同的多键快捷键。非常方便,而且使用起来非常有趣。虽然它不是VS的智能感知,但它是一个伟大的步骤,并带来了大量您希望从选项卡补全工具中期望的功能。

syntastic.vim

该工具将外部语法检查命令引入vim。我个人没有使用过它,但我听说过关于它的很棒的东西,而且它的概念很难被超越。无需手动检查语法可以极大地节省时间,并且可以帮助您在引入语法错误时捕获它们,而不是在最终停止测试时捕获它们。

fugitive.vim

从vim内部直接访问git。同样,我没有使用过这个插件,但我可以看到实用程序。不幸的是,在我所处的文化中,svn被认为是“新”的,所以我在很长一段时间内不太可能在工作中看到git。

nerdtree.vim

用于vim的树浏览器。我最近开始用这个,真的很方便。它可以让您在垂直分割中放置树视图,并轻松打开文件。这对于一个经常在许多源文件之间切换的项目非常有用。

FuzzyFinderTextmate.vim

这是一个未维护的插件,但仍然非常有用。它提供了使用“模糊”描述性语法打开文件的能力。这意味着在一个稀疏的文件树中,您只需要输入足够的字符来消除您感兴趣的文件与其余杂乱文件的歧义。

结论

有许多令人难以置信的工具可用于vim。我确信我在这里只触及了表面,寻找适用于您的领域的工具是非常值得的。结合了传统vi强大的工具集,vim的改进,以及进一步扩展vim的插件,它是有史以来最强大的编辑文本的方法之一。Vim与emacs、eclipse、visual studio和textmate一样强大。

谢谢

感谢duwanis的vim配置,从中我学到了很多,并借鉴了这里列出的大部分插件。

其他回答

特别是对于复制/剪切和粘贴,使用可视化模式更容易适应其他编辑器。我通常复制粘贴的方法是

Esc - exit Insert mode (skip if you are already in Normal mode) v - turn on visual mode move about the file to select the text you want - visual mode will show you what characters are selected. For a few words w, e and b are useful (move to start of next word, end of next word and start of this/previous word respectively). d - cut the text (use y if you want to copy the text) move about to where you want the text to go p - paste (this pastes after the current character, P pastes before the current character.

同样有用的是使用V进入视觉模式(行),它会自动选择整行,无论光标在哪里。

您可以搜索寄存器的内容。

假设你的寄存器x包含

字符串搜索

要搜索此字符串,必须在普通模式下键入 / CTRL-rxENTER

它会粘贴x寄存器的内容。

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

在将以下内容映射为一个简单的组合键后,以下内容对我来说非常有用:

在浏览文件路径时跳转到文件中

gf

获取现有文件的完整路径名

:r!echo %:p

获取现有文件的目录

:r!echo %:p:h

运行代码:

:!ruby %:p

ruby的缩写:

ab if_do if end<esc>bi<cr><esc>xhxO
ab if_else if end<esc>bi<cr><esc>xhxO else<esc>bhxA<cr> <esc>k$O
ab meth def method<cr>end<esc>k<esc>:s/method/
ab klas class KlassName<cr>end<esc>k<esc>:s/KlassName/
ab mod module ModName<cr>end<esc>k<esc>:s/ModName/

运行当前程序:

   map ,rby :w!<cr>:!ruby %:p<cr>

检查当前程序的语法:

   map ,c :w!<cr>:!ruby -c %:p<cr>

运行当前规格程序的所有规格:

   map ,s :w!<cr>:!rspec %:p<cr>

把它打开:

   map ,i :w!<cr>:!irb<cr>

rspec缩写:

   ab shared_examples shared_examples_for "behavior here" do<cr>end
   ab shared_behavior describe "description here" do<cr>  before :each do<cr>end<cr>it_should_behave_like "behavior here"<cr><bs>end<cr>
   ab describe_do describe "description here" do<cr>end
   ab context_do describe "description here" do<cr>end
   ab it_do it "description here" do<cr>end
   ab before_each before :each do<cr>end<cr>

rails的缩写:

用户认证:

     ab userc <esc>:r $VIMRUNTIME/Templates/Ruby/c-users.rb<cr>
     ab userv <esc>:r $VIMRUNTIME/Templates/Ruby/v-users.erb<cr>
     ab userm <esc>:r $VIMRUNTIME/Templates/Ruby/m-users.rb<cr>

在firefox中打开可视化选择的url:

"function
   function open_url_in_firefox:(copy_text)
     let g:open_url_in_firefox="silent !open -a \"firefox\" \"".a:copy_text."\""
     exe g:open_url_in_firefox
   endfunction

"abbreviations
   map ,d :call open_url_in_firefox:(expand("%:p"))<cr>
   map go y:call open_url_in_firefox:(@0)<cr> 

Rspec:运行包含当前行的规范:

"function
   function run_single_rspec_test:(the_test)
     let g:rake_spec="!rspec ".a:the_test.":".line(".")
     exe g:rake_spec
   endfunction

"abbreviations
   map ,s :call run_single_rspec_test:(expand("%:p"))<cr>

Rspec-rails:包含当前行的运行规范:

"function
   function run_single_rails_rspec_test:(the_test)
     let g:rake_spec="!rake spec SPEC=\"".a:the_test.":".line(".")."\""
     exe g:rake_spec
   endfunction

"abbreviations
   map ,r :call run_single_rails_rspec_test:(expand("%:p"))<cr>

Rspec-rails:运行规范包含当前行调试:

"function
   function run_spec_containing_current_line_with_debugging:(the_test)
     let g:rake_spec="!rake spec SPEC=\"".a:the_test.":".line(".")." -d\""
     exe g:rake_spec
   endfunction

"abbreviations
   map ,p :call run_spec_containing_current_line_with_debugging:(expand("%:p")) <cr>

html

"abbreviations

  "ab htm <html><cr><tab><head><cr></head><cr><body><cr></body><cr><bs><bs></html>
   ab template_html <script type = 'text/template' id = 'templateIdHere'></script>
   ab script_i <script src=''></script>
   ab script_m <script><cr></script>
   ab Tpage <esc>:r ~/.vim/templates/pageContainer.html<cr>
   ab Ttable <esc>:r ~/.vim/templates/listTable.html<cr>

"function to render common html template

   function html:() 
     call feedkeys( "i", 't' )
     call feedkeys("<html>\<cr>  <head>\<cr></head>\<cr><body>\<cr> ", 't')
     call feedkeys( "\<esc>", 't' )
     call feedkeys( "i", 't' )
     call include_js:()
     call feedkeys("\<bs>\<bs></body>\<cr> \<esc>hxhxi</html>", 't')
   endfunction

javascript

"jasmine.js
  "abbreviations
   ab describe_js describe('description here', function(){<cr>});
   ab context_js context('context here', function(){<cr>});
   ab it_js it('expectation here', function(){<cr>});
   ab expect_js expect().toEqual();
   ab before_js beforeEach(function(){<cr>});
   ab after_js afterEach(function(){<cr>});

"function abbreviations

   ab fun1 function(){}<esc>i<cr><esc>ko
   ab fun2 x=function(){};<esc>hi<cr>
   ab fun3 var x=function(){<cr>};

"method for rendering inclusion of common js files

   function include_js:()
     let includes_0  = "  <link   type = 'text\/css' rel = 'stylesheet' href = '\/Users\/johnjimenez\/common\/stylesheets\/jasmine-1.1.0\/jasmine.css'\/>"
     let includes_1  = "  <link   type = 'text\/css' rel = 'stylesheet' href = '\/Users\/johnjimenez\/common\/stylesheets\/screen.css'\/>"
     let includes_2  = "<script type = 'text\/javascript' src = '\/Users\/johnjimenez\/common\/javascripts\/jquery-1.7.2\/jquery-1.7.2.js'><\/script>"
     let includes_3  = "<script type = 'text\/javascript' src = '\/Users\/johnjimenez\/common\/javascripts\/underscore\/underscore.js'><\/script>"
     let includes_4  = "<script type = 'text\/javascript' src = '\/Users\/johnjimenez\/common\/javascripts\/backbone-0.9.2\/backbone.js'><\/script>"
     let includes_5  = "<script type = 'text\/javascript' src = '\/Users\/johnjimenez\/common\/javascripts\/jasmine-1.1.0\/jasmine.js'><\/script>"
     let includes_6  = "<script type = 'text\/javascript' src = '\/Users\/johnjimenez\/common\/javascripts\/jasmine-1.1.0\/jasmine-html.js'><\/script>"
     let includes_7  = "<script>"
     let includes_8  = "  describe('default page', function(){ "
     let includes_9  = "it('should have an html tag', function(){ "
     let includes_10 = "  expect( $( 'head' ).html() ).not.toMatch(\/^[\\s\\t\\n]*$\/);"
     let includes_11  = "});"
     let includes_12 = "});"
     let includes_13 = "$(function(){"
     let includes_14 = "jasmine.getEnv().addReporter( new jasmine.TrivialReporter() );"
     let includes_15 = "jasmine.getEnv().execute();"
     let includes_16 = "});"
     let includes_17 = "\<bs>\<bs><\/script>"

     let j = 0

     while j < 18
       let entry = 'includes_' . j
       call feedkeys( {entry}, 't' )
       call feedkeys( "\<cr>", 't' )
       let j = j + 1
     endwhile

   endfunction

"jquery

  "abbreviations

     ab docr $(document).ready(function(){});
     ab jqfun $(<cr>function(){<cr>}<cr>);

正常模式:

F <char>移动到当前行上特定字符的下一个实例,并且;重复。

F<char>移动到当前行上一个特定字符的上一个实例;重复。

如果使用得当,以上两点可以让你在队列中快速移动。

*在一个单词上搜索下一个实例。

#来搜索前一个实例。