个人VIM设置: 主要解决CPP代码补全问题

Vim Easy Setup For Cplusplus


Reference:

  1. cocnvim setup easy tutorial

  2. vim-plug 插件经github下载失效的解决方法

Setup Step by Step

  1. 安装vim-plug并对特定选项进行设置

    先使用vim ~/.vimrc进行基础写入

    syntax on
    filetype plugin indent on 
    set ts=2 sts=2 sw=2 et ai si
    set rnu
    call plug#begin()
    
    call plug#end()
    

    安装vim-plug

    sudo apt-get install curl
    curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
    

    如果无法安装vim-plug, 请参考我的这篇博客

  2. 安装coc-nvim插件

    同样vim ~/.vimrc, 并在call plug#begin() call plug#end()代码块中写入如下内容:

    call plug#begin()
    Plug 'neoclide/coc.nvim' 
    call plug#end()
    

    并在vim的指令模型下输入:PlugInstall

    如果插件安装报443错误,请参考链接2。注意,截止2024年6月12日,有效的第三方链接为:
    hub.nuaa.cf

    完整的文件修改为,cd ~/.vim/autoload && vim plug.vim, 找到如下的两行内容进行修改:

    let fmt = get(g:, 'plug_url_format', 'https://git::@github.com/%s.git')
    

    改为:

    let fmt = get(g:, 'plug_url_format', 'https://git::@hub.nuaa.cf/%s.git')
    

    以及:

    \ '^https://git::@github\.com', 'https://github.com', '')
    

    改为:

    \ '^https://git::@hub.nuaa\.cf', 'https://hub.nuaa.cf', '')
    
  3. 安装nodejs

    下载nodejs, Ubuntu20.04版本下采用18可以在后续步骤中安装通过:

    curl -sl https://deb.nodesource.com/setup_18.x | sudo -E bash -
    

    安装nodejs

    sudo apt-get install -y nodejs
    

    利用nodejs下载yarn

    sudo npm i -g yarn
    

    yarn完成cocnvim的剩下安装

    cd ~/.vim/plugged/coc.nvim/
    yarn install
    
  4. cocnvim 完成 clangd下载

    重新vim ~/.vimrc后进行:CocInstall coc-clangd, 然后CocCommand clangd.install

  5. 设置键作为补全

    进入.vimrc后将下面的内容复制到最后

    " use <tab> to trigger completion and navigate to the next complete item
    function! CheckBackspace() abort
        let col = col('.') - 1
        return !col || getline('.')[col - 1]  =~# '\s'
    endfunction
    
    inoremap <silent><expr> <Tab>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
    

    参考链接

  6. 补充其余插件

    进入.vimrc,并在call plug#begin() call plug#end()中插入下面的代码

    call plug#begin()
    Plug 'neoclide/coc.nvim' 
    Plug 'jiangmiao/auto-pairs'
    Plug 'gruvbox-community/gruvbox'
    Plug 'vim-airline/vim-airline'
    Plug 'vim-airline/vim-airline-themes'
    call plug#end()
    

    再次在vim的指令模式下运行:PlugInstall即可安装.

    最后为.vimrc补充最后的两行:

    set termguicolors
    colo gruvbox
    

    安装nerdtree插件,这里需要注意上面我们修改plug.vim文件来加速插件安装,但是该插件需要将plug.vim修改了的地方进行还原. 该插件的安装与上面的类似:

    call plug#begin()
    Plug 'neoclide/coc.nvim' 
    Plug 'jiangmiao/auto-pairs'
    Plug 'gruvbox-community/gruvbox'
    Plug 'vim-airline/vim-airline'
    Plug 'vim-airline/vim-airline-themes'
    
    Plug 'preservim/nerdtree'
    call plug#end()
    

    同样通过:PlugInstall进行安装即可.

  7. 完整的~/.vimrc如下:

    syntax on
    filetype plugin indent on
    set ts=2 sts=2 sw=2 et ai si
    set rnu
    call plug#begin()
    
    Plug 'neoclide/coc.nvim'
    Plug 'jiangmiao/auto-pairs'
    Plug 'gruvbox-community/gruvbox'
    Plug 'vim-airline/vim-airline'
    Plug 'vim-airline/vim-airline-themes'
    Plug 'preservim/nerdtree'
    
    call plug#end()
    
    " use <tab> to trigger completion and navigate to the next complete item
    function! CheckBackspace() abort
      let col = col('.') - 1
      return !col || getline('.')[col - 1]  =~# '\s'
    endfunction
    
    inoremap <silent><expr> <Tab>
          \ coc#pum#visible() ? coc#pum#next(1) :
          \ CheckBackspace() ? "\<Tab>" :
          \ coc#refresh()
    
    set termguicolors
    colo gruvbox
    
    inoremap <c-b> <Esc>:NERDTreeToggle<cr>
    nnoremap <c-b> <Esc>:NERDTreeToggle<cr>
    

    最后两行是通过+b打开文件树
    效果图

One more thing

上面的配置已经足够完成cpp的代码补全了,但代码编程完后,在vim的命令模式下:!bash可以返回bash,此时可以完成如g++之类的操作。相应地,通过在bash中键入exit即可返回vim中。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值