linux之vim配置

代码自动补全和代码跳转阅读,应该是作为程序员最常用的功能之一了,具体二者是指什么我就不解释了。微软的Visual Studio就是靠这两样必杀技牢牢占据着广大windows程序员的心(这里面要有强大的VS插件Visual Assistant X一份功劳)。。。但是Linux程序员其实更幸福,不花钱就能搞定这两大功能。

    从本质上说,这二者的实现都依赖于一样东西:tag。tag就是程序中的关键词,在C++中主要包括:变量、函数名、类名等。代码自动补全 实际上是 tag的匹配(例如,程序员输入cla时,由于存在class这个c++的tag,就可以用class匹配cla);代码跳转阅读实际上是tag的查找 (例如,程序员要查找一个函数func(),只需要在别的文件中寻找这个func这个tag的位置即可)。

    

     【准备】

    我现在的系统是Ubuntu Desktop 10.04 LTS版本。当然,一切工作的前提是你能上网,而且配置好了一个可用的源。

    1. 安装Vim和Vim基本插件

    我们需要首先安装好Vim和Vim的基本插件。这些使用apt-get安装即可:

    sudo apt-get install vim vim-doc vim-scripts

    其中vim-scripts是vim的一些基本插件,包括语法高亮的支持、缩进等等。

    2. Vim配置文件

    Vim强大的功能,其来源基本上就两个地方:插件,以及配置文件。

    上面已经下载了Vim的基本插件,下面说一下Vim的基本配置。Vim本身的系统配置文件夹是在/usr/share/vim/和/etc /vim /两个文件夹下,我们一般不要去改变这些,改了以后不容易恢复。我们需要在用户文件夹下建立自己的配置文件。假设用户的名字是user。进入用户文件夹 (/home/user/)之后,用gedit新建一个名叫.vimrc的文件:

    gedit .vimrc

    之所以用gedit是因为vim里面不能拷贝粘贴,为了方便大段大段的文字粘贴,还是先用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.

     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 nocompatible

    ” Vim5 and later versions support syntax highlighting. Uncommenting the

     ” following enables syntax highlighting by default.

     if has(“syntax”)

     syntax on

     endif

    ” 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(“‘\”")

     let OmniCpp_MayCompleteScope = 1 ” autocomplete with ::

     let OmniCpp_SelectFirstItem = 2 ” select first item (but don’t insert)

     let OmniCpp_NamespaceSearch = 2 ” search namespaces in this and included files

     let OmniCpp_ShowPrototypeInAbbr = 1 ” show function prototype in popup window

     let OmniCpp_GlobalScopeSearch=1

     let OmniCpp_DisplayMode=1

     let OmniCpp_DefaultNamespaces=["std"]

    (前几行就是提供了C++中的./->/::等操作符的提示和自动完成)。

    6. 自动补全功能的测试

    C++开发中经常会用到C++标准库的代码,因此STL的自动补全很重要。可以下载一份C++标准库的源代码来测试一下自动补全功能。

    sudo apt-get install build-essential

    然后在/usr/include/c++下就可以找到标准库的头文件了。在此文件夹下生成tags文件,并添加到vim的配置文件中(不再重复上面的内容),然后在编程的时候就可以使用自动补全功能了。

    下面展示了一张vector的函数补全的效果图:

    vector_auto_complete

    PS:在自动补全的点,Vim必须知道可能补全的定义。比如说,在namespace std命名空间下的变量和函数,必须要用using namespace std;暴露出来,否则是不能补全的。在.cpp文件中还可以,在.h文件中这样就不是好的做法了。暂时不知道这个问题是由于我自己配置错误还是程序没有 实现。

原文地址:https://www.cnblogs.com/chris-cp/p/3529300.html