linux server 配置vim编程位置

原创  如何在linux下面配置Vim+c.vim+Ctags+taglist 收藏

最近由于公司编程的需要,我发现键盘流的很多好处,因为由于没有过于繁琐的GUI 的限制,速度也就可以提升,更何况在linux下工作,把自己打造成一个键盘流还是有很多好处的。

1、 准备工作,首先可以查看一下在linux机器上的vim的版本,建议大家用新的Vim 7.2版本,据说这个版本还是比较稳定的。

如果没有安装vim7.2可以到vim 的官方网站上下载一个版本。http://www.vim.org/ 这个网站很实用,所以的和Vim相关的plugin都可以通

过这个网站搜索到。这篇文章没有专门介绍如何在linux上安装vim的知识,以后补上。

1.1) 下载安装c.vim

下载c.vim http://www.vim.org/scripts/download_script.php?src_id=9679

mkdir ~/.vim 所有的文件都要安装在这个目录下

cd ~/.vim

unzip /usr/src/cvim.zip

解压到~/.vim的目录下,如果想解压到一个专门的目录下可以加上 -d 的参数。

启用这一插件:

vim ~/.vimrc

创建一个.vimrc的文件作为你的整体vim环境的配置。

filetype plugin on

我们后面还要用到这个文件,这里只是写一句这样的脚本,让cvim可以按照C的习惯工作。

下面费点时间讲一下c.vim 的使用,本来我以为linux下面的编程工具和windows 的visual studio的功能,特别再加上一个assistant 就更不能比了,但是用过之后发现还是vim更加好用,键指如飞的感觉。

下面的内容摘录自网上的一些文章,这里我都已经试过了,相当好用。

Feature 1: Add Automatic Header to *.c file

当你新建一个扩展名.c的文件时候,自动在文件头部增加头部注释

$ vim myprogram.c
/*
* =================================================
* Filename: myprogram.c
*
* Description:
*
* Version: 1.0
* Created: 01/19/09 20:23:25
* Revision: none
* Compiler: gcc
*
* Author: Dr. Fritz Mehner (mn),
mehner@fh-swf.de
* Company: FH Südwestfalen, Iserlohn
*
* =================================================
*/


其中如果你要改变 AUTHOR 和 COMPANY的值, 要修改模板文件 ~/.vim/c-support/templates/Templates

$ vim ~/.vim/c-support/templates/Templates
|AUTHOR| = geekstuff
|AUTHORREF| = gk
|EMAIL| = subscribe@geekstuff
|COMPANY| = thegeekstuff.com


现在编辑一个新文件,看看你的作者和公司名称改变了没有。

$ vim myprogram.c
/*
* =================================================
*
* Filename: myprogram.c
*
* Description:
*
* Version: 1.0
* Created: 01/19/09 20:26:43
* Revision: none
* Compiler: gcc
*
* Author: geekstuff (gk), subscribe@geekstuff
* Company: thegeekstuff.com
*
* =================================================
*/

Feature 2: 用\if 添加c函数

键入\if 加函数名称就会在文件中自动完成函数的定义

Feature 3: 用\im 添加main函数

Feature 4: 用 \cfu 添加函数注释

Feature 5: 用\cfr添加注释框架

Feature 6: 用\p<包含头文件

Type \p< in the normal mode, which will include the text “#include <>”, and places the cursor in the < symbol in Insert mode where you can type the header file name.

Feature 7: 保存编译文件.

保存并编译文件 \rc.

运行 \rr.

Feature 8: 用\nr 插入预定义的代码片段

The plugin comes with few pre-defined code snippets that you can insert into your code. Following are the default code snippets that comes with the plugin.

$ ls ~/.vim/c-support/codesnippets
Makefile calloc_double_matrix.c main.c print_double_array.c.noindent
Makefile.multi-target.template calloc_int_matrix.c main.cc print_int_array.c.noindent

For example, if you want to create a function that will Allocate a dynamic int-matrix of size rows*columns; return a pointer, you can re-use it from the existing code snippets. Following is the content of the calloc_int_matrix.c pre-defined code snippets.

/*
* === FUNCTION ======================================================================
* Name: calloc_int_matrix
* Description: Allocate a dynamic int-matrix of size rows*columns; return a pointer.
* =====================================================================================
*/
int**
calloc_int_matrix ( int rows, int columns )
{
int i;
int **m;
m = calloc ( rows, sizeof(int*) ); /* allocate pointer array */
assert( m != NULL ); /* abort if allocation failed */
*m = calloc ( rows*columns, sizeof(int) ); /* allocate data array */
assert(*m != NULL ); /* abort if allocation failed */
for ( i=1; i
m[i] = m[i-1] + columns;
return m;
} /* ———- end of function calloc_int_matrix ———- */


如果要插入这段代码,可以键入\nr 文件名称,代码就会自动插入

1.2) 下载Ctags         

到http://ctags.sourceforge.net/下载ctags源码

ctags-5.6.tar.gz http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gz       

然后解压并且安装 

tar zxvf ctags-5.6.tar.gz

cd ctags-5.6

./configure && make && make install

1.3) 下载安装taglist

taglist 依赖于ctags 插件,所以要安装ctags 插件,然后才能安装taglist,否则不能用。

到http://vim.sourceforge.net/scripts/script.php?script_id=273
下载taglist_42.zip

这里可能有更新的版本,所以要根据文档的说明,配合vim的版本才可以安装。

解压文件:unzip -d taglist taglist_42.zip -d 表示要把文件解压到某个目录下。

解压得到两个文件。./taglist/doc/taglist.txt; ./taglist/plugin/taglist.vim

分别复制到各自的目录下:

cp ./taglist/doc/taglist.txt /usr/share/vim/vim72/doc/

cp ./taglist/plugin/taglist.vim /usr/share/vim/vim72/plugin/

但是我认为这两步复制操作不是必要的。

不过下面两步操作是必要的。

cp ./taglist/doc/taglist.txt ~/.vim/doc/

cp ./taglist/plugin/taglist.vim ~/.vim/plugin/

这两步的目的是让taglist和ctag联系起来。

至此cview 和 ctag 加上 taglist的工具基本环境已经搭好了,下面可以用这些工具生成代码的tag然后方便浏览,有了这些工具,假以时日,练习熟练,加上vim和linux shell的强大功能就可以方便的工作了。

2、使用Ctags 和 Taglist

:helptags ~/.vim/doc 生成帮助标签

:help taglist.txt 查看帮助文档

配置 ~/.vimrc文件。

filetype plugin on 
let Tlist_Ctags_Cmd = '/usr/local/bin/ctags' “设置ctags命令目录
let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的 
let Tlist_Exit_OnlyWindow =  1 "如果taglist窗口是最后一个窗口,则退出vim 
let Tlist_Use_Right_Window = 1 “让taglist窗口在右侧显示

命令与用法:

在Taglist窗口按
F1:打开帮助
回车键:跳到光标所在的标记的定义处(如将光标移到main函数,按回车键)
o:新建一个窗口,跳到标记定义处
p:预览标记定义(仍然在taglist窗口)
空格:显示标记的原型(如函数原型)
u:更新标记列表(比如源文件新增了一个函数,并在保存后,可在taglist窗口按u)
s:选择排序字段
d:删除光标所在的taglist文件(如用vi打开了两个文件f1.c,f2.c可以删除f1.c的标记)
x:放大/缩小taglist窗口
+:展开(指标记)
-:折叠
*:全部展开
=:全部折叠
[[:将光标移到前一个文件的起点
]]:将光标移到后一个文件的起点
q:退出taglist窗口
F1:关闭帮助

最方便的方法是把光标移到变量名或函数名上,然后按下"Ctrl-]"。用"Ctrl-o"退回原来的地方。
注意:运行vim的时候,必须在"tags"文件所在的目录下运行。否则,运行vim的时候还要用":set tags="命令设定"tags"文件的路径,这样vim才能找到"tags"文件。

3、附带的问题

在实际的工作中我需要linux和windows通讯,这样我自然就想到了用ftp服务器,可能是在学校下片子下多了,首先想到假设windows端ftp服务器的工具,就是Serv-U,然后配置好账号,这里的账号最好用匿名方式,且不要设密码,因为我们要递归一次下载很多文件,包括子目录的文件结构。

然后在liunx 机器上用wget ftp://192.168.1.100/dir_name/ 下载windows上的目录。最后一个斜杠是必要的,因为这可以让你的系统认为你是在递归下载一个目录。
posted @ 2010-10-27 23:34  elite_lcf  阅读(1440)  评论(0编辑  收藏  举报
知识共享许可协议
本博客采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。