推荐一下我的vim配置

  • ACG

    最终效果图:
    QQ截图20200912163204.jpg
    一入vim深似海 从此gui是路人


    0. 安装vim

    想要配置vim,首先肯定需要有vim这个软件。
    鉴于vim的强大和高效,很多系统都支持他,例如Windows,Mac OS,Linux,甚至OpenWrt。
    安装方法也很简单

    Windows (gvim)

    下载安装包安装

    https://github.com/vim/vim-win32-installer/releases

    或者,直接通过win-get也可以进行安装

    winget install vim
    

    Mac OS

    略(我没用过Mac,但是听说有些Mac程序员也喜欢用vim,所以明显Mac也是支持的)

    Linux

    一般发行版的仓库都会内置vim,直接用软件包管理程序安装即可

    Debian系

    sudo apt install vim
    

    RH系

    sudo yum install vim
    

    1. 安装并配置Vundle

    Vundle是一款用来管理vim插件的插件,而且可以通过git更新插件

    1.1 安装git

    和安装vim的方法一样,例如Debian系可以通过sudo apt install git来进行安装

    1.2 下载Vundle

    通过git进行克隆

    git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
    

    1.3 配置插件

    首先,需要将以下配置加入~/.vimrc开头

    set nocompatible              " be iMproved, required
    filetype off                  " required
    
    " set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()
    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')
    
    " let Vundle manage Vundle, required
    Plugin 'VundleVim/Vundle.vim'
    
    " The following are examples of different formats supported.
    " Keep Plugin commands between vundle#begin/end.
    " plugin on GitHub repo
    Plugin 'tpope/vim-fugitive'
    " plugin from http://vim-scripts.org/vim/scripts.html
    " Plugin 'L9'
    " Git plugin not hosted on GitHub
    Plugin 'git://git.wincent.com/command-t.git'
    " git repos on your local machine (i.e. when working on your own plugin)
    Plugin 'file:///home/gmarik/path/to/plugin'
    " The sparkup vim script is in a subdirectory of this repo called vim.
    " Pass the path to set the runtimepath properly.
    Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
    " Install L9 and avoid a Naming conflict if you've already installed a
    " different version somewhere else.
    " Plugin 'ascenator/L9', {'name': 'newL9'}
    
    " All of your Plugins must be added before the following line
    call vundle#end()            " required
    filetype plugin indent on    " required
    " To ignore plugin indent changes, instead use:
    "filetype plugin on
    "
    " Brief help
    " :PluginList       - lists configured plugins
    " :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
    " :PluginSearch foo - searches for foo; append `!` to refresh local cache
    " :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
    "
    " see :h vundle for more details or wiki for FAQ
    " Put your non-Plugin stuff after this line
    

    之后,就可以在call vundle#begin()call vundle#end()之间添加你想安装的插件了
    这里推荐2个插件。valloric/YouCompleteMe,可以用来代码补全。tomassiser/vim-code-dark,vscode的暗色主题

    1.4 插件安装

    vim +PluginInstall +qall
    

    *1.5 编译安装YouCompleteMe

    python3 ~/.vim/bundle/YouCompleteMe/install.py --all
    python3 ~/.vim/bundle/YouCompleteMe/install.py --clangd-completer
    

    *1.6 配置YouCompleteMe

    .vimrc文件后面追加内容:

    let g:ycm_key_invoke_completion = '<c-z>'
    set completeopt=menu,menuone
    let g:ycm_add_preview_to_completeopt = 0
    

    可以将语义补全的快捷键修改为Ctrl + z,默认的Ctrl + space是切换输入法。
    可以在切换补全的时候禁用函数原型展示,避免扰乱布局

    2. 配置主题

    set t_Co=256 " 需要终端支持256色
    set t_ut=
    colorsheme codedark
    

    3. 配置状态栏

    3.1 状态栏常驻显示

    set laststatus=2
    

    3.2 获取git branch

    function! GitBranch()
        return system("git rev-parse --abbrev-ref HEAD 2>/dev/null | tr -d '\n'")
    endfunction
    
    function! GetBranch()
        let l:branchname = GitBranch()
        return strlen(l:branchname) > 0? '  '.l:branchname.' ': ''
    endfunction
    

    3.3 自定义状态栏

    set statusline=
    set statusline+=%#PmenuSel#
    set statusline+=%{GetBranch()}
    set statusline+=%#LineNr#
    set statusline+=\ %F
    set statusline+=%m
    set statusline+=%=
    set statusline+=%#CursorColumn#
    set statusline+=\ %y
    set statusline+=\ %{&fileencoding?&fileencoding:&encoding}
    set statusline+=[%{&fileformat}]
    set statusline+=\ %p%%
    set statusline+=\ %l:%c
    

    显示出来的效果是 branch - 路径 - 类型 - 编码 - 行尾 - 百分比 - 行:列

    3.4 行号等杂项

    set nowrap " 取消自动折行
    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab " 将tab键切换为4个空格
    set nu " 显示行号
    set smartindent " 智能缩进
    set mouse=a " 支持鼠标
    set cursorline " 高亮当前行
    set cc=81 " 高亮第81列(在行长度超过80时起到提示作用,避免写出不便阅读的代码)
    

    4. 配置符号配对

    其实思路就是将一个按键映射成多个按键

    inoremap ' ''<ESC>i
    inoremap " ""<ESC>i
    inoremap ( ()<ESC>i
    inoremap [ []<ESC>i
    inoremap ` ``<ESC>i
    inoremap { {}<ESC>i
    

    不建议配置尖括号的配对,尖括号只有在泛型和C/C++引入头文件时才需要配对,这种场合相对来说较少。
    但是,当你写代码需要用到小于号时,你就会发现你配置的尖括号补全有多艹蛋了...

  • ACG

    @Bruce 用插件tpope/vim-fugitivevim-airline/vim-airline替换了状态栏,现在省去了一堆函数和状态栏配置
    vim效果图.jpg
    现在的最新配置是:

    set nocompatible              " be iMproved, required
    filetype off                  " required
    
    " set the runtime path to include Vundle and initialize
    set rtp+=~/.vim/bundle/Vundle.vim
    call vundle#begin()
    " alternatively, pass a path where Vundle should install plugins
    "call vundle#begin('~/some/path/here')
    
    " let Vundle manage Vundle, required
    Plugin 'VundleVim/Vundle.vim'
    
    " The following are examples of different formats supported.
    " Keep Plugin commands between vundle#begin/end.
    
    " plugin on GitHub repo
    " Plugin 'tpope/vim-fugitive'
    
    " plugin from http://vim-scripts.org/vim/scripts.html
    " Plugin 'L9'
    
    " Git plugin not hosted on GitHub
    " Plugin 'git://git.wincent.com/command-t.git'
    
    " git repos on your local machine (i.e. when working on your own plugin)
    " Plugin 'file:///home/gmarik/path/to/plugin'
    " The sparkup vim script is in a subdirectory of this repo called vim.
    " Pass the path to set the runtimepath properly.
    
    " Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
    " Install L9 and avoid a Naming conflict if you've already installed a
    " different version somewhere else.
    " Plugin 'ascenator/L9', {'name': 'newL9'}
    
    Plugin 'Valloric/YouCompleteMe'
    Plugin 'tomasiser/vim-code-dark'
    Plugin 'tpope/vim-fugitive'
    Plugin 'vim-airline/vim-airline'
    
    " All of your Plugins must be added before the following line
    call vundle#end()            " required
    filetype plugin indent on    " required
    " To ignore plugin indent changes, instead use:
    "filetype plugin on
    "
    " Brief help
    " :PluginList       - lists configured plugins
    " :PluginInstall    - installs plugins; append `!` to update or just :PluginUpdate
    " :PluginSearch foo - searches for foo; append `!` to refresh local cache
    " :PluginClean      - confirms removal of unused plugins; append `!` to auto-approve removal
    "
    " see :h vundle for more details or wiki for FAQ
    " Put your non-Plugin stuff after this line
    
    set t_Co=256
    set t_ut=
    colorscheme codedark
    set laststatus=2
    set nowrap
    
    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
    set nu
    set smartindent
    
    set cursorline
    set cc=81
    
    inoremap ' ''<ESC>i
    inoremap " ""<ESC>i
    inoremap ( ()<ESC>i
    inoremap [ []<ESC>i
    " inoremap < <><ESC>i
    inoremap ` ``<ESC>i
    inoremap { {}<ESC>i
    
    let g:ycm_key_invoke_completion='<c-z>'
    set completeopt=menu,menuone
    let g:ycm_add_preview_to_completeopt=0
    let g:airline#extensions#hunks#enabled=0
    let g:airline#extensions#branch#enabled=1
    
    if !exists('g:airline_symbols')
        let g:airline_symbols = {}
    endif
    let g:airline_symbols.space = "\ua0"
    
    
  • ACG

    @Bruce 持续折腾中...把万年不更新的Vundle换成了vim-plug

    • 使用新的vim-plug进行插件管理
    • 使用coc.nvim以及各种插件进行代码补全
      8WFR_BBO@PJ_XV0DLY_U_A8.png
    set nocompatible
    
    call plug#begin('~/.vim/plugged')
    
    Plug 'neoclide/coc.nvim', {'branch': 'release'}
    Plug 'tomasiser/vim-code-dark'
    Plug 'tpope/vim-fugitive'
    Plug 'vim-airline/vim-airline'
    Plug 'honza/vim-snippets'
    Plug 'octol/vim-cpp-enhanced-highlight'
    
    " All of your Plugins must be added before the following line
    call plug#end()
    
    let g:coc_global_extensions=['coc-cmake', 'coc-json', 'coc-python', 'coc-html', 'coc-tsserver', 'coc-snippets', 'coc-highlight']
    
    let g:cpp_class_scope_highlight=1
    let g:cpp_member_variable_highlight=1
    let g:cpp_class_decl_highlight=1
    let g:cpp_posix_standard=1
    let g:cpp_experimental_simple_template_highlight=1
    let g:cpp_concepts_highlight=1
    
    " coc completion settings
    inoremap <silent><expr> <TAB>
        \ pumvisible()? coc#_select_confirm() :
        \ coc#expandableOrJumpable() ? "\<C-r>=coc#rpc#request('doKeymap', ['snippets-expand-jump',''])\<CR>" :
        \ <SID>check_back_space()? "\<TAB>" :
        \ coc#refresh()
    
    function! s:check_back_space() abort
        let col = col('.') - 1
        return !col || getline('.')[col - 1] =~# '\s'
    endfunction
    
    let g:coc_snippet_next='<tab>'
    
    filetype plugin indent on
    
    set t_Co=256
    set t_ut=
    colorscheme codedark
    set laststatus=2
    set nowrap
    
    set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab
    set nu
    set smartindent
    
    set cursorline
    set cc=81
    
    inoremap ' ''<ESC>i
    inoremap " ""<ESC>i
    inoremap ( ()<ESC>i
    inoremap [ []<ESC>i
    " inoremap < <><ESC>i
    inoremap ` ``<ESC>i
    inoremap { {}<ESC>i
    
    let g:airline#extensions#hunks#enabled=0
    let g:airline#extensions#branch#enabled=1
    
    if !exists('g:airline_symbols')
        let g:airline_symbols = {}
    endif
    let g:airline_symbols.space = "\ua0"