配置 vim

来源:互联网 发布:linux删除整个目录 编辑:程序博客网 时间:2024/05/21 03:56
2011-10-21 21:57 by longcpp, 1407 阅读, 0评论, 收藏,编辑

一:配置 vim 的颜色方案

配置颜色方案,下载插件ColorSamplerPack.zip,将其解压缩到 .vim 文件夹下,如果没有该文件夹,则自己创建一个。在 .vimrc 文件中添加下面一行配置:

colorscheme fruity

其中 fruity 是一个颜色主题的名字,当然也可以选用别的主题(笔者在尝试多个主题后选择了 fruity ),ColorSamplerPack.zip 的页面上有链接指向各个主题的预览。

但是,这里的许多主题只适用于 iGvim(包括fruity在内),所以需要另外的插件来允许这些颜色主题工作在终端里。需要的插件是 CSApprox.zip,下载该插件,将其解压到 .vim 中,更新 vim 的帮助文件(也就是将关于该插件的帮助内容添加到 vim 的帮助文件中,命令如下:

vim -c 'helptags ~/.vim/doc'

完成之后,启动 vim 出现如下错误信息: “CSApprox needs gui support - not loading. See :help |csapprox-+gui| for possible workarounds.” 。根据提示,查阅帮助文件的相关章节,找到如下信息:

If CSApprox is being used to adjust a scheme's colors transparently, then the terminal "vim" binary that is being run must be built with GUI support (see |csapprox-limitations| for an explanation).
Unfortunately, several Linux distributions only include GUI support in their "gvim" binary, and not in their "vim" binary. You can check if GUI support is available with the following command:
:echo has('gui')
If that prints 0, the first thing to try would be searching for a larger vim package provided by your distribution, like "vim-enhanced" on RedHat/CentOS or "vim-gtk" or "vim-gnome" on Debian/Ubuntu.

根据提示,在笔者的系统(ubuntu 11.10)需要安装 vim-gtk 或者 vim-gnome 软件包,输入如下命令进行安装:

sudo apt-get install vim-gtk

安装完成之后,再次执行 vim 又出现如下错误信息:“CSApprox skipped; terminal only has 8 colors, not 88/256. Try checking :help csapprox-terminal for workarounds”。根据提示,查阅帮助文件相关章节,找到如下信息:

many modern terminals support 88 or 256 colors, but most of these default to setting $TERM to something generic (usually "xterm"). Since Vim uses the value of the "colors" attribute for the current $TERM in terminfo to figure out the number of colors used internally as 't_Co', this plugin will either need for 't_Co' to be set to 88 or 256 in |vimrc|, or for $TERM to be set to something that implies high color support. Possible choices include "xterm-256color" for 256 color support and "rxvt-unicode" for 88 color support.

根据上述提示,可以有两种解决方案,一是在 .vimrc 文件中将 t_Co 设置成 88 或者 256,另外一种是将 $TERM 设置成别的值来支持更多的颜色。这里,笔者采用第一种方法,在 .vimrc 文件中添加下面一行配置:

set t_Co=256

再次运行 vim ,没有输出错误信息,vim 的颜色主题也变成我们所设置的。

二:安装 cvim 插件

下载插件 cvim.zip,将其解压缩到 .vim 文件夹下面,然后用上面提到的命令将 cvim 的帮助文件添加到 vim 的帮助文件中。 如果要读取该帮助文件,在 vim 中执行下面命令

:h csupport

以熟悉插件功能,并验证 cvim 的帮助文档已经导入。值得一提的是,在 cvim 插件的主页上有 cvim 快捷键的 pdf 文档,可以作为参考。

cvim 插件会在我们利用 vim 新建 .c 或者 .cpp为后缀的文件时,自动在文件开始出添加一些信息,这些信息可以在 ~/.vim/c-support/templates 文件夹中的 各个文件中进行修改,在 Template 文件中,输入自己的名字,邮箱等信息,保存。再利用 vim 来创建 .c 或者 .cpp 的文件时,这些信息就会被自动添加到新文件的开始处。

---------------------------------------------------------------------------------------------

2012-06-06 添加

在使用cvim的时候,一直没有注意到一个问题,装上cvim插件后,无论是创建 *.c 还是 *.cpp 文件,文件头部分总是一样,如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
* ===========================================================================
*
* Filename: test.c
*
* Description:
*
* Version: 1.0
* Created: 06/06/2012 11:09:29 AM
* Revision: none
* Compiler: gcc
*
* Author: Long Wen (longcpp), longcpp9@gmail.com
* Organization: SDU
*
* ===========================================================================
*/

理想的情况应该是对 *.cpp文件,其中Compiler部分,对应的应该是 g++。相同的问题还出现在,利用cvim的快捷键插入代码时。我将c.idioms.template中main主函数部分修改如下(对默认的格式不大习惯,大家也可以修改别的部分):

?
1
2
3
4
5
6
7
8
== idioms.main ==
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[])
{<CURSOR>
<SPLIT> returnEXIT_SUCCESS;
}/* ---------- end of function main ---------- */

将cpp.idioms.template中main主函数部分修改如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
== idioms.main ==
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
usingnamespace std;
int main(int argc,char *argv[])
{<CURSOR>
<SPLIT> returnEXIT_SUCCESS;
}// ---------- end of function main ----------

理想的情况,就应该是编辑 *.c 文件时和 *.cpp 文件时,\im 插入代码应该是不同的,而事实是不管是什么文件,插入的总是c.idioms.template中的代码。

解决的问题的关键,就在Templates($HOME/.vim/c-support/tempaltes)中,Templates中有如下语句:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ =============================================================
$ ========== USER MACROS ======================================
$ =============================================================
$
|AUTHOR| = Long Wen
|AUTHORREF| = longcpp
|EMAIL| = longcpp9@gmail.com
|COMPANY| = SDU
|COPYRIGHT| = Copyright (c) |YEAR|, |AUTHOR|
|STYLE| = default
$
$ =============================================================
$ ========== FILE INCLUDES ====================================
$ =============================================================
$
|includefile| = c.comments.template
|includefile| = c.cpp.template
|includefile| = c.idioms.template
|includefile| = c.preprocessor.template
|includefile| = c.statements.template
$
== IF |STYLE| IS CPP ==
|includefile| = cpp.comments.template
|includefile| = cpp.cpp.template
|includefile| = cpp.idioms.template
|includefile| = cpp.preprocessor.template
|includefile| = cpp.statements.template
== ENDIF ==
$

查阅帮助文档就知道了,cvim根据STYLE的值来有选择性包含不同的模板(通过IF,IS,ENDIF关键词),而默认情况下,STYLE的值为default,此时,仅仅包含不在IF和ENDIF之间的文档。这样的话,上述问题就明白了,因为不论我们编辑什么文件,包含的永远都是 c.comments.template 等为c代码预置的模板。可以简单的将STYLE的值改为CPP,但是这样的话,又仅包含 cpp.comments.template 等为cpp代码预置的模板。所以,我们真正需要的是,能够根据所编辑文件的扩展名,来动态指定STYLE。

幸运的是,在帮助文档中,找到了解决方法,就是在 .vimrc 文件中添加如下配置语句:

let g:C_Styles = { '*.c,*.h' : 'default', '*.cc,*.cpp,*.hh' : 'CPP' }

具体原因的解释如下:(csupport帮助文档)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
==============================================================================
5.5 BINDING A STYLE TO AFILE EXTENSION *csupport-templates-bind*
==============================================================================
You can bind the existing styles to one or more filename extensions. Todo so
assign a Dictionary to the global variable g:C_Styles in'~/.vimrc' :
let g:C_Styles = { '*.c,*.h' : 'default','*.cc,*.cpp,*.hh' : 'CPP' }
A Dictionary is created with a comma separated list of entries in curly
braces. Each entry has a key and a value, separated by a colon. Each key can
only appear once. The keys are themselves a comma separated list of filename
pattern. The values are existing styles defined in thetemplate files.
The given style will be set automatically when switching to a buffer or
opening a new buffer with the associated filename pattern and supersedes the
macro *|STYLE|* .

接下来就是修改Templates($HOME/.vim/c-support/tempaltes),修改后的部分内容如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|COPYRIGHT| = Copyright (c) |YEAR|, |AUTHOR|
|STYLE| = CPP
$
$ =============================================================
$ ========== FILE INCLUDES ====================================
$ =============================================================
$
== IF |STYLE| IS C ==
|includefile| = c.comments.template
|includefile| = c.cpp.template
|includefile| = c.idioms.template
|includefile| = c.preprocessor.template
|includefile| = c.statements.template
== ENDIF ==
$
== IF |STYLE| IS CPP ==
|includefile| = cpp.comments.template
|includefile| = cpp.cpp.template
|includefile| = cpp.idioms.template
|includefile| = cpp.preprocessor.template
|includefile| = cpp.statements.template
== ENDIF ==
$

因为我经常写的是CPP代码,所以将STYLE的初值设定为CPP

2012-06-06添加结束

---------------------------------------------------------------------------------------------

三:自定义状态行

set laststatus=2 "总是显示状态行,状态栏占两行
"自定义状态行
set statusline=\%F%m%r%h%w[%L]\ [%{&ff}]\ [TYPE=%Y]\ [POS=%04l,%04v][%p%%]\ %{strftime(\"%d/%m/%y\ -\ %H:%M\")}
"%F   当前文件绝对路径 %m   已修改标志
"%r   只读标志   %h    帮助文件标志
"%w   预览标志   %L    文件总行数
"%{&ff}  文件格式   %y     使用的语法高亮器
"%p%%  当前光标位置百分比 %04l    当前行数
"%04v   当前列数   strftime   为时间值

四:代码折叠

" 折叠方法
" manual 手工折叠
" indent 使用缩进表示折叠
" expr 使用表达式定义折叠
" syntax 使用语法定义折叠
" diff 对没有更改的文本进行折叠
" marker 使用标记进行折叠, 默认标记是 {{{ 和 }}}
set foldenable
set foldmethod=indent
""set foldclose=all
set foldcolumn=4 "在左侧显示折叠的层次
highlight FoldColumn guibg=#454343 guifg=Yellow "设置前景色与背景色

五:括号与引号的自动补全

"自动括号和引号补全
:inoremap ( ()<ESC>i
:inoremap ) <c-r>=ClosePair(')')<CR>
:inoremap { {}<ESC>i
:inoremap } <c-r>=ClosePair('}')<CR>
:inoremap [ []<ESC>i
:inoremap ] <c-r>=ClosePair(']')<CR>
:inoremap < <><ESC>i
:inoremap > <c-r>=ClosePair('>')<CR>
:inoremap " ""<ESC>i
:inoremap ' ''<ESC>i

function ClosePair(char)
if getline('.')[col('.') - 1] == a:char
return "\<Right>"
else
return a:char
endif
endf

六:设置备份

set backup "keep a back up file
set backupdir=$HOME/.vim.backupdir "central backup directory(has to be created)

注意,添加这条配置语句,需要在用户家目录下建立文件夹 .vim.backdir

七:安装 Taglist 插件

首先需要安装 ctags 工具,用如下命令:

apt-get install exuberant-ctags

下载 taglist 插件,然后将其解压缩的 .vim 文件夹下。更新 vim 的帮助文件。在 .vimrc 文件中添加如下配置语句(其中,以引号开头的是被注释掉的,可以根据自己偏好来设定):

"let Tlist_Auto_Open = 1 "启动vim时,自动打开taglist窗口
let Tlist_Exit_OnlyWindow = 1 "如果只有taglist窗口打开,则退出vim
"let Tlist_Use_SingleClick = 1 "通过鼠标单击在tag之间跳转(不是双击)
let Tlist_Compact_Format = 1 "紧凑的打印模式,尽可能多的输出tag
let Tlist_WinWidth = 20 "taglist窗口的默认宽度

let Tlist_Enable_Fold_Column = 0 "disable column fold

在 vim 里面,可以用命令 :TlistToggle 来打开或者关闭 taglist 窗口,用 :help taglist 可以查看相应的帮助文档

原创粉丝点击