让vim易用人性一点

来源:互联网 发布:paxos算法实现 编辑:程序博客网 时间:2024/06/04 20:03
大牛程序猿必备神器,但小白刚开始使用vim的时候,会觉得很难用,因为不会用。。。

主要是因为不会配置,没能开发出vim的潜力,所以记录下自己使用vim时的常用配置供大家查阅,方便新手入门~~

基本说明:
1) vim配置文件
etc/vim/vimrc 是所有用户的配置(sudo 修改)
~/vimrc 只影响到单个用户
2) vim插件位置
~/.vim/ (~ 就是当前用户主目录/home/username,隐藏文件和文件夹使用Ctrl+H显示,如果不存在.vim目录则创建)


1. 基本配置
1) 设置显示行号
set number 或者简写
set nu
相反取消行号:set nonumber或者set nonu


2) colorscheme 配色方案
colors,plugins,docs都是在VIMRUNTIME目录下的,你可以先在vim的命令模式下输入:echo $VIMRUNTIME(如果没有自己另行修改,一般是/usr/share/vim/vim7x)
如果你下载了新的配色方案,就可以移动到/usr/share/vim/vim7x
设置配色方案:
colorscheme COLOR_SCHEME 或者简写
color COLOR_SCHEME 或者继续简写
colo COLOR_SCHEME


3) 设置缩进
set autoindent 每行缩进值和上一行相同,取消自动缩进set noautoindent
set cindent 设置使用C/C++语言的自动缩进


4) 其他设置
set tabstop=4 制表符宽度为4
set shiftwidth=4 缩进空格数为4
set mouse=a 在所有模式下都允许鼠标使用(主要方便在浏览代码文件及函数时鼠标选取)


给个链接(http://vimcolorschemetest.googlecode.com/svn/html/index-c.html),里面给出了各种配色方案的效果,如果感兴趣的可以去下载相应的配色方案。


2. 插件配置
1) 定位符号插件 Ctags
a) http://ctags.sourceforge.net/ 下载源码包,解压得到源码目录(tar -zxvf ctags-5.8.tar.gz)
b) cd 源码目录, ./configure, make, sudo make install(另外,实际你也可以sudo apt-get install ctags直接安装)
c) vimrc 修改配置
map <C-F12> :!ctags -R --c++-kinds=+p --fields=+iaS --extra=+q .<CR>
在vim中配置了Ctrl-F12组合快捷键,所以我们也可以进入代码根目录后,打开Vim,按下Ctrl-F12快捷键自动生成tags文件。或者直接打开终端cd到代码根目录,ctags -R生成相应的tags文件


命令执行完后,会在源代码目录生成tags文件。Vim默认会自动读取当前目录下的tags文件,所以不需要修改~/.vimrc文件。此时,我们已经具有定义跳转的功能了。有两组快捷键是最常用的。
Ctrl-]    跳转到光标所在符号的定义。
Ctrl-t    回到上次跳转前的位置。
更多功能通过命令man ctags或在Vim命令行下运行help ctags查询。

实际上对于tags文件,我一般在编写代码的目录生成一个tags文件,再在.vimrc中配置tags路径
set tags+=/home/hl/Coding/tags
后面提到的补全插件都是依赖Ctags生成的tags文件


2) 浏览源码插件 TagList
a) http://www.vim.org/scripts/script.php?script_id=273 下载安装包,解压到~/.vim/目录下,会得到新的子目录(如plugin和doc)
b) 进入~/.vim/doc目录,在Vim下运行"helptags ."命令。此步骤是将doc下的帮助文档加入到Vim的帮助主题中,这样我们就可以通过在Vim中运行“help taglist.txt”查看taglist帮助。后面不再赘述。
c) vimrc 修改配置
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1

在Vim命令行下运行TlistToggle命令就可以打开Taglist窗口,再次运行TlistToggle则关闭
我们可以通过Ctrl-w快捷键或鼠标点击在Taglist窗口和编辑区之间切换焦点,在Taglist窗口用鼠标或键盘选择某个符号,然后点击或回车,就可以跳转到该符号定义的位置。
更多功能可通过在Vim命令行下运行help taglist.txt查询。

3) Tab补全插件 SuperTab
a) http://www.vim.org/scripts/script.php?script_id=1643 下载supertag.vmb文件(vim插件)
b) 使用vim打开该文件,最下面有提示在vim的命令行输入: so %, 安装插件到~/.vim/目录下
现在重新打开vim,感受下Tab补全的感觉,越来越人性化了。


4) 类结构体属性方法提示补全插件 OmniCppComplete
a) http://www.vim.org/scripts/script.php?script_id=1520 下载压缩包
b) 解压到~/.vim/目录下
c) vimrc 修改配置
set nocp
filetype plugin on


值得注意的是目前插件还不能支持STL的补全,因为没用相应的tags文件,下面是相关解决办法:

http://www.sgi.com/tech/stl/download.html 下载stl.tar压缩包
先解压到stl目录下,先不急着直接生成tags文件,里面的内容需要稍微替换一下,先写一个小脚本:

#!/bin/bashmkdir stl2mkdir stl3cd stlfor i in *do    sed 's/__STL_BEGIN_NAMESPACE/namespace std{/' $i > ../stl2/$idonecd ../stl2for i in *do    sed 's/__STL_END_NAMESPACE/}/' $i > ../stl3/$idonecd ../stl3ctags -R --c++-kinds=+p --fields=+iaS --extra=+q -f ../stltagscd ..rm -rf stl2rm -rf stl3
此时已经生成了tags文件stltags,再将stltags移动到了~/.vim/tags/目录下(个人习惯),最后添加到vim的tags路径中。
set tags+=/home/hl/.vim/tags/stltags
重新打开vim,编写cpp代码,已经能够支持STL补全。

5) 文件浏览器窗口管理插件 WinManager
a) http://www.vim.org/scripts/script.php?script_id=95 下载压缩包
b) 解压到~/.vim/doc/和~/.vim/plugin/目录下
c) vimrc 修改配置
let g:AutoOpenWinManager=1
nmap wm :WMToggle<CR>
在vim中配置快捷键w+m,开启目录文件浏览的WMToggle快捷键。当然你可以重启vim,感受下,已经有点VS的味道了,但不太美观。

6) 树形浏览文件插件 NERDTree
a) http://www.vim.org/scripts/script.php?script_id=1658 下载压缩包
b) 解压到~/.vim/目录下

c) vimrc 修改配置

let g:miniBufExplMapWindowNavVim = 1   let g:miniBufExplMapWindowNavArrows = 1   let g:miniBufExplMapCTabSwitchBufs = 1   let g:miniBufExplModSelTarget = 1  let g:miniBufExplMoreThanOne=0


7) 浏览文件插件 MiniBufExplorer
a) http://www.vim.org/scripts/script.php?script_id=159 下载得到minibufexpl.vim文件,直接放入~/.vim/plugin/目录下
b) vimrc 修改配置
let g:NERDTree_title="[NERDTree]"
let g:winManagerWindowLayout="NERDTree|TagList" 使用Winmanager整合NERDTree界面和Taglist界面

如果你迫不及待的想感受下效果,可能你已经发现每次使用w+m打开Winmanager界面时会同时打开一个空文件。这是这个版本Winmanager的一个小bug,所以我们要在打开Winmanager时关掉这个空文件。
在~/.vim/plugin目录下修改winmanager.vim文件,找到该函数(命令行下输入: ?ToggleWindowsManager 查找)增加第6行代码(exe 'q'):

function! <SID>ToggleWindowsManager()if IsWinManagerVisible()call s:CloseWindowsManager()elsecall s:StartWindowsManager()exe 'q'endendfunction

现在再来体验效果,已经解决空文件问题^_^
 

不知不觉就写了这么长一篇了。。。给个最后的效果图




最后贴上vimrc:

" All system-wide defaults are set in $VIMRUNTIME/debian.vim and sourced by" the call to :runtime you can find below.  If you wish to change any of those" settings, you should do it in this file (/etc/vim/vimrc), since debian.vim" will be overwritten everytime an upgrade of the vim packages is performed." It is recommended to make changes after sourcing debian.vim since it alters" the value of the 'compatible' option." This line should not be removed as it ensures that various options are" properly set to work with the Vim-related packages available in Debian.runtime! debian.vim" Uncomment the next line to make Vim more Vi-compatible" NOTE: debian.vim sets 'nocompatible'.  Setting 'compatible' changes numerous" options, so any other options should be set AFTER setting 'compatible'."set compatible" Vim5 and later versions support syntax highlighting. Uncommenting the next" line enables syntax highlighting by default.if has("syntax")  syntax onendif" If using a dark background within the editing area and syntax highlighting" turn on this option as well"set background=dark" Uncomment the following to have Vim jump to the last position when" reopening a file"if has("autocmd")"  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif"endif" Uncomment the following to have Vim load indentation rules and plugins" according to the detected filetype."if has("autocmd")"  filetype plugin indent on"endif" The following are commented out as they cause vim to behave a lot" differently from regular Vi. They are highly recommended though."set showcmd" Show (partial) command in status line.set showmatch" Show matching brackets."set ignorecase" Do case insensitive matching"set smartcase" Do smart case matching"set incsearch" Incremental search"set autowrite" Automatically save before commands like :next and :make"set hidden" Hide buffers when they are abandonedset mouse=a" Enable mouse usage (all modes)" Source a global configuration file if availableif filereadable("/etc/vim/vimrc.local")  source /etc/vim/vimrc.localendifset nucolo eveningset cindentset tabstop=4set shiftwidth=4" Configure tags - add additional tags here or comment out not-used ones" load user definition tagsset tags+=/home/hl/Coding/tags" Configure omnicppcompleteset nocpfiletype plugin on" Configure TagListlet Tlist_Show_One_File=1let Tlist_Exit_OnlyWindow=1" Configure winmanagernmap wm :WMToggle<CR>let g:winManagerWindowLayout="NERDTree|TagList"" Configure MiniBufExplorerlet g:miniBufExplMapWindowNavVim = 1   let g:miniBufExplMapWindowNavArrows = 1   let g:miniBufExplMapCTabSwitchBufs = 1   let g:miniBufExplModSelTarget = 1  let g:miniBufExplMoreThanOne=0" Configure NERDTreelet g:NERDTree_title="[NERDTree]" function! NERDTree_Start()      exec 'NERDTree'  endfunction    function! NERDTree_IsValid()      return 1  endfunction


给vim安装完上述插件,易用人性不止增加了一点点,在vim下写代码已经比较顺手,已经有了VS的既视感,还有Linux下vim的高端感(可能刚开始还不太顺手),暂且vim就配置到这地方,后续有觉得好的再继续更新。


最后的最后,把上面提到的插件全部打了个包,方便以后大家下载:
http://download.csdn.net/download/mrknight/7155745


vim学习链接:
http://coolshell.cn/articles/5426.html

参考链接:
http://blog.csdn.net/namecyf/article/details/7787479
http://blog.csdn.net/bokee/article/details/6633193



0 0
原创粉丝点击