Vim折腾笔记(七) | CentOS7中配置C/C++环境的Vim8

Ricsy · · 2024 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。



安装最新版vim

  • 增加最新版vim源
    curl -L https://copr.fedorainfracloud.org/coprs/lantw44/vim-latest/repo/epel-7/lantw44-vim-latest-epel-7.repo -o /etc/yum.repos.d/vim-latest.repo
  • 更新vim
    yum update -y
  • 验证vim
    vim
  • 在vim中输入命令:echo $VIMRUNTIME来查看Vim的运行目录

参阅:


配置最新版vim

  • 创建用户的.vim文件夹
    mkdir ~/.vim
  • 创建用户的.vimrc文件
    touch ~/.vimrc
    创建vim的主题文件夹
    mkdir -p ~/.vim/colors

配色方案

  • 安装gruvbox配色方案
    curl -fLo ~/.vim/colors/gruvbox.vim --create-dirs https://raw.githubusercontent.com/morhetz/gruvbox/master/colors/gruvbox.vim
  • 配置用户的vim
    vim ~/.vimrc
" gruvbox配色方案
colorscheme gruvbox

参阅:


插件管理


  • 安装开发工具
    yum grouplist
    yum groupinstall "开发工具" -yyum groupinstall Development tools -y
  • 安装部分依赖
    yum install make cmake gcc gcc-c++ python-devel clang kernel-devel ncurses-devel yum-utils epel-release -y
    yum install https://centos7.iuscommunity.org/ius-release.rpm -y
  • 安装python3和pip3

yum search python3 | grep devel

yum install python36u python36u-devel python36u-pip -y

  • 创建python3连接符
    ln -s /usr/bin/python3.6 /usr/bin/python3
  • 创建pip3链接符
    ln -s /usr/bin/pip3.6 /usr/bin/pip3
  • 配置pip3
    mkdir ~/.pip && cd ~/.pip
    vim pip.conf
[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host = pypi.douban.com
  • 版本查看
    python -V
    python3 -V
    pip3 -V
  • 升级pip3
    pip3 install --upgrade pip

  • 安装vim_runtime配置方案
    git clone --depth=1 https://github.com/amix/vimrc.git ~/.vim_runtime
    sh ~/.vim_runtime/install_awesome_vimrc.sh
  • 下载plug.vim(vim插件管理器)
    curl -fLo ~/.vim/autoload/plug.vim --create-dirs https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  • 配置用户的vim
    vim ~/.vimrc
" F11快捷键激活/取消paste模式
set pastetoggle=<F11>
  • 配置用户的vim
    vim ~/.vimrc
"===========================================================
call plug#begin()

" 目录结构展示插件
Plug 'scrooloose/nerdtree'
Plug 'Xuyuanp/nerdtree-git-plugin'

" 代码自动补全
Plug 'ycm-core/YouCompleteMe'

" 括号自动补全
Plug 'jiangmiao/auto-pairs'

" 你所有的插件必需在这一行之前添加
call plug#end()            "required

" --------------------------------------------------------------

" vim 启动的时候默认开启 NERDTree
autocmd VimEnter * NERDTree
" 默认光标在右侧文件编辑区 
wincmd w
autocmd VimEnter * wincmd w
" 文件树设置 {{{
" NERDTree开关
map <silent> <F3> :NERDTreeToggle<CR>
""当NERDTree为剩下的唯一窗口时自动关闭
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
"" 修改树的显示图标
let g:NERDTreeDirArrowExpandable = '►'
let g:NERDTreeDirArrowCollapsible = '▼'
let NERDTreeAutoCenter=1
" 指定鼠标模式(1.双击打开;2.单目录双文件;3.单击打开)
let NERDTreeMouseMode=2
" 将 NERDTree 的窗口设置在 vim 窗口的左侧
let NERDTreeWinPos="left"
" 显示行号
let NERDTreeShowLineNumbers=1
" 是否显示隐藏文件
let NERDTreeShowHidden=1
" 设置宽度
let NERDTreeWinSize=25
" 在终端启动vim时,共享NERDTree
let g:nerdtree_tabs_open_on_console_startup=1
" 忽略一下文件的显示
let NERDTreeIgnore=['\.pyc','\~$','\.swp']
let g:NERDTreeIndicatorMapCustom = {
    \ "Modified"  : "✹",
    \ "Staged"    : "✚",
    \ "Untracked" : "✭",
    \ "Renamed"   : "➜",
    \ "Unmerged"  : "═",
    \ "Deleted"   : "✖",
    \ "Dirty"     : "✗",
    \ "Clean"     : "✔︎",
    \ 'Ignored'   : '☒',
    \ "Unknown"   : "?"
    \ }
" }}}
" --------------------------------------------------------------

let g:ycm_global_ycm_extra_conf='~/.ycm_extra_conf.py'

" 打开vim时不再询问是否加载ycm_extra_conf.py配置
let g:ycm_confirm_extra_conf=0

let g:ycm_python_binary_path = 'python3'
let g:ycm_autoclose_preview_window_after_completion = 1

” 在注释输入中也能补全
let g:ycm_complete_in_comments = 1  
" 在字符串输入中也能补全
let g:ycm_complete_in_strings = 1   
" 开启 YCM 基于标签引擎
let g:ycm_collect_identifiers_from_tags_files=1
" 注释和字符串中的文字也会被收入补全
let g:ycm_collect_identifiers_from_comments_and_strings = 1  

" 语言关键字补全, 不过python关键字都很短,所以,需要的自己打开 
let g:ycm_seed_identifiers_with_syntax=1
let g:ycm_collect_identifiers_from_tags_files = 1
" 从第2个键入字符就开始罗列匹配项
let g:ycm_min_num_of_chars_for_completion=2       
              
"mapping
nmap <leader>gd :YcmDiags<CR>
" 跳转到申明处
nnoremap <leader>gl :YcmCompleter GoToDeclaration<CR>   
" 跳转到定义处       
nnoremap <leader>gf :YcmCompleter GoToDefinition<CR>            
nnoremap <leader>gg :YcmCompleter GoToDefinitionElseDeclaration<CR>

" 对指定源文件,输入两个字母后即触发语义补全
let g:ycm_semantic_triggers =  {
           \ 'c,cpp,python,java,go,erlang,perl': ['re!\w{2}'],
           \ 'cs,lua,javascript': ['re!\w{2}'],
           \ }

let g:ycm_filetype_whitelist = { 
            \ "c":1,
            \ "cpp":1, 
            \ "go":1,
            \ "python":1,
            \ "sh":1,
            \ "zsh":1,
            \ }
" 黑名单,不启用
let g:ycm_filetype_blacklist = {
          \ 'tagbar' : 1,
          \ 'gitcommit' : 1,
          \ }
"===========================================================
set autoindent             " Indent according to previous line.
set expandtab              " Use spaces instead of tabs.
set softtabstop =4         " Tab key indents by 4 spaces.
set shiftwidth  =4         " >> indents by 4 spaces.
set shiftround             " >> indents to next multiple of 'shiftwidth'.

set hidden                 " Switch between buffers without having to save first.
set display     =lastline  " Show as much as possible of the last line.

set ttyfast                " Faster redrawing.
set lazyredraw             " Only redraw when necessary.

set splitbelow             " Open new windows below the current window.
set splitright             " Open new windows right of the current window.

set cursorline             " Find the current line quickly.
set wrapscan               " Searches wrap around end-of-file.
set report      =0         " Always report changed lines.
set synmaxcol   =120       " Only highlight the first 200 columns.

set list                   " Show non-printable characters.
if has('multi_byte') && &encoding ==# 'utf-8'
  let &listchars = 'tab:▸ ,extends:❯,precedes:❮,nbsp:±'
else
  let &listchars = 'tab:> ,extends:>,precedes:<,nbsp:.'
endif

" Put all temporary files under the same directory.
let s:vim_backup = expand("$HOME/.vim/files/backup/")
if !isdirectory(s:vim_backup)
   silent! call mkdir(s:vim_backup, 'p')
endif
let s:vim_swap = expand("$HOME/.vim/files/swap/")
if !isdirectory(s:vim_swap)
   silent! call mkdir(s:vim_swap, 'p')
endif
let s:vim_undo = expand("$HOME/.vim/files/undo/")
if !isdirectory(s:vim_undo)
   silent! call mkdir(s:vim_undo, 'p')
endif
let s:vim_info = expand("$HOME/.vim/files/info/")
if !isdirectory(s:vim_info)
   silent! call mkdir(s:vim_info, 'p')
endif
set backup
set backupdir   =$HOME/.vim/files/backup/
set backupext   =-vimbackup
set backupskip  =
set directory   =$HOME/.vim/files/swap/
set updatecount =100
set undofile
set undodir     =$HOME/.vim/files/undo/
set viminfo     ='100,n$HOME/.vim/files/info/viminfo
" 显示行号
set number
" 取消换行
set nowrap 
" 为方便复制,用<F6>开启/关闭行号显示
nnoremap <F6> :set nonumber!<CR>:set foldcolumn=0<CR>

" C,C++的调试
map <F8> :call Rungdb()<CR>
 func! Rungdb()
    exec "w"
    exec "!g++ % -g -o %<"
    exec "!gdb ./%<" endfunc

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 """""新文件标题
 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
 " 新建.c,.h,.sh,.java文件,自动插入文件头 
 autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" 
 "" 定义函数SetTitle,自动插入文件头 
 func SetTitle() 
     "如果文件类型为.sh文件 
     if &filetype == 'sh' 
     call setline(1,"\#########################################################################") 
     call append(line("."), "\# File Name: ".expand("%")) 
     call append(line(".")+1, "\# Author: xiehongfeng") 
     call append(line(".")+2, "\# mail: xiehongfeng100@qq.com") 
     call append(line(".")+3, "\# Created Time: ".strftime("%c")) 
     call append(line(".")+4, "\#########################################################################") 
     call append(line(".")+5, "\#!/bin/bash") 
     call append(line(".")+6, "") 
     else 
     call setline(1, "/*************************************************************************") 
     call append(line("."), "    > File Name: ".expand("%")) 
     call append(line(".")+1, "    > Author: xiehongfeng") 
     call append(line(".")+2, "    > Mail: xiehongfeng100@qq.com") 
     call append(line(".")+3, "    > Created Time: ".strftime("%c")) 
     call append(line(".")+4, " ************************************************************************/") 
     call append(line(".")+5, "")
     endif
     if &filetype == 'cpp'
     call append(line(".")+6, "#include <iostream>")
     call append(line(".")+7, "using namespace std;")
     call append(line(".")+8, "")
     endif
     if &filetype == 'c'
     call append(line(".")+6, "#include <stdio.h>")
     call append(line(".")+7, "")
     endif
     "    if &filetype == 'java'
     "        call append(line(".")+6,"public class ".expand("%"))
     "        call append(line(".")+7,"")
     "    endif
     "新建文件后,自动定位到文件末尾
     autocmd BufNewFile * normal G
 endfunc 
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  • 安装YouCompleteMe插件
    vim
    :PlugInstall

提示:

  • Q1:
  • A1:

mkdir -p ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/go/src/golang.org/x
cd ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/go/src/golang.org/x
git clone https://github.com/golang/tools.git

cd ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/third_party/go/src/golang.org/x
git clone https://github.com/golang/tools.git

cd ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe
git submodule update --init --recursive

  • 安装虚拟环境
    pip3 install virtualenv
    cd ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe
    virtualenv venv
    source venv/bin/activate

提示:

  • 退出venv
    deactivate
  • 支持C
    ./install.py --clang-completer
  • 新建项目文件夹
    cd ~ && mkdir mydata
    cd mydata
    virtualenv venv
    source venv/bin/activate
  • 保证cpp/ycm中有.ycm_extra_conf.py
    cp ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/ycm/
  • 安装ctags
    yum install ctags -y
    源码位置生成tags
    ctags -R *

参阅:

  • 编译YCM需要的ycm_support_libs库

cd ~
mkdir ycm_build
cd ycm_build

cmake -G "Unix Makefiles" . ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/cpp

wget http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-amd64-unknown-freebsd11.tar.xz
xz -d clang+llvm-8.0.0-amd64-unknown-freebsd11.tar.xz

mkdir -p ~/ycm_temp
tar -xvf clang+llvm-8.0.0-amd64-unknown-freebsd11.tar -C ~/ycm_temp

cd ~/ycm_temp
mv clang+llvm-8.0.0-amd64-unknown-freebsd11 clang_llvm8

cmake -G "Unix Makefiles" -DPATH_TO_LLVM_ROOT=~/ycm_temp/clang_llvm8 . ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/cpp

编译
cd ~/ycm_build && make
cd ~/ycm_temp/clang_llvm8 && make
cd ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe
./install.py --clang-completer

  • .ycm_extra_conf.py拷贝到~/下
cp ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/.ycm_extra_conf.py ~/.ycm_extra_conf.py
  • 配置YCM
    vim ~/.vim_runtime/sources_non_forked/ack.vim/plugged/YouCompleteMe/third_party/ycmd/cpp/ycm/.ycm_extra_conf.py
'~/ycm_temp/clang_llvm8/include',
'-isystem',
'~/ycm_temp/clang_llvm8/include/clang-c',
'-isystem',
'/usr/include',
'-isystem',
'/usr/include/c++/4.8.5',
'-isystem',
'/usr/include/c++/4.8.2',
'-isystem',

参阅:


更新中......



有疑问加站长微信联系(非本文作者)

本文来自:简书

感谢作者:Ricsy

查看原文:Vim折腾笔记(七) | CentOS7中配置C/C++环境的Vim8

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2024 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传