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
保存退出即可生效。
