首页 > 系统相关 >在Ubuntu下,为Vim配置C/C++代码补全

在Ubuntu下,为Vim配置C/C++代码补全

时间:2024-09-30 16:48:43浏览次数:11  
标签:co 补全 c# float C++ Vim nmap coc vim

1. 安装vim-plug

vim-plug 是 vim 的一个插件管理器。

(1)vim-plug的下载网址

(2)新建目录 ~/.vim/autoload/

(3)将 plug.vim 文件放入该目录

(4)添加 vim-plug 的代码到 ~/.vimrc 文件,如下所示

call plug#begin()

" List your plugins here
Plug 'tpope/vim-sensible'

call plug#end()

(5)vim-plug 插件的基本命令为:

:PlugInstallto install the plugins
:PlugUpdateto install or update the plugins
:PlugDiffto review the changes from the last update
:PlugCleanto remove plugins no longer in the list

2. 安装 coc.nvim

(1)coc.nvim的下载网址

(2)在 vim-plug 中安装该插件

Plug 'neoclide/coc.nvim', {'branch': 'release'}

(3)安装 clangd

sudo apt install clangd

(4)安装 nodejs

sudo apt install nodejs

(5)确保 vim >= 9.0.0438,并且 nodejs >= 16.18.0

(6)更新 vim 到最新版本

sudo add-apt-repository ppa:jonathonf/vim

sudo apt install vim

(7)更新 nodejs 到最新版本

sudo apt install npm

sudo npm install -g n

sudo N_NODE_MIRROR=https://npmmirror.com/mirrors/node n stable

bash

3. 配置 CocConfig

(1)安装 coc-clangd 插件

将 coc.nvim:registry=https://registry.npmmirror.com 添加进 ~/.npmrc,接着执行下列命令:

:CocInstall coc-clangd

将下列代码粘贴到 CocConfig 文件中

"languageserver": {
  "clangd": {
    "command": "clangd",
    "rootPatterns": ["compile_flags.txt", "compile_commands.json"],
    "filetypes": ["c", "cc", "cpp", "c++", "objc", "objcpp"]
  }
}

将下列代码粘贴进 ~/.vimrc 中

 https://raw.githubusercontent.com/neoclide/coc.nvim/master/doc/coc-example-config.vim

" May need for Vim (not Neovim) since coc.nvim calculates byte offset by count
" utf-8 byte sequence
set encoding=utf-8
" Some servers have issues with backup files, see #649
set nobackup
set nowritebackup

" Having longer updatetime (default is 4000 ms = 4s) leads to noticeable
" delays and poor user experience
set updatetime=300

" Always show the signcolumn, otherwise it would shift the text each time
" diagnostics appear/become resolved
set signcolumn=no

" Use tab for trigger completion with characters ahead and navigate
" NOTE: There's always complete item selected by default, you may want to enable
" no select by `"suggest.noselect": true` in your configuration file
" NOTE: Use command ':verbose imap <tab>' to make sure tab is not mapped by
" other plugin before putting this into your config
inoremap <silent><expr> <TAB>
      \ coc#pum#visible() ? coc#pum#next(1) :
      \ CheckBackspace() ? "\<Tab>" :
      \ coc#refresh()
inoremap <expr><S-TAB> coc#pum#visible() ? coc#pum#prev(1) : "\<C-h>"

" Make <CR> to accept selected completion item or notify coc.nvim to format
" <C-g>u breaks current undo, please make your own choice
inoremap <silent><expr> <CR> coc#pum#visible() ? coc#pum#confirm()
                              \: "\<C-g>u\<CR>\<c-r>=coc#on_enter()\<CR>"

function! CheckBackspace() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

" Use <c-space> to trigger completion
if has('nvim')
  inoremap <silent><expr> <c-space> coc#refresh()
else
  inoremap <silent><expr> <c-@> coc#refresh()
endif

" Use `[g` and `]g` to navigate diagnostics
" Use `:CocDiagnostics` to get all diagnostics of current buffer in location list
nmap <silent> [g <Plug>(coc-diagnostic-prev)
nmap <silent> ]g <Plug>(coc-diagnostic-next)

" 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)

" Use K to show documentation in preview window
nnoremap <silent> K :call ShowDocumentation()<CR>

function! ShowDocumentation()
  if CocAction('hasProvider', 'hover')
    call CocActionAsync('doHover')
  else
    call feedkeys('K', 'in')
  endif
endfunction

" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')

" Symbol renaming
nmap <leader>rn <Plug>(coc-rename)

" Formatting selected code
xmap <leader>f  <Plug>(coc-format-selected)
nmap <leader>f  <Plug>(coc-format-selected)

augroup mygroup
  autocmd!
  " Setup formatexpr specified filetype(s)
  autocmd FileType typescript,json setl formatexpr=CocAction('formatSelected')
  " Update signature help on jump placeholder
  autocmd User CocJumpPlaceholder call CocActionAsync('showSignatureHelp')
augroup end

" Applying code actions to the selected code block
" Example: `<leader>aap` for current paragraph
xmap <leader>a  <Plug>(coc-codeaction-selected)
nmap <leader>a  <Plug>(coc-codeaction-selected)

" Remap keys for applying code actions at the cursor position
nmap <leader>ac  <Plug>(coc-codeaction-cursor)
" Remap keys for apply code actions affect whole buffer
nmap <leader>as  <Plug>(coc-codeaction-source)
" Apply the most preferred quickfix action to fix diagnostic on the current line
nmap <leader>qf  <Plug>(coc-fix-current)

" Remap keys for applying refactor code actions
nmap <silent> <leader>re <Plug>(coc-codeaction-refactor)
xmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)
nmap <silent> <leader>r  <Plug>(coc-codeaction-refactor-selected)

" Run the Code Lens action on the current line
nmap <leader>cl  <Plug>(coc-codelens-action)

" Map function and class text objects
" NOTE: Requires 'textDocument.documentSymbol' support from the language server
xmap if <Plug>(coc-funcobj-i)
omap if <Plug>(coc-funcobj-i)
xmap af <Plug>(coc-funcobj-a)
omap af <Plug>(coc-funcobj-a)
xmap ic <Plug>(coc-classobj-i)
omap ic <Plug>(coc-classobj-i)
xmap ac <Plug>(coc-classobj-a)
omap ac <Plug>(coc-classobj-a)

" Remap <C-f> and <C-b> to scroll float windows/popups
if has('nvim-0.4.0') || has('patch-8.2.0750')
  nnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  nnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
  inoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(1)\<cr>" : "\<Right>"
  inoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? "\<c-r>=coc#float#scroll(0)\<cr>" : "\<Left>"
  vnoremap <silent><nowait><expr> <C-f> coc#float#has_scroll() ? coc#float#scroll(1) : "\<C-f>"
  vnoremap <silent><nowait><expr> <C-b> coc#float#has_scroll() ? coc#float#scroll(0) : "\<C-b>"
endif

" Use CTRL-S for selections ranges
" Requires 'textDocument/selectionRange' support of language server
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)

" Add `:Format` command to format current buffer
command! -nargs=0 Format :call CocActionAsync('format')

" Add `:Fold` command to fold current buffer
command! -nargs=? Fold :call     CocAction('fold', <f-args>)

" Add `:OR` command for organize imports of the current buffer
command! -nargs=0 OR   :call     CocActionAsync('runCommand', 'editor.action.organizeImport')

" Add (Neo)Vim's native statusline support
" NOTE: Please see `:h coc-status` for integrations with external plugins that
" provide custom statusline: lightline.vim, vim-airline
set statusline^=%{coc#status()}%{get(b:,'coc_current_function','')}

" Mappings for CoCList
" Show all diagnostics
nnoremap <silent><nowait> <space>a  :<C-u>CocList diagnostics<cr>
" Manage extensions
nnoremap <silent><nowait> <space>e  :<C-u>CocList extensions<cr>
" Show commands
nnoremap <silent><nowait> <space>c  :<C-u>CocList commands<cr>
" Find symbol of current document
nnoremap <silent><nowait> <space>o  :<C-u>CocList outline<cr>
" Search workspace symbols
nnoremap <silent><nowait> <space>s  :<C-u>CocList -I symbols<cr>
" Do default action for next item
nnoremap <silent><nowait> <space>j  :<C-u>CocNext<CR>
" Do default action for previous item
nnoremap <silent><nowait> <space>k  :<C-u>CocPrev<CR>
" Resume latest coc list
nnoremap <silent><nowait> <space>p  :<C-u>CocListResume<CR>

标签:co,补全,c#,float,C++,Vim,nmap,coc,vim
From: https://blog.csdn.net/weixin_47777621/article/details/142534143

相关文章

  • vscode 配置 c/c++
    vscode配置c/c++[!CAUTION]使用本文的配置需要预装cmake,msvc,拥有cmake、CMakeTools插件工程目录D:.│CMakeLists.txt│├─.vscode│CMakePresets.json│c_cpp_properties.json│└─code│main.cpp│└─head......
  • 分享C++程序员面试八股文(十五)
    以下是C++常见八股文(十五):一、C++中的高级文件操作(AdvancedFileOperations)解释文件随机访问的方法及应用场景方法:在C++中,可以使用文件流对象(如std::ifstream、std::ofstream、std::fstream)的seekg(设置输入位置)和seekp(设置输出位置)成员函数来实现文件的随机访问。这......
  • Qt/C++ 音视频开发 - FFmpeg 安卓版
    Qt/C++音视频开发-FFmpeg安卓版介绍FFmpeg是一个开源的多媒体框架,它可以用来录制、转换和流式传输音视频。在Qt/C++开发中,FFmpeg可以用于处理各种音视频任务,例如转码、推流等。将FFmpeg集成到安卓平台上,可以实现强大的移动端音视频处理功能。应用使用场景视频......
  • 南沙C++信奥赛陈老师解一本通题 2005:【20CSPJ普及组】直播获奖
    ​ 【题目描述】NOI2130即将举行。为了增加观赏性,CCF决定逐一评出每个选手的成绩,并直播即时的获奖分数线。本次竞赛的获奖率为 w%w%,即当前排名前 w%w% 的选手的最低成绩就是即时的分数线。更具体地,若当前已评出了 pp 个选手的成绩,则当前计划获奖人数为 max(1,⌊p∗w%......
  • 南沙C++信奥赛陈老师解一本通题1965:【14NOIP普及组】珠心算测验
    ​ 【题目描述】珠心算是一种通过在脑中模拟算盘变化来完成快速运算的一种计算技术。珠心算训练,既能够开发智力,又能够为日常生活带来很多便利,因而在很多学校得到普及。某学校的珠心算老师采用一种快速考察珠心算加法能力的测验方法。他随机生成一个正整数集合,集合中的数各不......
  • C++入门
    第1节:开发环境的搭建与配置1.1目标在本节课中,学生将学习如何在Windows上搭建一个现代化的C++开发环境,并使用VSCode和CMake工具进行C++程序的开发与调试。学生将掌握以下内容:安装VSCode及C++插件安装MinGW或其他C++编译器安装并配置CMake创建并编译第一个C++项目使用VSCod......
  • 南沙C++信奥赛陈老师解一本通题:1945:【09NOIP普及组】多项式输出
    ​ 【题目描述】一元 nn 次多项式可用如下的表达式表示: f(x)=anxn+an−1xn−1+...+a1x+a0,an≠0f(x)=anxn+an−1xn−1+...+a1x+a0,an≠0 其中,aixii 称为i次项,ai称为ii次项的系数。给出一个一元多项式各项的次数和系数,请按照如下规定的格式要求输出该多项式:1.多项式中......
  • 分享C++程序员面试八股文(十四)
    以下是C++常见八股文(十四):一、C++中的智能指针高级用法(AdvancedUsageofSmartPointers)解释unique_ptr、shared_ptr和weak_ptr的循环引用问题及解决方法循环引用问题:当使用shared_ptr进行相互引用时,可能会导致循环引用问题。例如,两个对象相互持有对方的shared_pt......
  • c++:引用
    一、引用概念是什么?引用不是新定义一个变量,而是给已存在变量取了一个别名,编译器不会为引用变量开辟内存空间,它和它引用的变量共用同一块内存空间。比如:李逵,在家称为"铁牛",江湖上人称"黑旋风"类型&引用变量名(对象名)=引用实体;voidTestRef(){inta=10;int......
  • C++发邮件:如何轻松实现邮件自动化发送?
    C++发邮件的详细步骤与教程指南?如何在C++中发邮件?无论是定期发送报告、通知客户还是管理内部沟通,自动化邮件系统都能显著提升工作效率。AokSend将详细介绍如何使用C++发邮件,实现邮件自动化发送,帮助您轻松管理邮件通信。C++发邮件:配置环境在选择了合适的C++发邮件库之后,接下......