世界上有两个伟大的编辑器:一个是emacs,一个是vi。它们从诞生以来,一直在Unix/Linux世界得到最广泛的支持。尽管过了几十年,在Windows平台上和跨平台上有层出不穷的后起之秀不断挑战它们的地位,比如编辑器界的Sublime Text,Ultra Editor,Notepad++,pspad,Atom,Visual Studio Code,IDE界的Eclipse, IntellJ IDEA, NetBeans等等,但是它们的地位从未动摇过。写C#的最好IDE一定是Visual Studio,写Java还是IntellJ IDEA和Eclipse更方便。但是,对于其它语言和其它任务,比起层出不穷的后来者,一定是emacs和vim的支持是最广泛的。针对一个特定的任务,可能Sublime Text和Atom只有区区几种插件选择甚至是没有,但是在vim和emacs上可以找到好多页。
这两种编辑器的学习曲线确实是相对比较陡的,但是,一旦修炼成功,这样的付出是绝对值得的。造成学习困难的主要原因,其实是功能太多太强大。这个修炼需要花不少的精力和时间,想修成上仙甚至上神是不容易的,但是,只要坚持下去,最终一定可以做到。好了,废话不多说了,我们开始修炼之旅。
修仙之前,我们先享受一下凡人的生活。我们先看看如何利用各位上仙上神做好的法器,也就是写好的脚本。
做为人类,我们先不研究上仙上神们是如何摆放脚本位置的,我们统一通过vundle来管理其余插件。
首先我们下载Vundle,通过git命令,非常简单:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim下面我们再通过修改~/.vimrc来配置一下,把下面原封不动抄到~/.vimrc就好:
set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " To ignore plugin indent changes, instead use: "filetype plugin on我们只需要在call vundle#end()那一行之前,加上我们需要的plugin的内容就可以了。下面我们开始vim plugin的寻宝之旅,第一站当然是官网!
vim的第一集散地当然是官网:http://www.vim.org/scripts/index.php
比如我写C/C++比较多,我们就先拿c.vim来试试水。官网上的插件的git库都放在这里:http://vim-scripts.org/vim/scripts.html对于官网上的插件,我们直接使用vim-scripts.org上的目录名就好,比如c.vim,taglist.vim之类的,然后修改~/.vimrc文件:
set nocompatible " be iMproved, required filetype off " required " set the runtime path to include Vundle and initialize set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() " alternatively, pass a path where Vundle should install plugins "call vundle#begin('~/some/path/here') " let Vundle manage Vundle, required Plugin 'VundleVim/Vundle.vim Plugin 'c.vim' " All of your Plugins must be added before the following line call vundle#end() " required filetype plugin indent on " required " To ignore plugin indent changes, instead use: "filetype plugin on重启vim,让新的然后我们运行:PluginInstall命令,c.vim插件就被自动安装上了。我们试验一下c.vim的强大功能吧,我们创建一个新的C++文件,比如test2.cpp,运行:
vim test2.cpp打开之后,发生了什么?c.vim插件已经按照模板自动帮我们生成的文档头:
/* * ===================================================================================== * * Filename: test2.cpp * * Description: * * Version: 1.0 * Created: 2017/06/23 12时24分15秒 * Revision: none * Compiler: gcc * * Author: YOUR NAME (), * Organization: * * ===================================================================================== */ #include <stdlib.h>比如我们要写一个for循环,试试输入i0,vim会提示输入一个变量名,我们就用i吧,于是就生成了这样的代码块:
for ( i = 0; i < n; i += 1 ) { }更多的好玩的快捷键功能,可以参考这个:https://wolfgangmehner.github.io/vim-plugins/csupport/c-hotkeys.pdf
更完整的功能,可以参考文档:https://wolfgangmehner.github.io/vim-plugins/doc/csupport.html
也可以通过:help csupport在vim中查看。
除了官网之外,github上也是vim插件的大型集散地。很多官网上的插件,在github上也有镜像。vundle对于github的支持也是很好的,比如NERD tree的地址是:https://github.com/scrooloose/nerdtree,我们可以省略github地址,直接输用户名和库名就可以了,例:
Plugin 'scrooloose/nerdtree'一般来说,官网和github上已经足够用了。但是,总是有一些特殊需求,比如公司内部使用之类的。这时候,我们只要附上git的地址就好,例:
Plugin 'git@gitlab.alibaba-inc.com:ziying.liuziying/studyvim.git'如果上面几种方式都不支持,比如是我们自己的写的插件,不想放到网上去,我们还可以指定本地目录的方式来使用插件。
例:
Plugin 'file:///home/gmarik/path/to/plugin'就是将插件名写进.vimrc,然后通过:PluginInstall命令去安装
PluginList命令:列出已经安装的插件。下面是我安装的一些插件:
" My Plugins Plugin 'VundleVim/Vundle.vim' Plugin 'd.vim' Plugin 'taglist.vim' Plugin 'scrooloose/nerdtree' Plugin 'c.vim' Plugin 'OmniCppComplete' Plugin 'clang-complete' Plugin 'AutoComplPop' Plugin 'bufexplorer.zip' Plugin 'SuperTab' Plugin 'SrcExpl' Plugin 'molokai' Plugin 'omlet.vim' Plugin 'cpp.vim' Plugin 'thinca/vim-logcat' Plugin 'vim-scripts/prop.vim' Plugin 'mglb/vim-android-syntax' PluginInstall命令:安装在.vimrc中指定的插件。如果要更新的话使用:PluginInstall! 或者:PluginUpdate命令PluginClean:删除从.vimrc中删去的plugin:PluginSearch命令可以搜索字符串,搜索到之后,就可以直接点击i进行安装。不过重启vim之后就没有了。希望每次启动后都有的话,还是写进.vimrc吧。
相关资源:敏捷开发V1.0.pptx