上一篇:
cramfs 文件系统修改方法
![](template/digiart1/images/toolbar_previous.gif)
把vim配成IDE,以及用vim阅读代码
[ 2010/09/25 10:51 | by selboo ]
From:http://www.wurmbai.info/2010/02/vim/
很就之前就像写这么一篇文章了,还是那句话,如果你是高手请无视这篇文章。
很多人习惯用codeblocks,netbeans,之类的集成开发环境编写程序,用source insight来阅读代码,但是集成开发环境很耗资源如果你的机子恰巧很鸡肋的话….另一方面我们好多人用的source insight貌似都是非正版的….这也就是我写这篇文章的原因。
还记得刚接触linux的时候用vim觉得是一特麻烦的东西(都是windows惯得),很多高手都说vim如何强大之类的,我就很纳闷…其实vim的配置选型之多,插件之繁琐,以至于完全可以把它配成支持各种语言的集成开发环境,以及代码阅读器。
一,把vim配成IDE
下面的是我的vim配置文件,别告诉我你不知道vim的配置文件在哪里…
看着那些选项的字面意思你一定能大致才出与之对应的功能了吧。
VimL语言: Codee#9164
” All s[[separator]separator]ystem-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
” /usr/share/vim/vimcurrent/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.
” 关于taglist
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
” 去掉vi一致性
set nocompatible
” 新添加的配置参数
set mouse=a
syntax on
set autowrite
set number
set showcmd
set lcs=tab:>-,trail:-
set list
set showmode
set title
set ruler
set encoding=utf-8
set fileencoding=utf-8
set nobackup
set expandtab
set shiftwidth=4
set smarttab
set fdm=indent
set fdc=4
set nowrap
set hlsearch
set incsearch
” 设置行号
set number
” 检测文件类型
filetype on
” 记录历史的行数
set history=100
” 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
” following enables syntax highlighting by default.
if has(“syntax”)
syntax on
endif
” If using a dark background within the editing area and syntax highlighting
” turn on this option as well
” 背景为黑色
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
” All s[[separator]separator]ystem-wide defaults are set in $VIMRUNTIME/debian.vim (usually just
” /usr/share/vim/vimcurrent/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.
” 关于taglist
let Tlist_Show_One_File=1
let Tlist_Exit_OnlyWindow=1
” 去掉vi一致性
set nocompatible
” 新添加的配置参数
set mouse=a
syntax on
set autowrite
set number
set showcmd
set lcs=tab:>-,trail:-
set list
set showmode
set title
set ruler
set encoding=utf-8
set fileencoding=utf-8
set nobackup
set expandtab
set shiftwidth=4
set smarttab
set fdm=indent
set fdc=4
set nowrap
set hlsearch
set incsearch
” 设置行号
set number
” 检测文件类型
filetype on
” 记录历史的行数
set history=100
” 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
” following enables syntax highlighting by default.
if has(“syntax”)
syntax on
endif
” If using a dark background within the editing area and syntax highlighting
” turn on this option as well
” 背景为黑色
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中直接用
:make
命令,然后再用
:cw
命令调出quickfix窗口就可以了。不过我觉得这么作还不如新开一个窗口专门编译来的方便。
作为一个IDE肯定的有调试的功能吧,在linux自带的调试工具gdb是一个比较古老的东西,用起来不太舒服,而且看不到源码,这里推荐用cgdb,他是基于gdb的,用法跟gdb差不多,你可以直接用
# sudo apt-get install cgdb
如果没有的话更新下源:
# 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的选项如:
let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的
let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist窗口
let Tlist_Exit_OnlyWindow = 1 "如果taglist窗口是最后一个窗口,则退出vim
let Tlist_Use_Right_Window = 1 "在右侧窗口中显示taglist窗口
在vim直接用
: Tlist
就可以调用taglist了,下面是taglist常用的一些命令(在taglist窗口直接使用):
【CR】 跳到光标下tag所定义的位置,用鼠标双击此tag功能也一样
o 在一个新打开的窗口中显示光标下tag(常用)
【Space】 显示光标下tag的原型定义(常用)
u 更新taglist窗口中的tag
s 更改排序方式,在按名字排序和按出现顺序排序间切换
x taglist窗口放大和缩小,方便查看较长的tag
+ 打开一个折叠,同zo
- 将tag折叠起来,同zc
* 打开所有的折叠,同zR
= 将所有tag折叠起来,同zM
[[ 跳到前一个文件
]] 跳到后一个文件
q 关闭taglist窗口
【F1】 显示帮助
o 在一个新打开的窗口中显示光标下tag(常用)
【Space】 显示光标下tag的原型定义(常用)
u 更新taglist窗口中的tag
s 更改排序方式,在按名字排序和按出现顺序排序间切换
x taglist窗口放大和缩小,方便查看较长的tag
+ 打开一个折叠,同zo
- 将tag折叠起来,同zc
* 打开所有的折叠,同zR
= 将所有tag折叠起来,同zM
[[ 跳到前一个文件
]] 跳到后一个文件
q 关闭taglist窗口
【F1】 显示帮助
2,ctags的安装使用
# 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
# tar zxvf ctags-5.8.tar.gz
# make
# sudo make install
使用:
找到源码的顶目录,用
# ctags -R
生成名为tags的文件。
在vim窗口浏览源码时加载与之对应的tags文件:
: set tags=[tags所在目录]/tags
ctags的使用也很简单:
Ctrl+] 跳到当前函数或者变量定义处(常用)
Ctrl+T 调回(常用)
Ctrl+T 调回(常用)
就写到这吧,事实上vim远非我写的这么肤浅,拜vim~~~,我是菜鸟,飞过~~~~
最后编辑: selboo 编辑于2012/05/01 11:14