效果展示
- 整体效果
-
支持定义、声明跳转
-
支持引用查找和跳转:
-
支持代码补全和snippet:
-
支持语法错误提示(基于clangd):
-
光标所在行高亮(采用了背景色轻红):
-
行尾多余空格红色显示
-
开启vim自身的语法高亮功能,按照以下分类高亮:
- -
支持保存自动格式化(基于clang-format)
- 增加代码块虚线
[
- 选中一键注释
映射了windows几个常用的按键:
- ctrl+s=保存
- ctrl+c=复制,支持和系统剪切板互通,但和y、p命令不互通(此处占用了vim的列块模式)
- ctrl+v=粘贴(ctrl+v是vim的列块模式),支持和系统剪切板互通,但和y、p命令不互通
- space+f=查找(ctrl+f是vim的翻页)
- space+r=全局替换选中文本(ctrl+r是vim的重做)
- ctrl+a=全选
- ctrl+/ 注释选中文本,再次执行取消注释
- ctrl+n 新建tab标签
- tab 切换下一个tab标签
- shift-tab 正常模式下切换前一个tab标签,输入模式下反向缩进
- alt+数字 tab标签切换
- ctrl+j上移一行
- ctrl+k下移一行
配置流程
第一步
需要预装几个工具:
$ sudo apt install -y libclang-dev universal-ctags nodejs clang-format clangd
第二步
vim需要安装插件管理器,安装方法见其他文章,然后更新以上配置到~/.vimrc,打开vim,执行:PlugInstall
附录 .vimrc配置文件
filetype on " 開啓文件類型檢測
syntax enable "开启语法高亮,和systax on的区别是允许你使用highlight命令设置你喜欢的颜色,而systax on会覆盖你的修改,不管先后
set encoding=UTF-8 " 設置文件編碼
call plug#begin() " 安裝插件
" 没有tagbar好用,所以用tagbar替代
"Plug 'vim-scripts/taglist.vim' " depend command ctags
"let Tlist_Exit_OnlyWindow=1
"let Tlist_Ctags_Cmd = '/usr/bin/ctags' "设定系统中ctags程序的位置
"let Tlist_Show_One_File = 1 "不同时显示多个文件的tag,只显示当前文件的
"let Tlist_Auto_Update=1
"let Tlist_Display_Tag_Scope=1
"map <silent> <F9> :TlistToggle<CR> "按F9等同于在命令行模式输入:TlistToggle
"autocmd vimenter *.c,*.cc,*.h,*.hpp,*.cpp :TlistOpen | wincmd p
" Use release branch (recommended)
Plug 'neoclide/coc.nvim', {'branch': 'release'}
" 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>"
" 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)
" Highlight the symbol and its references when holding the cursor
autocmd CursorHold * silent call CocActionAsync('highlight')
" Symbol renaming
nmap <leader>rn <Plug>(coc-rename)
let g:coc_global_extensions = [
\ 'coc-json',
\ 'coc-tsserver',
\ 'coc-clangd',
\ 'coc-sh',
\ 'coc-css',
\ 'coc-html',
\ 'coc-vimlsp',
\ 'coc-cmake',
\ 'coc-highlight',
\ 'coc-pyright'
\ ]
Plug 'vim-autoformat/vim-autoformat'
let g:autoformat_verbosemode=1 "开启详细模式便于查错
let g:formatdef_clangformat_google = '"clang-format-14 -style google -"' " 設置格式化命令,選擇clang-format, google風格
let g:formatters_c = ['clangformat_google'] " 對C語言進行格式化
let g:formatters_cpp = ['clangformat_google'] " 對C++語言進行格式化
"
"au BufWrite *.c,*.cc,*.cpp,*.h,*.hpp :Autoformat " 保存時自動格式化
Plug 'yggdroot/indentline' " 代码块虚线
Plug 'honza/vim-snippets' " 提供代碼片段
Plug 'ludovicchabant/vim-gutentags'
" gutentags搜索工程目录的标志,碰到这些文件/目录名就停止向上一级目录递归,
" 可以自行添加 "
let g:gutentags_project_root = ['.root', '.bashrc', '.profile']
" 所生成的数据文件的名称 "
let g:gutentags_ctags_tagfile = '.tags'
" 将自动生成的 tags 文件全部放入 ~/.cache/tags 目录中,避免污染工程目录 "
let s:vim_tags = expand('~/.cache/tags')
let g:gutentags_cache_dir = s:vim_tags
" 检测 ~/.cache/tags 不存在就新建 "
if !isdirectory(s:vim_tags)
silent! call mkdir(s:vim_tags, 'p')
endif
" 配置 ctags 的参数 "
let g:gutentags_ctags_extra_args = ['--fields=+niazS', '--extra=+q']
let g:gutentags_ctags_extra_args += ['--c++-kinds=+pxI']
let g:gutentags_ctags_extra_args += ['--c-kinds=+px']
Plug 'https://github.com/preservim/nerdtree', { 'on': 'NERDTreeToggle' } " NERD tree will be loaded on the first invocation of NERDTreeToggle command
map <F4> :NERDTreeToggle<CR>
let NERDTreeWinPos = "right"
autocmd vimenter * NERDTree | wincmd p "auto open and go to previous (last accessed) window.
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif " auto close if nerdtree is the last open win
let g:NERDTreeDirArrowExpandable = '+' " 配置目录展开的符号
let g:NERDTreeDirArrowCollapsible = '-' " 配置目录折叠的符号
let NERDTreeIgnore = ['\.pyc$', '\.swp', '\.swo', '__pycache__', '\.o', '\.class'] " 文件忽略列表
let NERDTreeMouseMode = 3 " single click mouse will fold/unfold the dir or open file
Plug 'tiagofumo/vim-nerdtree-syntax-highlight'
"Highlight full name (not only icons). You need to add this if you don't have vim-devicons and want highlight.
let g:NERDTreeFileExtensionHighlightFullName = 1
let g:NERDTreeExactMatchHighlightFullName = 1
let g:NERDTreePatternMatchHighlightFullName = 1
"Highlight full name (not only icons). You need to add this if you don't have vim-devicons and want highlight.
let g:NERDTreeHighlightFolders = 1
"highlights the folder name
let g:NERDTreeHighlightFoldersFullName = 1
"you can add these colors to your .vimrc to help customizing
let s:brown = "905532"
let s:aqua = "3AFFDB"
let s:blue = "0000FF"
let s:darkBlue = "44788E"
let s:purple = "834F79"
let s:lightPurple = "834F79"
let s:red = "AE403F"
let s:beige = "F5C06F"
let s:yellow = "F09F17"
let s:orange = "D4843E"
let s:darkOrange = "F16529"
let s:pink = "CB6F6F"
let s:salmon = "EE6E73"
let s:green = "8FAA54"
let s:Turquoise = "40E0D0"
let s:lightGreen = "31B53E"
let s:white = "FFFFFF"
let s:rspec_red = "FE405F"
let s:git_orange = "F54D27"
let s:gray = "808A87"
let g:NERDTreeExtensionHighlightColor = {} " this line is needed to avoid error
let g:NERDTreeExtensionHighlightColor['o'] = s:gray " sets the color of o files to blue
let g:NERDTreeExtensionHighlightColor['h'] = s:red " sets the color of h files to red
let g:NERDTreeExtensionHighlightColor['c'] = s:blue " sets the color of c files to blue
let g:NERDTreeExtensionHighlightColor['cpp'] = s:blue " sets the color of cpp files to blue
let g:NERDTreeExtensionHighlightColor['cc'] = s:blue " sets the color of cpp files to blue
let g:NERDTreeExtensionHighlightColor['c++'] = s:blue " sets the color of c++ files to blue
"this not working,i have no solution yet!
Plug 'Xuyuanp/nerdtree-git-plugin'
let g:NERDTreeGitStatusIndicatorMapCustom = {
\ "Modified" : "✹",
\ "Staged" : "✚",
\ "Untracked" : "✭",
\ "Renamed" : "➜",
\ "Unmerged" : "═",
\ "Deleted" : "✖",
\ "Dirty" : "✗",
\ "Clean" : "✔︎",
\ 'Ignored' : '☒',
\ "Unknown" : "?"
\}
"If only 2 windows left, NERDTree and Tag_List, close vim, todo, have some bug
function ExitIfOnlyNerdAndTag()
echo tabpagenr("$")
if (exists(":NERDTree") && exists(":TlistOpen"))
exec 'qa'
endif
endif
endfun
"autocmd BufWinLeave * call ExitIfOnlyNerdAndTag()
Plug 'ryanoasis/vim-devicons' " show icon in nerd tree, need to install nerd font, and set terminel font to nerd font
"Can be enabled or disabled
let g:webdevicons_enable_nerdtree = 1
"whether or not to show the nerdtree brackets around flags
let g:webdevicons_conceal_nerdtree_brackets = 1
"adding to vim-airline's tabline
let g:webdevicons_enable_airline_tabline = 1
"adding to vim-airline's statusline
let g:webdevicons_enable_airline_statusline = 1
Plug 'vim-airline/vim-airline-themes'
Plug 'vim-airline/vim-airline'
let g:airline#extensions#tabline#enabled = 1 " 设置开启
let g:airline#extensions#tabline#formatter = 'jsformatter' " 设置默认tab栏样式
let g:airline_theme='angr' " 设置主题
Plug 'https://github.com/ap/vim-css-color' " CSS Color Preview
Plug 'https://github.com/tpope/vim-commentary' " For Commenting
vnoremap <C-_> :Commentary<CR> "ctrl + / 注释
"Plug 'Yggdroot/LeaderF', { 'do': './install.sh' }
Plug 'jiangmiao/auto-pairs' " pair complete
Plug 'kien/rainbow_parentheses.vim'
let g:rbpt_colorpairs = [
\ ['brown', 'RoyalBlue3'],
\ ['Darkblue', 'SeaGreen3'],
\ ['darkgray', 'DarkOrchid3'],
\ ['darkgreen', 'firebrick3'],
\ ['darkcyan', 'RoyalBlue3'],
\ ['darkred', 'SeaGreen3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['brown', 'firebrick3'],
\ ['gray', 'RoyalBlue3'],
\ ['darkmagenta', 'DarkOrchid3'],
\ ['Darkblue', 'firebrick3'],
\ ['darkgreen', 'RoyalBlue3'],
\ ['darkcyan', 'SeaGreen3'],
\ ['darkred', 'DarkOrchid3'],
\ ['red', 'firebrick3'],
\ ]
let g:rbpt_max = 16
let g:rbpt_loadcmd_toggle = 0
au VimEnter * RainbowParenthesesToggle
au Syntax * RainbowParenthesesLoadRound
au Syntax * RainbowParenthesesLoadSquare
au Syntax * RainbowParenthesesLoadBraces
Plug 'mbbill/undotree' " undo历史可视化,用处不大
nnoremap <F5> :UndotreeToggle<CR>
" 配置还需完善,函数跳转后自动关闭了
Plug 'majutsushi/tagbar'
let g:tagbar_ctags_bin = 'ctags' " tagbar 依赖 ctags 插件
let g:tagbar_width = 30 " 设置 tagbar 的宽度为 30 列,默认 40 列
let g:tagbar_autofocus = 1 " 打开 tagbar 时光标在 tagbar 页面内,默认在 vim 打开的文件内
let g:tagbar_left = 1 " 让 tagbar 在页面左侧显示,默认右边
let g:tagbar_sort = 0 " 标签不排序,默认排序
map <F8> :TagbarToggle<CR> "
"autocmd vimenter *.c,*.cc,*.h,*.hpp,*.cpp :TagbarToggle| wincmd p
Plug 'preservim/vim-markdown'
call plug#end()
set undofile " Maintain undo history between sessions
set undodir=~/.vim/undodir " set undo history save dir, you should mkdir ~/.vim/undodir
set shiftwidth=2
" open number
set nu
" open hight search
set hlsearch
" open mouse in all vim mode
set mouse=a
set wildmenu " 启用增强模式的命令行补全。在命令行中输入命令时,按下'wildchar'键(默认为Tab)将自动补全命令和参数:此时将在命令行的上方显示可能的匹配项;继续按下'wildchar'键,可以遍历所有的匹配项;也可以使用方向键或者CTRL-P/CTRL-N键,在匹配列表中进行移动;最后点击回车键,选择需要的匹配项。
set wildmode=list:full " e命令打開文件時,tab按鍵顯示文件補全列表, full默認會選中匹配的第一個, 而longest匹配到只剩一個時纔會選中
set wildignore=*.dll,*.exe,*.jpg,*.gif,*.png " e命令打開文件時忽略哪些文件補全
set iskeyword=@,48-57,-,_,192-255 " 設置單詞由哪些組成
set ts=2 " tabstop, number of space that a <Tab> in the file counts for.
set sts=2 " Number of spaces that a <Tab> counts for while performing editing operations, like inserting a <Tab> or using <BS>.
"对光标所在行进行高亮处理,背景色 轻红色,字体加粗
highlight CursorLine cterm=bold ctermbg=LightRed
"行尾空格高亮,我们经常会遇到行尾有多余空格的情况,但正常情况下,很难发现这些多余的空格。使用高亮来显示这些空格再合适不过了。
highlight extraSpace ctermbg=red guibg=red " 定义高亮组extraSpace
match extraSpace /\v\s+$/ " 匹配行位空格
set backspace=indent,eol,start " 智能回删
set virtualedit=block,onemore " 允许光标出现在最后一个字符的后面
set autoread " 自动加载外部修改
set confirm " 弹出文件未保存确认
set foldmethod=syntax " 按照语法格式进行代码折叠
"support <space> + f 全局查找,ctrl-f 是vim的翻页
noremap <space>f /
" support ctrl-a to select all, ctrl-a是 vim的数字+,不常用,直接覆盖
noremap <C-a> ggVG
" support crtl + s = save file in insert mode
inoremap <C-s> <esc>:w<CR>:Autoformat<CR>a
" support crtl + s = save file in normal mode
nnoremap <C-s> <esc>:w<CR>:Autoformat<CR>
" support global replace for space + r, ctrl + r 是vim的重做
vnoremap <space>r y:%s`<C-R>"``g<left><left>
" support shift tab for reverse tab
inoremap <S-tab> <ESC>:<<CR>a
" move whole line down
noremap <C-j> ddp
" move whole line up
noremap <C-k> :call feedkeys( line('.')==1 ? '' : 'ddkP' )<CR>
" 没有办法同时映射ctrl + shift,原因位置
inoremap <C-S-c> "+y " ctrl + c 复制
vnoremap <C-S-c> "+y " ctrl + c 复制
inoremap <space>v "+p " space + v 粘贴 ctrl + v是vim的列块视图模式
nnoremap <space>v "+p " sapce + v 粘贴 ctrl + v是vim的列块视图模式
" alt + 数字,tab标签切换,和google浏览器的窗口切换快捷键保持一致
" tab 切换下一个tab标签
" shift + tab 切换前一个tab标签
noremap 1 1gt
noremap 2 2gt
noremap 3 3gt
noremap 4 4gt
noremap 5 5gt
noremap 6 6gt
noremap 7 7gt
noremap 8 8gt
noremap 9 9gt
noremap 0 :tablast<CR>
noremap <C-n> :tabnew<CR>
noremap <tab> gt
noremap <S-tab> gT