vim 填坑之路

1,512 阅读4分钟

vim-plug

用来管理vim的插件,和bundle很相似,我是新手,没有区别

GitHub

  1. you can install plug.vim by command curl
  2. you also can download plug.vim then push it on ~/.vim/autoload

YouCompleteMe

GitHub

  1. 在~/.vimrc下加入配置插件文件
call plug#begin('~/.vim/plugged')

" Make sure you use single quotes

" Shorthand notation; fetches https://github.com/junegunn/vim-easy-align
Plug 'junegunn/vim-easy-align'

" Any valid git URL is allowed
Plug 'https://github.com/junegunn/vim-github-dashboard.git'

" Multiple Plug commands can be written in a single line using | separators
Plug 'SirVer/ultisnips' | Plug 'honza/vim-snippets'

" On-demand loading
Plug 'scrooloose/nerdtree', { 'on':  'NERDTreeToggle' }

Plug 'tpope/vim-fireplace', { 'for': 'clojure' }

" Using a non-master branch
Plug 'rdnetto/YCM-Generator', { 'branch': 'stable' }

" Using a tagged release; wildcard allowed (requires git 1.9.2 or above) 
Plug 'fatih/vim-go', { 'tag': '*' }

" Plugin options
Plug 'nsf/gocode', { 'tag': 'v.20150303', 'rtp': 'vim' }

" Plugin outside ~/.vim/plugged with post-update hook
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
  
" Unmanaged plugin (manually installed and updated)
Plug '~/my-prototype-plugin'

Plug 'Valloric/YouCompleteMe'
" Initialize plugin system
call plug#end()

  1. 打开vim,输入:PlugInstall,安装各个插件
    1. 其中YouCompleteMe中/third_party中有一个子git项目,需要单独clone,如果像我司clone不方便的话需要手动导入
    2. ubuntu上clone该子项目的时候git可能会报错
    error: RPC failed; curl 56 GnuTLS recv error (-110): The TLS connection was non-properly terminated.
    
    此情况是由于Git不支持openssl,需要重新手动编译,编译方法如下
    sudo apt-get install build-essential fakeroot dpkg-dev -y
    sudo apt-get build-dep git -y
    sudo apt-get install libcurl4-openssl-dev -y
    cd ~
    mkdir source-git
    cd source-git/
    apt-get source git
    cd git-2.*.*/
    sed -i -- 's/libcurl4-gnutls-dev/libcurl4-openssl-dev/' ./debian/control
    sed -i -- '/TEST\s*=\s*test/d' ./debian/rules
    dpkg-buildpackage -rfakeroot -b -uc -us
    sudo dpkg -i ../git_*ubuntu*.deb
    
  2. 执行./install.py --clang-completer的时候可能会出现libclang-6.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.bz2该文件MD5值不对,于是手动下载,存放的目录在/third_party/ycmd/clang_archive下,是把整个压缩包放入,install过程中会自己解压。
  3. 执行cmake的两条命令,参考github。其中如果需要自动补全需要LLVM,需要去官网下载LLVM的二进制文件。
  4. 拷贝/third_party/ycmd/.ycm_extra_conf.pyYouCompleteMe/cpp/ycmd/目录下(补充:其实拷贝到哪儿都可以,只需要在~/.vimrc文件中指定路径即可)
  5. .ycm_extra_conf.py文件中需要在在 flag=[*] 里加入
'-isystem',
'/usr/include'
  1. ~/.vimrc中加入
let g:ycm_server_python_interpreter='/usr/bin/python'
let g:ycm_global_ycm_extra_conf='~/.vim/plugged/YouCompleteMe/cpp/ycmd/.ycm_extra_conf.py'
let g:ycm_semantic_triggers =  {
  \   'c' : ['->', '.'],
  \   'objc' : ['->', '.', 're!\[[_a-zA-Z]+\w*\s', 're!^\s*[^\W\d]\w*\s',
  \             're!\[.*\]\s'],
  \   'ocaml' : ['.', '#'],
  \   'cpp,objcpp' : ['->', '.', '::'],
  \   'perl' : ['->'],
  \   'php' : ['->', '::'],
  \   'cs,java,javascript,typescript,d,python,perl6,scala,vb,elixir,go' : ['.'],
  \   'ruby' : ['.', '::'],
  \   'lua' : ['.', ':'],
  \   'erlang' : [':'],
  \ }
let g:ycm_collect_identifiers_from_tags_files=1
let g:ycm_min_num_of_chars_for_completion=3
let g:ycm_seed_identifiers_with_syntax=1

" YouCompleteMe 功能
" 补全功能在注释中同样有效
let g:ycm_complete_in_comments=0
" 允许 vim 加载 .ycm_extra_conf.py 文件,不再提示
let g:ycm_confirm_extra_conf=0
" 开启 YCM 基于标签引擎
let g:ycm_collect_identifiers_from_tags_files=1
" 引入 C++ 标准库tags,这个没有也没关系,只要.ycm_extra_conf.py文件中指定了正确的标准库路径
set tags+=/data/misc/software/misc./vim/stdcpp.tags
" YCM 集成 OmniCppComplete 补全引擎,设置其快捷键
inoremap <leader>; <C-x><C-o>
" 补全内容不以分割子窗口形式出现,只显示补全列表
set completeopt-=preview
" 从第一个键入字符就开始罗列匹配项
let g:ycm_min_num_of_chars_for_completion=1
" 禁止缓存匹配项,每次都重新生成匹配项
let g:ycm_cache_omnifunc=0
" 语法关键字补全
let g:ycm_seed_identifiers_with_syntax=1
" 修改对C函数的补全快捷键,默认是CTRL + space,修改为ALT + ;
let g:ycm_key_invoke_completion = '<M-;>'
" 设置转到定义处的快捷键为ALT + G,这个功能非常赞
nmap <M-g> :YcmCompleter GoToDefinitionElseDeclaration <C-R>=expand("<cword>")<CR><CR>
" 设置按哪个键上屏
let g:ycm_key_list_select_completion = ['<TAB>', '<Down>', '<Enter>']

YCM中容易忽略的配置

vimrc

这是vim的配置文件,不需要执行source ~/.vimrc来执行

这是我本机的一些配置

syntax on "语法高亮
colorscheme ron " elflord ron peachpuff default 设置配色方案,vim自带的配色方案保存在/usr/share/vim/vim72/colors目录下

" detect file type
filetype on
filetype plugin on

" If using a dark background within the editing area and syntax highlighting
" turn on this option as well
set background=dark

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
"have Vim load indentation rules and plugins according to the detected filetype
filetype plugin indent on
endif

set number " 显示行号
"set ignorecase " 搜索模式里忽略大小写
"set smartcase " 如果搜索模式包含大写字符,不使用 'ignorecase' 选项。只有在输入搜索模式并且打开 'ignorecase' 选项时才会使用。
set autowrite " 自动把内容写回文件
set autoindent " 设置自动对齐(缩进):即每行的缩进值与上一行相等;使用 noautoindent 取消设置
"set smartindent " 智能对齐方式
set tabstop=4 " 设置制表符(tab键)的宽度
set softtabstop=4 " 设置软制表符的宽度
set shiftwidth=4 " (自动) 缩进使用的4个空格
set cindent " 使用 C/C++ 语言的自动缩进方式
set cinoptions={0,1s,t0,n-2,p2s,(03s,=.5s,>1s,=1s,:1s "设置C/C++语言的具体缩进方式
"set backspace=2 " 设置退格键可用
set showmatch " 设置匹配模式
set ruler " 打开状态栏标尺
set wrap 
set incsearch 
"set relativenumber 
set showcmd 

vim 配置文件.vimrc

Vim学习

space-vim

Github

spf13-vim

Github

知乎

有哪些编程必备的 Vim 配置 如何在 Linux 下利用 Vim 搭建 C/C++ 开发环境?