用于Linux内核开发的Vim配置 [英] Vim configuration for Linux kernel development

查看:115
本文介绍了用于Linux内核开发的Vim配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

内核开发实际上不同于传统的C项目开发(在我看来,作为新手).因此,我总是想知道内核黑客的vim配置是什么.

最重要的是如何在vim中导航内核源代码树.我尝试了ctags,但是,它的工作非常糟糕.

有人可以给我一个线索吗?

解决方案

接下来是Linux内核和常规C项目之间的主要区别(从开发人员的角度来看):

  • 内核是一个很大的项目(因此您应该选择要索引的代码)
  • 它具有依赖于体系结构的代码(并且您一次只对一种特定的体系结构感兴趣;不应为其他体系结构建立索引)
  • 它具有非常具体的编码风格,您应该坚持使用到(并且应该将vim配置为相应地显示代码)
  • 它不使用C标准库,而是具有自己的类似例程(因此,索引工具不应索引libc标头)

安装索引工具

要导航内核代码,我建议使用cscopectags工具.要安装它们,请运行以下命令:

$ sudo aptitude install cscope exuberant-ctags

一些解释:

  • cscope:将用于浏览代码(在功能等之间切换).可以跳转到符号定义,查找所有符号用法等.
  • ctags:Tagbar插件(将进一步讨论)和Omni completion(vim中的自动完成机制)所需;也可以用于导航. ctags不像cscope那样是C代码导航的理想选择,因为ctags仅能跳转到符号定义(而不是其调用者).

创建索引数据库

现在,您应该索引内核源文件.这里有2种方法:手动创建索引或在内核中使用可用的脚本.如果您不确定哪种方法最适合您,我建议您使用内核脚本,因为它会在幕后进行许多巧妙的操作(例如忽略非生成的源代码并将头文件移到结果列表的顶部).

但是首先,请为您的体系结构/主板配置和构建内核,因为生成的文件可在以后用于改善索引编制过程.

scripts/tags.sh

编制索引

内核具有用于创建内核索引数据库的相当不错的脚本(scripts/tags.sh).应该使用make cscopemake tags规则创建索引,而不是直接运行该脚本.

示例:

$ make O=. ARCH=arm SUBARCH=omap2 COMPILED_SOURCE=1 cscope tags

其中

  • O=.-使用绝对路径(如果要在内核目录之外加载创建的cscope/ctags索引文件,例如用于开发树外内核模块,则很有用).如果您想使用相对路径(即仅在内核目录中进行开发),只需忽略该参数
  • ARCH=...-选择要建立索引的CPU体系结构.请参阅arch/下的目录以供参考.例如,如果ARCH=arm,则arch/arm/目录将被索引,其余arch/*目录将被忽略
  • SUBARCH=...-选择要索引的子架构(即与主板相关的文件).例如,如果SUBARCH=omap2,仅arch/arm/mach-omap2/arch/arm/plat-omap/目录将被索引,则其余计算机和平台将被忽略.
  • COMPILED_SOURCE=1-仅索引已编译的文件.通常,您仅对构建中使用的源文件(因此已编译)感兴趣.如果您还想索引未生成的文件,只需忽略此选项.
  • cscope-创建cscope索引的规则
  • tags-使ctags索引的规则

手动编制索引

内核脚本(tags.sh)可能无法正常工作,或者您可能希望对索引编制过程有更多控制.在这种情况下,您应该手动为内核源建立索引.

有关手动索引编制的信息来自此处.

首先,您需要创建cscope.files文件,该文件将列出您要索引的所有文件.例如,我正在使用下一个命令来列出ARM体系结构(arch/arm),特别是OMAP平台(不包括其他平台,以便于导航)的文件:

 find    $dir                                          \
        -path "$dir/arch*"               -prune -o    \
        -path "$dir/tmp*"                -prune -o    \
        -path "$dir/Documentation*"      -prune -o    \
        -path "$dir/scripts*"            -prune -o    \
        -path "$dir/tools*"              -prune -o    \
        -path "$dir/include/config*"     -prune -o    \
        -path "$dir/usr/include*"        -prune -o    \
        -type f                                       \
        -not -name '*.mod.c'                          \
        -name "*.[chsS]" -print > cscope.files
find    $dir/arch/arm                                 \
        -path "$dir/arch/arm/mach-*"     -prune -o    \
        -path "$dir/arch/arm/plat-*"     -prune -o    \
        -path "$dir/arch/arm/configs"    -prune -o    \
        -path "$dir/arch/arm/kvm"        -prune -o    \
        -path "$dir/arch/arm/xen"        -prune -o    \
        -type f                                       \
        -not -name '*.mod.c'                          \
        -name "*.[chsS]" -print >> cscope.files
find    $dir/arch/arm/mach-omap2/                     \
        $dir/arch/arm/plat-omap/                      \
        -type f                                       \
        -not -name '*.mod.c'                          \
        -name "*.[chsS]" -print >> cscope.files
 

对于x86架构(arch/x86),您可以使用类似以下的内容:

 find    $dir                                          \
        -path "$dir/arch*"               -prune -o    \
        -path "$dir/tmp*"                -prune -o    \
        -path "$dir/Documentation*"      -prune -o    \
        -path "$dir/scripts*"            -prune -o    \
        -path "$dir/tools*"              -prune -o    \
        -path "$dir/include/config*"     -prune -o    \
        -path "$dir/usr/include*"        -prune -o    \
        -type f                                       \
        -not -name '*.mod.c'                          \
        -name "*.[chsS]" -print > cscope.files
find    $dir/arch/x86                                 \
        -path "$dir/arch/x86/configs"    -prune -o    \
        -path "$dir/arch/x86/kvm"        -prune -o    \
        -path "$dir/arch/x86/lguest"     -prune -o    \
        -path "$dir/arch/x86/xen"        -prune -o    \
        -type f                                       \
        -not -name '*.mod.c'                          \
        -name "*.[chsS]" -print >> cscope.files
 

其中dir变量可以具有以下值之一:

  • .:如果您仅在内核源代码目录中工作;在这种情况下,这些命令应从内核源代码的根目录运行
  • 内核源代码目录的绝对路径:如果您要开发一些树外内核模块;在这种情况下,脚本可以在任何地方运行

我正在使用第一个选项(dir=.),因为我没有开发任何树外模块.

现在,当cscope.files文件准备就绪时,我们需要运行实际的索引编制:

$ cscope -b -q -k

其中的-k参数告诉cscope不要索引C标准库(因为内核不使用它).

现在是时候创建ctags索引数据库了.为了加速这一阶段,我们将重用已经创建的cscope.files:

$ ctags -L cscope.files

可以建立

Ok,cscopectags索引数据库,并且您可以删除cscope.files文件,因为我们不再需要它:

$ rm -f cscope.files

下一个文件包含索引数据库(用于cscopectags):

- cscope.in.out
- cscope.out
- cscope.po.out
- tags

将它们保留在内核源目录的根目录中.

vim插件

注意:进一步,我展示了如何使用 pathogen 处理Vim插件.但是,既然Vim 8已发布,则可以出于相同的目的使用本地包加载. >

接下来,我们将为Vim安装一些插件.为了更好地掌握它,我建议您使用 pathogen 插件.它允许您仅将git clone vim插件添加到~/.vim/bundle/并将其隔离,而不是将~/.vim目录中不同插件的文件混合在一起.

此处中所述安装 pathogen .

>

别忘了做下一件事情(如同一链接所述):

将此添加到您的vimrc:

execute pathogen#infect()

如果您是Vim的新手,并且缺少vimrcvim ~/.vimrc,请粘贴以下超级示例:

execute pathogen#infect()
syntax on
filetype plugin indent on

为vim安装cscope映射

Vim已经具有cscope支持(请参见:help cscope).您可以使用:cs f g kfree之类的命令跳转到符号或文件.虽然不是那么方便.为了加快操作速度,您可以改用快捷键(这样您就可以将光标置于某个功能上,按某些组合键并跳至该功能).为了为cscope添加快捷方式,您需要获取cscope_maps.vim文件.

要使用 pathogen 安装它,您只需克隆回购到您的~/.vim/bundle:

$ git clone https://github.com/joe-skb7/cscope-maps.git ~/.vim/bundle/cscope-maps

现在,您应该可以使用快捷方式在vim中的功能和文件之间导航.打开一些内核源文件,将键盘光标放在某个函数调用上,然后按 Ctrl + \ ,然后按 g .它应该带您到函数实现.或者它可以显示所有可用的函数实现,然后您可以选择使用哪个实现:.

有关其余关键映射的信息,请参见 cscope_maps.vim 文件.

您还可以在vim中使用以下命令:

:cs f g kmalloc

有关详细信息,请参见:help cscope.

标签注释

ctags仍可用于导航,例如,在查找某些#define声明时.您可以将光标放在该定义用法上,然后按 g ,然后按 Ctrl + ] .有关详细信息,请参见此答案.

cscope note

下一个技巧可用于在内核中找到结构声明:

:cs f t struct device {

请注意,以上命令依赖于特定的结构声明样式(在内核中使用),因此我们知道结构声明始终具有以下形式:struct some_stuct {.此技巧可能不适用于具有另一种编码样式的项目.

树外模块开发说明

如果要开发树外模块,则可能需要从内核目录加载cscopectags数据库.可以通过vim中的下一个命令来完成(在命令模式下).

加载外部cscope数据库:

:cs add /path/to/your/kernel/cscope.out

加载外部ctags数据库:

:set tags=/path/to/your/kernel/tags

vimrc

还需要对~/.vimrc进行一些修改,以便更好地支持内核开发.

首先,让我们用垂直行突出显示第81列(因为内核编码要求您将行的长度保持在80个字符以内):

" 80 characters line
set colorcolumn=81
"execute "set colorcolumn=" . join(range(81,335), ',')
highlight ColorColumn ctermbg=Black ctermfg=DarkRed

如果要同时突出显示80列以上的列,请取消注释第二行.

内核编码样式禁止尾随空格,因此您可能需要突出显示它们:

" Highlight trailing spaces
" http://vim.wikia.com/wiki/Highlight_unwanted_spaces
highlight ExtraWhitespace ctermbg=red guibg=red
match ExtraWhitespace /\s\+$/
autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
autocmd InsertLeave * match ExtraWhitespace /\s\+$/
autocmd BufWinLeave * call clearmatches()

内核编码风格

要使vim尊重内核编码样式,您可以准备使用插件: NERDTree

  • 标签栏
  • 文件行
  • vim-airline
  • 这些也是有趣的插件,但是您可能需要为内核配置它们:

    全方位完成

    Vim 7(及更高版本)已经内置了自动完成支持.它调用Omni completion.有关详细信息,请参见:help new-omni-completion . /p>

    在内核这样的大型项目上,Omni的完成工作相当缓慢.如果仍然需要,可以启用它,在~/.vimrc中添加下一行:

    " Enable OmniCompletion
    " http://vim.wikia.com/wiki/Omni_completion
    filetype plugin on
    set omnifunc=syntaxcomplete#Complete
    
    " Configure menu behavior
    " http://vim.wikia.com/wiki/VimTip1386
    set completeopt=longest,menuone
    inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
    inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
      \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
    inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
      \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
    
    " Use Ctrl+Space for omni-completion
    " https://stackoverflow.com/questions/510503/ctrlspace-for-omni-and-keyword-completion-in-vim
    inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
      \ "\<lt>C-n>" :
      \ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
      \ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
      \ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
    imap <C-@> <C-Space>
    
    " Popup menu hightLight Group
    highlight Pmenu ctermbg=13 guibg=LightGray
    highlight PmenuSel ctermbg=7 guibg=DarkBlue guifg=White
    highlight PmenuSbar ctermbg=7 guibg=DarkGray
    highlight PmenuThumb guibg=Black
    
    " Enable global scope search
    let OmniCpp_GlobalScopeSearch = 1
    " Show function parameters
    let OmniCpp_ShowPrototypeInAbbr = 1
    " Show access information in pop-up menu
    let OmniCpp_ShowAccess = 1
    " Auto complete after '.'
    let OmniCpp_MayCompleteDot = 1
    " Auto complete after '->'
    let OmniCpp_MayCompleteArrow = 1
    " Auto complete after '::'
    let OmniCpp_MayCompleteScope = 0
    " Don't select first item in pop-up menu
    let OmniCpp_SelectFirstItem = 0
    

    并使用 Ctrl + Space 来自动完成.

    眼睛糖果外观

    256色

    首先,您要确保终端支持256色.例如,可以使用 urxvt-256 终端来实现.对于gnome-terminal,您只需将下一行添加到~/.bashrc:

     export TERM="xterm-256color"
     

    完成后,将下一行添加到~/.vimrc:

    set t_Co=256
    

    配色方案

    现在,您更喜欢~/.vim/colors的下载方案,并在~/.vimrc中选择它们:

    set background=dark
    colorscheme hybrid
    

    使用哪种配色方案是强烈基于意见的问题.我可能会推荐 mrkn256 隔离对于初学者.

    字体

    有很多不错的字体可以在那里编程. Linux上的许多程序员都使用 Terminus 字体,您可以尝试使用入门字体.

    已知的缺点

    vim中仍然缺少某些功能.

    1. cscope/ctags无法使用include/generated/autoconf.h中的定义,并且会忽略未构建的代码.索引所有代码以在编码时用作参考仍然有用.
    2. 没有宏扩展(嗯,一些带有CDT的Eclipse .

      Kernel development is actually different from a traditional C project development (from my view, as a newbie). So, I always wonder what is the vim configuration of a kernel hacker.

      The most important is that how to navigate kernel source tree in vim.. I tried ctags, however, it works terribly.

      Can someone give me a clue?

      解决方案

      Main differences between Linux kernel and regular C project (from developer's point of view) are next:

      • kernel is very big project (so you should choose which code to index)
      • it has architecture dependent code (and you are only interested in one specific architecture at a time; other architectures shouldn't be indexed)
      • it has very specific coding style you should stick to (and vim should be configured to display code accordingly)
      • it doesn't use C standard library, but instead has its own similar routines (so your index tool shouldn't index libc headers)

      Installing indexing tools

      To navigate kernel code I would advise cscope and ctags tools. To install them run next command:

      $ sudo aptitude install cscope exuberant-ctags
      

      A little explanation:

      • cscope: will be used to navigate the code (switch between functions, etc.). It's able to jump to symbol definition, find all symbol usages, etc.
      • ctags: needed for Tagbar plugin (will be discussed further) and for Omni completion (auto completion mechanism in vim); can be also used for navigation. ctags is not as a good choice for C code navigation as cscope, because ctags is only able to jump to symbol definition (not to its callers).

      Creating index database

      Now you should index your kernel source files. There are 2 approaches here: create index manually or use available script in kernel. If you are not sure which way is best for you, I recommend to go with kernel script, as it does a lot of neat tricks behind the scenes (like ignoring non-built sources and moving header files on top of the result list).

      But first of all, configure and build the kernel for your architecture/board, as built files can be used later to improve indexing process.

      Indexing with scripts/tags.sh

      Kernel has quite good script (scripts/tags.sh) for creating kernel index database. One should use make cscope and make tags rules to create index, instead of running that script directly.

      Example:

      $ make O=. ARCH=arm SUBARCH=omap2 COMPILED_SOURCE=1 cscope tags
      

      where

      • O=. - use absolute paths (useful if you want to load created cscope/ctags index files outside of kernel directory, e.g. for development of out-of-tree kernel modules). If you want to use relative paths (i.e. you're gonna do development only in kernel dir), just omit that parameter
      • ARCH=... - select CPU architecture to be indexed. See directories under arch/ for reference. For example, if ARCH=arm, then arch/arm/ directory will be indexed, the rest of arch/* directories will be ignored
      • SUBARCH=... - select sub-architecture (i.e. board-related files) to be indexed. For example, if SUBARCH=omap2, only arch/arm/mach-omap2/ and arch/arm/plat-omap/ directories will be indexed, the rest of machines and platforms will be ignored.
      • COMPILED_SOURCE=1 - index only compiled files. You are usually only interested in source files used in your build (hence compiled). If you want to index also files that weren't built, just omit this option.
      • cscope - rule to make cscope index
      • tags - rule to make ctags index

      Indexing manually

      Kernel script (tags.sh) might not work correctly or you may want to have more control over indexing process. In those cases you should index kernel sources manually.

      Insights on manual indexing were taken from here.

      First you need to create cscope.files file which would list all files you want to index. For example, I'm using next commands to list files for ARM architecture (arch/arm), and particularly for OMAP platform (excluding rest of platforms to keep navigation easy):

      find    $dir                                          \
              -path "$dir/arch*"               -prune -o    \
              -path "$dir/tmp*"                -prune -o    \
              -path "$dir/Documentation*"      -prune -o    \
              -path "$dir/scripts*"            -prune -o    \
              -path "$dir/tools*"              -prune -o    \
              -path "$dir/include/config*"     -prune -o    \
              -path "$dir/usr/include*"        -prune -o    \
              -type f                                       \
              -not -name '*.mod.c'                          \
              -name "*.[chsS]" -print > cscope.files
      find    $dir/arch/arm                                 \
              -path "$dir/arch/arm/mach-*"     -prune -o    \
              -path "$dir/arch/arm/plat-*"     -prune -o    \
              -path "$dir/arch/arm/configs"    -prune -o    \
              -path "$dir/arch/arm/kvm"        -prune -o    \
              -path "$dir/arch/arm/xen"        -prune -o    \
              -type f                                       \
              -not -name '*.mod.c'                          \
              -name "*.[chsS]" -print >> cscope.files
      find    $dir/arch/arm/mach-omap2/                     \
              $dir/arch/arm/plat-omap/                      \
              -type f                                       \
              -not -name '*.mod.c'                          \
              -name "*.[chsS]" -print >> cscope.files
      

      For x86 architecture (arch/x86) you can use something like this:

      find    $dir                                          \
              -path "$dir/arch*"               -prune -o    \
              -path "$dir/tmp*"                -prune -o    \
              -path "$dir/Documentation*"      -prune -o    \
              -path "$dir/scripts*"            -prune -o    \
              -path "$dir/tools*"              -prune -o    \
              -path "$dir/include/config*"     -prune -o    \
              -path "$dir/usr/include*"        -prune -o    \
              -type f                                       \
              -not -name '*.mod.c'                          \
              -name "*.[chsS]" -print > cscope.files
      find    $dir/arch/x86                                 \
              -path "$dir/arch/x86/configs"    -prune -o    \
              -path "$dir/arch/x86/kvm"        -prune -o    \
              -path "$dir/arch/x86/lguest"     -prune -o    \
              -path "$dir/arch/x86/xen"        -prune -o    \
              -type f                                       \
              -not -name '*.mod.c'                          \
              -name "*.[chsS]" -print >> cscope.files
      

      Where dir variable can have one of next values:

      • .: if you are gonna work only in kernel source code directory; in this case those commands should be run from root directory of kernel source code
      • absolute path to your kernel source code directory: if you are gonna develop some out-of-tree kernel module; in this case script can be run from anywhere

      I'm using first option (dir=.), because I'm not developing any out-of-tree modules.

      Now when cscope.files file is ready, we need to run actual indexing:

      $ cscope -b -q -k
      

      Where -k parameter tells cscope to not index C standard library (as kernel doesn't use it).

      Now it's time to create ctags index database. To accelerate this stage, we're gonna reuse already created cscope.files:

      $ ctags -L cscope.files
      

      Ok, cscope and ctags index databases are built, and you can remove cscope.files file, as we don't need it anymore:

      $ rm -f cscope.files
      

      Next files contain index databases (for cscope and ctags):

      - cscope.in.out
      - cscope.out
      - cscope.po.out
      - tags
      

      Keep them in root of kernel sources directory.

      vim plugins

      NOTE: Further I show how to use pathogen for handling Vim plugins. But now that Vim 8 is released, one can use native package loading for the same purpose.

      Next we are gonna install some plugins for vim. To have a better grasp on it, I encourage you to use pathogen plugin. It allows you to just git clone vim plugins to your ~/.vim/bundle/ and keep them isolated, rather than mixing files from different plugins in ~/.vim directory.

      Install pathogen like it's described here.

      Don't forget to do next stuff (as it's described at the same link):

      Add this to your vimrc:

      execute pathogen#infect()
      

      If you're brand new to Vim and lacking a vimrc, vim ~/.vimrc and paste in the following super-minimal example:

      execute pathogen#infect()
      syntax on
      filetype plugin indent on
      

      Installing cscope maps for vim

      Vim already has cscope support in it (see :help cscope). You can jump to symbol or file using commands like :cs f g kfree. It's not so convenient though. To accelerate things you can use shortcuts instead (so you can put your cursor on some function, press some key combination and jump to function). In order to add shortcuts for cscope you need to obtain cscope_maps.vim file.

      To install it using pathogen you can just clone this repo to your ~/.vim/bundle:

      $ git clone https://github.com/joe-skb7/cscope-maps.git ~/.vim/bundle/cscope-maps
      

      Now you should be able to navigate between functions and files in vim using shortcuts. Open some kernel source file, put your keyboard cursor on some function call, and press Ctrl+\ followed by g. It should bring you to the function implementation. Or it can show you all available function implementations, then you can choose which one to use: .

      For the rest of key mappings see cscope_maps.vim file.

      You can also use commands in vim like:

      :cs f g kmalloc
      

      See :help cscope for details.

      ctags note

      ctags still can be useful for navigation, for example when looking for some #define declaration. You can put cursor on this define usage and press g followed by Ctrl+]. See this answer for details.

      cscope note

      Next trick can be used to find structure declaration in kernel:

      :cs f t struct device {
      

      Note that above command relies on specific struct declaration style (used in kernel), so we know that struct declaration is always has this form: struct some_stuct {. This trick might not work in projects with another coding style.

      out-of-tree modules development note

      If you are developing out-of-tree module, you will probably need to load cscope and ctags databases from your kernel directory. It can be done by next commands in vim (in command mode).

      Load external cscope database:

      :cs add /path/to/your/kernel/cscope.out
      

      Load external ctags database:

      :set tags=/path/to/your/kernel/tags
      

      vimrc

      Some modifications need to be done to your ~/.vimrc as well, in order to better support kernel development.

      First of all, let's highlight 81th column with vertical line (as kernel coding requires that you should keep your lines length at 80 characters max):

      " 80 characters line
      set colorcolumn=81
      "execute "set colorcolumn=" . join(range(81,335), ',')
      highlight ColorColumn ctermbg=Black ctermfg=DarkRed
      

      Uncomment second line if you want to make 80+ columns highlighted as well.

      Trailing spaces are prohibited by kernel coding style, so you may want to highlight them:

      " Highlight trailing spaces
      " http://vim.wikia.com/wiki/Highlight_unwanted_spaces
      highlight ExtraWhitespace ctermbg=red guibg=red
      match ExtraWhitespace /\s\+$/
      autocmd BufWinEnter * match ExtraWhitespace /\s\+$/
      autocmd InsertEnter * match ExtraWhitespace /\s\+\%#\@<!$/
      autocmd InsertLeave * match ExtraWhitespace /\s\+$/
      autocmd BufWinLeave * call clearmatches()
      

      Kernel coding style

      To make vim respect kernel coding style, you can pull ready to use plugin: vim-linux-coding-style.

      Useful plugins

      Next plugins are commonly used, so you can find them useful as well:

      Also these are interesting plugins, but you may need to configure them for kernel:

      Omni completion

      Vim 7 (and up) already has auto completion support built in it. It calls Omni completion. See :help new-omni-completion for details.

      Omni completion works rather slow on such a big project as kernel. If you still want it, you can enable it adding next lines to your ~/.vimrc:

      " Enable OmniCompletion
      " http://vim.wikia.com/wiki/Omni_completion
      filetype plugin on
      set omnifunc=syntaxcomplete#Complete
      
      " Configure menu behavior
      " http://vim.wikia.com/wiki/VimTip1386
      set completeopt=longest,menuone
      inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
      inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
        \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
      inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
        \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
      
      " Use Ctrl+Space for omni-completion
      " https://stackoverflow.com/questions/510503/ctrlspace-for-omni-and-keyword-completion-in-vim
      inoremap <expr> <C-Space> pumvisible() \|\| &omnifunc == '' ?
        \ "\<lt>C-n>" :
        \ "\<lt>C-x>\<lt>C-o><c-r>=pumvisible() ?" .
        \ "\"\\<lt>c-n>\\<lt>c-p>\\<lt>c-n>\" :" .
        \ "\" \\<lt>bs>\\<lt>C-n>\"\<CR>"
      imap <C-@> <C-Space>
      
      " Popup menu hightLight Group
      highlight Pmenu ctermbg=13 guibg=LightGray
      highlight PmenuSel ctermbg=7 guibg=DarkBlue guifg=White
      highlight PmenuSbar ctermbg=7 guibg=DarkGray
      highlight PmenuThumb guibg=Black
      
      " Enable global scope search
      let OmniCpp_GlobalScopeSearch = 1
      " Show function parameters
      let OmniCpp_ShowPrototypeInAbbr = 1
      " Show access information in pop-up menu
      let OmniCpp_ShowAccess = 1
      " Auto complete after '.'
      let OmniCpp_MayCompleteDot = 1
      " Auto complete after '->'
      let OmniCpp_MayCompleteArrow = 1
      " Auto complete after '::'
      let OmniCpp_MayCompleteScope = 0
      " Don't select first item in pop-up menu
      let OmniCpp_SelectFirstItem = 0
      

      And use Ctrl+Space for auto completion.

      Eye candy appearance

      256 colors

      First of all you want to be sure that your terminal supports 256 colors. For example, it can be achieved using urxvt-256 terminal. For gnome-terminal you can just add next line to your ~/.bashrc:

      export TERM="xterm-256color"
      

      Once it's done put next line to your ~/.vimrc:

      set t_Co=256
      

      Color scheme

      Now download schemes you prefer to ~/.vim/colors and select them in ~/.vimrc:

      set background=dark
      colorscheme hybrid
      

      Which color scheme to use is strongly opinion based matter. I may recommend mrkn256, hybrid and solarized for starters.

      Font

      There are a lot of good fonts for programming out there. Many programmers on Linux use Terminus font, you can try it for starters.

      Known shortcomings

      Some features are still missing in vim.

      1. cscope/ctags can't use definitions from include/generated/autoconf.h and ignore code that wasn't built. It is still may be useful to have all code indexed to use it as reference when coding.
      2. There is no macro expansion (well, there is some function out there (based on gcc -E), but I'm not sure if it's gonna work for kernel).

      The only IDE I know to handle those issues is Eclipse with CDT.

      这篇关于用于Linux内核开发的Vim配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆