ctags和vim的安装和使用详解

14 篇文章 0 订阅

1、安装Vim和Vim基本插件
首先安装好Vim和Vim的基本插件。这些使用apt-get安装即可:
lingd@ubuntu:~/arm$sudo apt-get install vim vim-scripts vim-doc
其中vim-scripts是vim的一些基本插件,包括语法高亮的支持、缩进等等。
vim中文帮助文档tar包下载地址:
http://sourceforge.net/projects/vimcdoc/files/vimcdoc/
解压后其中有个doc文件夹, 将其中的内容全部复制到~/.vim/doc, 或者vim安装目录下的doc目录中, 此时vim中的help信息已经是中文的了.
网页版中文帮助文档网址http://vimcdoc.sourceforge.net/doc/help.html
首页就时vim帮助文档的目录,阅读起来更方便有效、更有针对性!

2、Vim配置文件
Vim强大的功能,其来源基本上就两个地方:Vim插件以及Vim配置文件。
Vim本身的系统配置文件夹是在/usr/share/vim/和/etc/vim/两个文件夹下。一般情况下,我们不会去改变这两个文件夹下的配置文件,而是在用户文件夹/home/user(其中,user为用户名,我的用户名是lingd)下建立自己的配置文件。进入用户文件夹(/home/user/)之后,用gedit新建一个名叫.vimrc的文件:
lingd@ubuntu:~/arm cd lingd@ubuntu:  gedit .vimrc
注:使用gedit主要是为了方便大段大段的文字粘贴!
然后把下面的文字拷贝进这个文件之后保存:

” 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.
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 nocompatible

” Vim5 and later versions support syntax highlighting. Uncommenting the
” following enables syntax highlighting by default.
if has(“syntax”)
syntax on ” 语法高亮
endif
colorscheme ron ” elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下

” detect file type
filetype on
filetype plugin on

” 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
“have Vim load indentation rules and plugins according to the detected filetype
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 ignorecase ” 搜索模式里忽略大小写
“set smartcase ” 如果搜索模式包含大写字符,不使用 ‘ignorecase’ 选项。只有在输入搜索模式并且打开 ‘ignorecase’ 选项时才会使用。
set autowrite ” 自动把内容写回文件: 如果文件被修改过,在每个 :next、:rewind、:last、:first、:previous、:stop、:suspend、:tag、:!、:make、CTRL-] 和 CTRL-^命令时进行;用 :buffer、CTRL-O、CTRL-I、’{A-Z0-9} 或 `{A-Z0-9} 命令转到别的文件时亦然。
set autoindent ” 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
“set smartindent ” 智能对齐方式
set tabstop=4 ” 设置制表符(tab键)的宽度
set softtabstop=4 ” 设置软制表符的宽度
set shiftwidth=4 ” (自动) 缩进使用的4个空格
set cindent ” 使用 C/C++ 语言的自动缩进方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s “设置C/C++语言的具体缩进方式
“set backspace=2 ” 设置退格键可用
set showmatch ” 设置匹配模式,显示匹配的括号
set linebreak ” 整词换行
set whichwrap=b,s,<,>,[,] ” 光标从行首和行末时可以跳到另一行去
“set hidden ” Hide buffers when they are abandoned
set mouse=a ” Enable mouse usage (all modes) “使用鼠标
set number ” Enable line number “显示行号
“set previewwindow ” 标识预览窗口
set history=50 ” set command history to 50 “历史记录50条

“–状态行设置–
set laststatus=2 ” 总显示最后一个窗口的状态行;设为1则窗口数多于一个的时候显示最后一个窗口的状态行;0不显示最后一个窗口的状态行
set ruler ” 标尺,用于显示光标位置的行号和列号,逗号分隔。每个窗口都有自己的标尺。如果窗口有状态行,标尺在那里显示。否则,它显示在屏幕的最后一行上。

“–命令行设置–
set showcmd ” 命令行显示输入的命令
set showmode ” 命令行显示vim当前模式

“–find setting–
set incsearch ” 输入字符串就显示匹配点
set hlsearch

注:配置文件中,以单个双引号开头的文字为注释。
保存文件之后,启动Vim。此时,Vim已经是这种效果了(语法高亮挺漂亮的–这个是由vim-scripts中的插件支持的):

3、ctags安装与配置
ctags可以建立源码树的标签索引(标签就是一个标识符被定义的地方,如函数定义),使程序员在编程时能迅速定位函数、变量、宏定义等位置去查看原形
以下是在ubuntu下ctags的下载安装和配置过程:
下载并安装ctags,终端输入命令
lingd@ubuntu:~/arm sudoaptgetinstallctagsLinux/home/lingd/arm/linux2.6.24.7ctagsRtagslingd@ubuntu: /arm cd linux-2.6.24.7
lingd@ubuntu:~/arm/linux-2.6.24.7 lsarchcryptoincludekernelmmsamplesusrblockDocumentationinitlibnetscriptsCOPYINGdriversipcMAINTAINERSREADMEsecurityCREDITSfsKbuildMakefileREPORTINGBUGSsoundlingd@ubuntu: /arm/linux2.6.24.7 ctags -R *
lingd@ubuntu:~/arm/linux-2.6.24.7 lsarchcryptoincludekernelmmsamplestagsblockDocumentationinitlibnetscriptsusrCOPYINGdriversipcMAINTAINERSREADMEsecurityCREDITSfsKbuildMakefileREPORTINGBUGSsoundvimtagslingd@ubuntu: /arm/linux2.6.24.7 vi ~/.vimrc
在打开文件的最后添加如下内容(当然,具体路径根据你自己的情况)

“–ctags setting–
” 按下F5重新生成tag文件,并更新taglist
map :!ctags -R –c++-kinds=+p –fields=+iaS –extra=+q . :TlistUpdate
imap :!ctags -R –c++-kinds=+p –fields=+iaS –extra=+q . :TlistUpdate
set tags=tags
set tags+=./tags “add current directory’s generated tags file
set tags+=~/arm/linux-2.6.24.7/tags “add new tags file(刚刚生成tags的路径,在ctags -R 生成tags文件后,不要将tags移动到别的目录,否则ctrl+]时,会提示找不到源码文件)

set tags+=./tags表示在当前工作目录下搜索tags文件
set tags+=~/arm/linux-2.6.24.7/tags表示在搜寻tags文件的时候,也要搜寻~/arm/linux-2.6.24.7/文件夹下的tags文件。
然后保存并退出vi。这样,你就可以用vim在任意地方查看有关Linux的函数原形
------------------------------------
tag命令用法:
Ctrl+] 跳到当前光标下单词的标签
Ctrl+O 返回上一个标签
Ctrl+T 返回上一个标签
:tag TagName 跳到TagName标签
以上命令是在当前窗口显示标签,当前窗口的文件替代为包标签的文件,当前窗口光标跳到标签位置。如果不希望在当前窗口显示标签,可以使用以下命令:
:stag TagName 新窗口显示TagName标签,光标跳到标签处
Ctrl+W + ] 新窗口显示当前光标下单词的标签,光标跳到标签处
当一个标签有多个匹配项时(函数 (或类中的方法) 被多次定义),”:tags” 命令会跳转到第一处。如果在当前文件中存在匹配,那它将会被首先使用。
可以用这些命令在各匹配的标签间移动:
:tfirst 到第一个匹配
:[count]tprevious 向前 [count] 个匹配
:[count]tnext 向后 [count] 个匹配
:tlast 到最后一个匹配
或者使用以下命令选择要跳转到哪一个
:tselect TagName
输入以上命令后,vim会为你展示一个选择列表。然后你可以输入要跳转到的匹配代号 (在第一列)。其它列的信息可以让你知道标签在何处被定义过。
以下命令将在预览窗口显示标签
:ptag TagName 预览窗口显示TagName标签,光标跳到标签处
Ctrl+W + } 预览窗口显示当前光标下单词的标签,光标跳到标签处
:pclose 关闭预览窗口
:pedit file.h 在预览窗口中编辑文件file.h(在编辑头文件时很有用)
:psearch atoi 查找当前文件和任何包含文件中的单词并在预览窗口中显示匹配,在使用没有标签文件的库函数时十分有用。

最简单的使用方法举例
用vi在任意目录写一个Test.c文件,内容如下:

int main(void)
{
printf(“Hello World!\n”);
return 0;
}

写好后末行模式输入w保存好(不要退出vi),按Esc回到指令模式,把光标停留在printf上
然后按 Ctrl+W + ],vi会自动跳到Linux系统函数printf()处,这时我们能查看printf()的原形
查看完了,按Ctrl+o(回到上一个标签) 就回到原来的地方

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值