vim coc.nvim安装及配置c,c++补全

在安装之前需要安装npm yarn node.js

# 这里以ubuntu示例 
sudo apt install clang # 如果是c,c++补全需要安装
# 安装npm
sudo apt-get install npm
# 安装yarn
sudo npm install -g yarn
# 安装升级node.js到最新版本
sudo npm cache clean -f
sudo npm install -g n
sudo n stable
node -v

Vundle

添加下面的内容到.vimrc:

Plugin 'neoclide/coc.nvim'

VimPlug

添加下面的内容到.vimrc:

Plug 'neoclide/coc.nvim'

Vim 里面运行以下命令:

:source %
:PlugInstall

等待成功安装

Vim 里面运行以下命令:

:CocInstall coc-clangd
# CocInstall coc-pyright 是python的

等待成功安装即可

可能遇到的问题

提示vim/nvim版本过低

# 更新vim版本
sudo add-apt-repository ppa:jonathonf/vim
sudo apt update
sudo apt upgrade
# 更新nvim版本
sudo apt-add-repository ppa:neovim-ppa/stable
sudo apt update
sudo apt install neovim

或者是

[coc.nvim] build/index.js not found, please install dependencies and compile coc.nvim by: yarn install

# 解决办法
# ~/.vim/bundle/coc.nvim/    #是我的coc.nvim插件的安装目录
cd ~/.vim/bundle/coc.nvim/	
yarn install
yarn build

或者是
yarn install时候报错,提示不存在这个文件或者目录

# 解决办法 尝试一下,再yarn install
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update
sudo apt install yarn

这里贴一下我的配置

" CocUninstall 删除插件
" CocList extensions  列出扩展
" Cocinstall 安装插件
let g:coc_global_extensions = [
            \'coc-vimlsp',
            \'coc-clangd',
            \'coc-pyright',
            \'coc-word',
            \'coc-snippets']
set pumheight=10 " 显示最大补全
set signcolumn=no " 取消左边报错提醒
highlight CocErrorLine cterm=undercurl ctermfg=Red " 将错误提醒改为 红线
" 使用CocConfig
function! SetupCommandAbbrs(from, to)
  exec 'cnoreabbrev <expr> '.a:from
        \ .' ((getcmdtype() ==# ":" && getcmdline() ==# "'.a:from.'")'
        \ .'? ("'.a:to.'") : ("'.a:from.'"))'
endfunction
call SetupCommandAbbrs('C', 'CocConfig')
" 设置回车补全
inoremap <silent><expr> <cr> pumvisible() ? coc#_select_confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"
" GoTo code navigation.
" 定义跳转
nmap <silent> gd <Plug>(coc-definition)
nmap <silent> gy <Plug>(coc-type-definition)
nmap <silent> gi <Plug>(coc-implementation)
" nmap <silent> gr <Plug>(coc-references)
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)
" Remap for format selected region
xmap <leader>fm  <Plug>(coc-format-selected)
nmap <leader>fm  <Plug>(coc-format-selected)
" 跳转到帮助文档
nnoremap <silent> <LEADER>h :call <SID>show_documentation()<CR>
function! s:show_documentation()
  if (index(['vim','help'], &filetype) >= 0)
    execute 'h '.expand('<cword>')
  elseif (coc#rpc#ready())
    call CocActionAsync('doHover')
  else
    execute '!' . &keywordprg . " " . expand('<cword>')
  endif
endfunction
" set signcolumn=yes
set updatetime=300
autocmd CursorHold * silent call CocActionAsync('highlight') 

" 代码片段 参考 https://blog.csdn.net/niuiic/article/details/117599130
" CocCommand snippets.editSnippets 创建默认代码片段不推荐,推荐自定义位置
" CocCommand snippets.openSnippetFiles 打开代码片段文件

参考文章

[coc.nvim] build/index.js not found, please install dependencies and compile coc.nvim by: yarn install
Press ENTER or type command to continue

上手Coc.nvim 完全指南

posted @ 2023-07-29 02:16  O_JF?  阅读(1449)  评论(0编辑  收藏  举报