把vim配成IDE,以及用vim阅读代码

来源:互联网 发布:淘宝 黑搜与白搜 编辑:程序博客网 时间:2024/05/16 15:40

很久之前就像写这么一篇文章了,还是那句话,如果你是高手请无视这篇文章。
很多人习惯用codeblocks,netbeans,之类的集成开发环境编写程序,用source insight来阅读代码,但是集成开发环境很耗资源如果你的机子恰巧很鸡肋的话….另一方面我们好多人用的source insight貌似都是非正版的….这也就是我写这篇文章的原因。
还记得刚接触linux的时候用vim觉得是一特麻烦的东西(都是windows惯得),很多高手都说vim如何强大之类的,我就很纳闷…其实vim的配置选型之多,插件之繁琐,以至于完全可以把它配成支持各种语言的集成开发环境,以及代码阅读器。

一,把vim配成IDE
下面的是我的vim配置文件,别告诉我你不知道vim的配置文件在哪里…
看着那些选项的字面意思你一定能大致猜出与之对应的功能了吧。

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"taglist配置
"let Tlist_Show_One_File=1

let Tlist_Exit_OnlyWindow=1
set mouse=a
set showcmd
set lcs=tab:>-,trail:-
set list
set showmode
set title
set tabstop=4
set smartindent
set expandtab
set shiftwidth=4
set smarttab
set fdm=indent
set fdc=4
set nowrap
set hlsearch
"编码设置

set encoding=utf-8
set langmenu=zh_CN.UTF-8
language message zh_CN.UTF-8
let &termencoding=&encoding
"设置解码顺序,解决解码中文乱码问题

set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1
" 去掉vi一致性

set nocompatible
" 设置行号

set number
" 检测文件类型

filetype on
" 记录历史的行数

set history=100

runtime! debian.vim

if has("syntax")
  syntax on
endif

" 背景为黑色

set background=dark
" 自动对齐

set autoindent
" 智能自动对齐

set smartindent
" 缩进

set tabstop=4
set shiftwidth=4
" 匹配模式

set showmatch
" 去除vim的GUI版本的toolbar

set guioptions-=T
" 显示光标所在位置

set ruler
" 快速找到答案

set incsearch
" 自动补全

" inoremap ( ()

" inoremap { {}

" inoremap "
" inoremap < <>


if &term=="xterm"
  set t_Co=8
  set t_Sb=^[[4%dm
  set t_Sf=^[[3%dm
endif

" 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 abandoned
"set mouse=a        " Enable mouse usage (all modes)

" Source a global configuration file if available

if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

上面的配置已经让vim具有一般IDE的自动缩进,自动合并,高亮显示,行号显示,自动补全(我注释掉了,感觉用起来不习惯)的功能,现在让我们看看如何在vim如何编译,很简单,为你的程序写一个makefile,在vim中直接用

1
:make

命令,然后再用

1
:cw

命令调出quickfix窗口就可以了。不过我觉得这么作还不如新开一个窗口专门编译来的方便。
作为一个IDE肯定的有调试的功能吧,在linux自带的调试工具gdb是一个比较古老的东西,用起来不太舒服,而且看不到源码,这里推荐用cgdb,他是基于gdb的,用法跟gdb差不多,你可以直接用

1
sudo apt-get install cgdb

如果没有的话更新下源:

1
sudo apt-get update

它的使用和gdb差不多,具体的可以去查相关资料。


二,用vim作代码阅读器
下面来介绍如何把vim配成代码阅读器,要实现这个功能我们至少需要两个插件:taglist,ctags。
1. taglist的安装使用
在http://www.vim.org/scripts/script.php?script_id=273下载最新的taglist版本,解压后插件和说明doc文档分别复制到你的vim安装目录下的plugin和doc目录下,在vim的配置文件种添加有关taglist的选项如:

1
2
3
let Tlist_Show_One_File = 1            "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1          "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1         "在右侧窗口中显示taglist窗口

在vim直接用

1
: Tlist

就可以调用taglist了,下面是taglist常用的一些命令(在taglist窗口直接使用):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
【CR】          跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样
o             在一个新打开的窗口中显示光标下tag(常用)
【Space】       显示光标下tag的原型定义(常用)
u             更新taglist窗口中的tag
s             更改排序方式,在按名字排序和按出现顺序排序间切换
x             taglist窗口放大和缩小,方便查看较长的tag
+             打开一个折叠,同zo
-             将tag折叠起来,同zc
*             打开所有的折叠,同zR
=             将所有tag折叠起来,同zM
[[            跳到前一个文件
]]            跳到后一个文件
q             关闭taglist窗口
【F1】         显示帮助

2,ctags的安装使用

1
2
3
4
 wget http://sourceforge.net/projects/ctags/files/ctags/5.8/ctags-5.8.tar.gz/download
 tar zxvf ctags-5.8.tar.gz
 make
 sudo make install

使用:
找到源码的顶目录,用

1
ctags -R

生成名为tags的文件。
在vim窗口浏览源码时加载与之对应的tags文件:

1
: set tags=[tags所在目录]/tags

ctags的使用也很简单:

1
2
Ctrl+]      跳到当前函数或者变量定义处(常用)
Ctrl+T     调回(常用)

说到这里只不过是VIM基础中的基础,本篇文章只不过是抛砖引玉,同时希望更多的朋友加入到vimer的行列,深入了解vim这款“编辑器之神”,让你的代码编写更加高效。其他的一些诸如快捷操作方式、多窗口编辑模式、VIM脚本的编写都没有罗列出来,如果想真正的了解VIM请点击:VIM中文手册