其它综合

关注公众号 jb51net

关闭
首页 > 网络编程 > 其它综合 > Vim配置详解

Vim配置完整示例详解

作者:Varden

这篇文章主要介绍了Vim配置的相关资料,包括通用配置,常用插件配置,设置插件的方法,本文给大家讲解的非常详细,需要的朋友可以参考

1. 通用配置

在用户家目录下创建配置文件~/.vimrc,通用配置如下:

"===通用配置==="
"去掉vi的一致性"
set nocompatible
"显示行号"
"set number
"隐藏滚动条"
set guioptions-=r 
set guioptions-=L
set guioptions-=b
"隐藏顶部标签栏"
set showtabline=0
"设置字体"
set guifont=Monaco:h13
"开启语法高亮"
syntax on
"solarized主题设置在终端下的设置"
let g:solarized_termcolors=256
"设置背景色"
set background=dark
"颜色主题"
colorscheme solarized
"设置不折行"
set nowrap
"设置以unix的格式保存文件"
set fileformat=unix
"设置C样式的缩进格式"
set cindent
"设置tab长度"
set tabstop=4
set shiftwidth=4
"显示匹配的括号"
set showmatch
"距离顶部和底部5行"
set scrolloff=5
"命令行为两行"
set laststatus=2
"文件编码"
set fenc=utf-8
set backspace=2
"启用鼠标"
set mouse=a
set selection=exclusive
set selectmode=mouse,key
set matchtime=5
"忽略大小写"
set ignorecase
set incsearch
"高亮搜索项"
set hlsearch
"不允许扩展tab"
set noexpandtab
set whichwrap+=<,>,h,l
set autoread
"突出显示当前行"
"set cursorline
"突出显示当前列"
set cursorcolumn
"按F9进入粘贴模式"
set nopaste
set pastetoggle=<F9>

2. 常用插件配置

2.1 环境说明

Ubuntu 18.04.4 LTS
Vim 8.0
Python 3.6.7

2.2 安装并设置Vundle核心插件

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

编辑~/.vimrc文件,添加以下内容:

"===设置Vundle核心插件==="
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"安装的插件列表"
"核心插件,用于安装其他所有插件"
Plugin 'VundleVim/Vundle.vim'
"美化状态栏"
Plugin 'powerline/powerline'
"类似IDE的补全插件"
Plugin 'ycm-core/YouCompleteMe'
"添加一个树形目录"
Plugin 'preservim/nerdtree'
"缩进指示线"
Plugin 'Yggdroot/indentLine'
"自动格式化代码"
Plugin 'tell-k/vim-autopep8'
"括号和引号自动补全"
Plugin 'jiangmiao/auto-pairs'
"多行注释"
Plugin 'preservim/nerdcommenter'
"实时语法检查"
Plugin 'vim-syntastic/syntastic'
"美化状态栏"
Plugin 'Lokaltog/vim-powerline'
call vundle#end()
"开启文件类型自动检测,编写代码时自动换行对齐"
filetype plugin indent on

保存文件并使用以下命令安装插件:

:PluginInstall

2.3 编译YouCompleteMe插件

apt-get install build-essential cmake
apt-get install python3-dev
apt-get install python-dev  ##可选,最新版Debian系统bullseye(testing版本)已经移除python2,只支持python3
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
./install.py

遇到的问题:

YouCompleteMe unavailable: requires Vim 8.1.2269+.

Vim提示以上错误,原因是当前最新版的YouCompleteMe插件要求Vim的版本为8.1.2269+,必须升级Vim,Debian 10自带的Vim版本达不到要求。
源码编译安装Vim太麻烦,可以考虑使用Debian bullseye(testing版本)自带的Vim 8.2以上的版本。

The ycmd server SHUT DOWN (restart with ':YcmRestartServer'). Unexpected error while loading the YCM core library. Type ':YcmToggleLogs ycmd_46199_stderr_zazh98c3.log' to check the logs.

  File "/root/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/utils.py", line 498, in ImportAndCheckCore
    ycm_core = ImportCore()
  File "/root/.vim/bundle/YouCompleteMe/third_party/ycmd/ycmd/utils.py", line 489, in ImportCore
    import ycm_core as ycm_core
ImportError: Python version mismatch: module was compiled for Python 3.7, but the interpreter version is incompatible: 3.8.5 (default, Aug  2 2020, 15:09:07)
[GCC 10.2.0].

Vim提示以上错误,原因是Python版本从3.7升级到了3.8,原来编译的YouCompleteMe插件不兼容Python 3.8,必须重新基于3.8版本编译此插件。

3. 设置插件

3.1 设置一键运行Python代码(可用)

"===设置按F5运行Python==="
map <F5> :Autopep8<CR> :w<CR> :call RunPython()<CR>
function RunPython()
    let mp = &makeprg
    let ef = &errorformat
    let exeFile = expand("%:t")
    setlocal makeprg=python3\ -u
    set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
    silent make %
    copen
    let &makeprg = mp
    let &errorformat = ef
endfunction

必须安装依赖包:

pip3 install autopep8

3.2 设置YouCompleteMe插件(可用)

"===设置YouCompleteMe插件==="
"默认配置文件路径"
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
"打开vim时不再询问是否加载ycm_extra_conf.py配置"
let g:ycm_confirm_extra_conf=0
set completeopt=longest,menu
"Python解释器路径"
let g:ycm_path_to_python_interpreter='/usr/bin/python3'
"是否开启语义补全"
let g:ycm_seed_identifiers_with_syntax=1
"是否在注释中也开启补全"
let g:ycm_complete_in_comments=1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
"开始补全的字符数"
let g:ycm_min_num_of_chars_for_completion=2
"补全后自动关机预览窗口"
let g:ycm_autoclose_preview_window_after_completion=1
"禁止缓存匹配项,每次都重新生成匹配项"
let g:ycm_cache_omnifunc=0
"字符串中也开启补全"
let g:ycm_complete_in_strings = 1
"离开插入模式后自动关闭预览窗口"
autocmd InsertLeave * if pumvisible() == 0|pclose|endif

debian 10自带vim不支持python问题,vim打开文件会有以下提示:

YouCompleteMe unavailable: requires Vim compiled with Python (3.6.0+) support. Press ENTER or type command to continue

必须安装以下依赖包:

apt-get install vim-gtk

3.3 设置NERDTree插件(可用)

"===设置NERDTree插件==="
"F2开启和关闭树"
map <F2> :NERDTreeToggle<CR>
let NERDTreeChDirMode=1
"显示书签"
let NERDTreeShowBookmarks=1
"设置忽略文件类型"
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
"窗口大小"
let NERDTreeWinSize=25

3.4 设置indentLine插件(可用)

"===设置indentLine插件==="
"缩进指示线"
let g:indentLine_char='┆'
let g:indentLine_enabled = 1

3.5 设置autopep8插件(可用)

"===设置autopep8插件==="
let g:autopep8_disable_show_diff=1

3.6 设置nerdcommenter插件(可用)

"nerdcommenter的leader默认为\,可以用下面的命令更改"
let mapleader=','
"在normal模式下按v并移动光标选择需要注释的行,再按F4就可以为所有选中的行添加注释"
map <F4> <leader>ci <CR>

4. 完整配置示例

"===通用配置==="
"去掉vi的一致性"
set nocompatible
"显示行号"
"set number
"隐藏滚动条"
set guioptions-=r
set guioptions-=L
set guioptions-=b
"隐藏顶部标签栏"
set showtabline=0
"设置字体"
set guifont=Monaco:h13
"开启语法高亮"
syntax on
"solarized主题设置在终端下的设置"
let g:solarized_termcolors=256
"设置背景色"
set background=dark
"设置不折行"
set nowrap
"设置以unix的格式保存文件"
set fileformat=unix
"设置C样式的缩进格式"
set cindent
"设置tab长度"
set tabstop=4
set shiftwidth=4
"显示匹配的括号"
set showmatch
"距离顶部和底部5行"
set scrolloff=5
"命令行为两行"
set laststatus=2
"文件编码"
set fenc=utf-8
set backspace=2
"忽略大小写"
set ignorecase
set incsearch
"高亮搜索项"
set hlsearch
"不允许扩展tab"
set noexpandtab
set whichwrap+=<,>,h,l
set autoread
"突出显示当前行"
"set cursorline
"突出显示当前列"
set cursorcolumn
"按F9进入粘贴模式"
set nopaste
set pastetoggle=<F9>
"===设置Vundle核心插件==="
filetype off
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
"安装的插件列表"
Plugin 'VundleVim/Vundle.vim'
Plugin 'powerline/powerline'
Plugin 'ycm-core/YouCompleteMe'
Plugin 'preservim/nerdtree'
Plugin 'Yggdroot/indentLine'
Plugin 'tell-k/vim-autopep8'
Plugin 'jiangmiao/auto-pairs'
Plugin 'preservim/nerdcommenter'
Plugin 'vim-syntastic/syntastic'
Plugin 'Lokaltog/vim-powerline'
call vundle#end()
"开启文件类型自动检测,编写代码时自动换行对齐"
filetype plugin indent on
"===设置按F5运行Python==="
map <F5> :Autopep8<CR> :w<CR> :call RunPython()<CR>
function RunPython()
    let mp = &makeprg
    let ef = &errorformat
    let exeFile = expand("%:t")
    setlocal makeprg=python3\ -u
    set efm=%C\ %.%#,%A\ \ File\ \"%f\"\\,\ line\ %l%.%#,%Z%[%^\ ]%\\@=%m
    silent make %
    copen
    let &makeprg = mp
    let &errorformat = ef
endfunction
"===设置YouCompleteMe插件==="
"默认配置文件路径"
let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py'
"打开vim时不再询问是否加载ycm_extra_conf.py配置"
let g:ycm_confirm_extra_conf=0
set completeopt=longest,menu
"Python解释器路径"
let g:ycm_path_to_python_interpreter='/usr/bin/python3'
"是否开启语义补全"
let g:ycm_seed_identifiers_with_syntax=1
"是否在注释中也开启补全"
let g:ycm_complete_in_comments=1
let g:ycm_collect_identifiers_from_comments_and_strings = 0
"开始补全的字符数"
let g:ycm_min_num_of_chars_for_completion=2
"补全后自动关机预览窗口"
let g:ycm_autoclose_preview_window_after_completion=1
"禁止缓存匹配项,每次都重新生成匹配项"
let g:ycm_cache_omnifunc=0
"字符串中也开启补全"
let g:ycm_complete_in_strings = 1
"离开插入模式后自动关闭预览窗口"
autocmd InsertLeave * if pumvisible() == 0|pclose|endif
"===设置NERDTree插件==="
"F2开启和关闭树"
map <F2> :NERDTreeToggle<CR>
let NERDTreeChDirMode=1
"显示书签"
let NERDTreeShowBookmarks=1
"设置忽略文件类型"
let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$']
"窗口大小"
let NERDTreeWinSize=25
"===设置indentLine插件==="
"缩进指示线"
let g:indentLine_char='┆'
let g:indentLine_enabled = 1
"===设置autopep8插件==="
let g:autopep8_disable_show_diff=1
"===设置nerdcommenter插件==="
"nerdcommenter的leader默认为\,可以用下面的命令更改"
let mapleader=','
"在normal模式下按v并移动光标选择需要注释的行,再按F4就可以为所有选中的行添加注释"
map <F4> <leader>ci <CR>

Debian的vim右键进入visual模式:

Debian默认装好vim之后,右键不能粘贴,反而进入了visual模式,甚是恼人,可通过如下方法修改。

vim版本:version 8.1.0875

修改方法:

编辑/usr/share/vim/vim81/defaults.vim文件,定位到第79行,在mouse=a的=前面加个-,如下:

if has('mouse')
    set mouse-=a
endif

保存退出即可生效。

到此这篇关于Vim配置详解的文章就介绍到这了,更多相关Vim配置详解内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文