Vim配置完整示例詳解
1. 通用配置
在用戶家目錄下創(chuàng)建配置文件~/.vimrc,通用配置如下:
"===通用配置===" "去掉vi的一致性" set nocompatible "顯示行號(hào)" "set number "隱藏滾動(dòng)條" set guioptions-=r set guioptions-=L set guioptions-=b "隱藏頂部標(biāo)簽欄" set showtabline=0 "設(shè)置字體" set guifont=Monaco:h13 "開啟語法高亮" syntax on "solarized主題設(shè)置在終端下的設(shè)置" let g:solarized_termcolors=256 "設(shè)置背景色" set background=dark "顏色主題" colorscheme solarized "設(shè)置不折行" set nowrap "設(shè)置以u(píng)nix的格式保存文件" set fileformat=unix "設(shè)置C樣式的縮進(jìn)格式" set cindent "設(shè)置tab長度" set tabstop=4 set shiftwidth=4 "顯示匹配的括號(hào)" set showmatch "距離頂部和底部5行" set scrolloff=5 "命令行為兩行" set laststatus=2 "文件編碼" set fenc=utf-8 set backspace=2 "啟用鼠標(biāo)" set mouse=a set selection=exclusive set selectmode=mouse,key set matchtime=5 "忽略大小寫" set ignorecase set incsearch "高亮搜索項(xiàng)" set hlsearch "不允許擴(kuò)展tab" set noexpandtab set whichwrap+=<,>,h,l set autoread "突出顯示當(dāng)前行" "set cursorline "突出顯示當(dāng)前列" set cursorcolumn "按F9進(jìn)入粘貼模式" set nopaste set pastetoggle=<F9>
2. 常用插件配置
2.1 環(huán)境說明
Ubuntu 18.04.4 LTS
Vim 8.0
Python 3.6.7
2.2 安裝并設(shè)置Vundle核心插件
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
編輯~/.vimrc文件,添加以下內(nèi)容:
"===設(shè)置Vundle核心插件===" filetype off set rtp+=~/.vim/bundle/Vundle.vim call vundle#begin() "安裝的插件列表" "核心插件,用于安裝其他所有插件" Plugin 'VundleVim/Vundle.vim' "美化狀態(tài)欄" Plugin 'powerline/powerline' "類似IDE的補(bǔ)全插件" Plugin 'ycm-core/YouCompleteMe' "添加一個(gè)樹形目錄" Plugin 'preservim/nerdtree' "縮進(jìn)指示線" Plugin 'Yggdroot/indentLine' "自動(dòng)格式化代碼" Plugin 'tell-k/vim-autopep8' "括號(hào)和引號(hào)自動(dòng)補(bǔ)全" Plugin 'jiangmiao/auto-pairs' "多行注釋" Plugin 'preservim/nerdcommenter' "實(shí)時(shí)語法檢查" Plugin 'vim-syntastic/syntastic' "美化狀態(tài)欄" Plugin 'Lokaltog/vim-powerline' call vundle#end() "開啟文件類型自動(dòng)檢測,編寫代碼時(shí)自動(dòng)換行對齊" 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系統(tǒng)bullseye(testing版本)已經(jīng)移除python2,只支持python3 cd ~/.vim/bundle/YouCompleteMe git submodule update --init --recursive ./install.py
遇到的問題:
YouCompleteMe unavailable: requires Vim 8.1.2269+.
Vim提示以上錯(cuò)誤,原因是當(dāng)前最新版的YouCompleteMe插件要求Vim的版本為8.1.2269+,必須升級(jí)Vim,Debian 10自帶的Vim版本達(dá)不到要求。
源碼編譯安裝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提示以上錯(cuò)誤,原因是Python版本從3.7升級(jí)到了3.8,原來編譯的YouCompleteMe插件不兼容Python 3.8,必須重新基于3.8版本編譯此插件。
3. 設(shè)置插件
3.1 設(shè)置一鍵運(yùn)行Python代碼(可用)
"===設(shè)置按F5運(yùn)行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 設(shè)置YouCompleteMe插件(可用)
"===設(shè)置YouCompleteMe插件===" "默認(rèn)配置文件路徑" let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "打開vim時(shí)不再詢問是否加載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' "是否開啟語義補(bǔ)全" let g:ycm_seed_identifiers_with_syntax=1 "是否在注釋中也開啟補(bǔ)全" let g:ycm_complete_in_comments=1 let g:ycm_collect_identifiers_from_comments_and_strings = 0 "開始補(bǔ)全的字符數(shù)" let g:ycm_min_num_of_chars_for_completion=2 "補(bǔ)全后自動(dòng)關(guān)機(jī)預(yù)覽窗口" let g:ycm_autoclose_preview_window_after_completion=1 "禁止緩存匹配項(xiàng),每次都重新生成匹配項(xiàng)" let g:ycm_cache_omnifunc=0 "字符串中也開啟補(bǔ)全" let g:ycm_complete_in_strings = 1 "離開插入模式后自動(dòng)關(guān)閉預(yù)覽窗口" autocmd InsertLeave * if pumvisible() == 0|pclose|endif
debian 10自帶vim不支持python問題,vim打開文件會(huì)有以下提示:
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 設(shè)置NERDTree插件(可用)
"===設(shè)置NERDTree插件===" "F2開啟和關(guān)閉樹" map <F2> :NERDTreeToggle<CR> let NERDTreeChDirMode=1 "顯示書簽" let NERDTreeShowBookmarks=1 "設(shè)置忽略文件類型" let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$'] "窗口大小" let NERDTreeWinSize=25
3.4 設(shè)置indentLine插件(可用)
"===設(shè)置indentLine插件===" "縮進(jìn)指示線" let g:indentLine_char='┆' let g:indentLine_enabled = 1
3.5 設(shè)置autopep8插件(可用)
"===設(shè)置autopep8插件===" let g:autopep8_disable_show_diff=1
3.6 設(shè)置nerdcommenter插件(可用)
"nerdcommenter的leader默認(rèn)為\,可以用下面的命令更改" let mapleader=',' "在normal模式下按v并移動(dòng)光標(biāo)選擇需要注釋的行,再按F4就可以為所有選中的行添加注釋" map <F4> <leader>ci <CR>
4. 完整配置示例
"===通用配置===" "去掉vi的一致性" set nocompatible "顯示行號(hào)" "set number "隱藏滾動(dòng)條" set guioptions-=r set guioptions-=L set guioptions-=b "隱藏頂部標(biāo)簽欄" set showtabline=0 "設(shè)置字體" set guifont=Monaco:h13 "開啟語法高亮" syntax on "solarized主題設(shè)置在終端下的設(shè)置" let g:solarized_termcolors=256 "設(shè)置背景色" set background=dark "設(shè)置不折行" set nowrap "設(shè)置以u(píng)nix的格式保存文件" set fileformat=unix "設(shè)置C樣式的縮進(jìn)格式" set cindent "設(shè)置tab長度" set tabstop=4 set shiftwidth=4 "顯示匹配的括號(hào)" set showmatch "距離頂部和底部5行" set scrolloff=5 "命令行為兩行" set laststatus=2 "文件編碼" set fenc=utf-8 set backspace=2 "忽略大小寫" set ignorecase set incsearch "高亮搜索項(xiàng)" set hlsearch "不允許擴(kuò)展tab" set noexpandtab set whichwrap+=<,>,h,l set autoread "突出顯示當(dāng)前行" "set cursorline "突出顯示當(dāng)前列" set cursorcolumn "按F9進(jìn)入粘貼模式" set nopaste set pastetoggle=<F9> "===設(shè)置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() "開啟文件類型自動(dòng)檢測,編寫代碼時(shí)自動(dòng)換行對齊" filetype plugin indent on "===設(shè)置按F5運(yùn)行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 "===設(shè)置YouCompleteMe插件===" "默認(rèn)配置文件路徑" let g:ycm_global_ycm_extra_conf = '~/.ycm_extra_conf.py' "打開vim時(shí)不再詢問是否加載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' "是否開啟語義補(bǔ)全" let g:ycm_seed_identifiers_with_syntax=1 "是否在注釋中也開啟補(bǔ)全" let g:ycm_complete_in_comments=1 let g:ycm_collect_identifiers_from_comments_and_strings = 0 "開始補(bǔ)全的字符數(shù)" let g:ycm_min_num_of_chars_for_completion=2 "補(bǔ)全后自動(dòng)關(guān)機(jī)預(yù)覽窗口" let g:ycm_autoclose_preview_window_after_completion=1 "禁止緩存匹配項(xiàng),每次都重新生成匹配項(xiàng)" let g:ycm_cache_omnifunc=0 "字符串中也開啟補(bǔ)全" let g:ycm_complete_in_strings = 1 "離開插入模式后自動(dòng)關(guān)閉預(yù)覽窗口" autocmd InsertLeave * if pumvisible() == 0|pclose|endif "===設(shè)置NERDTree插件===" "F2開啟和關(guān)閉樹" map <F2> :NERDTreeToggle<CR> let NERDTreeChDirMode=1 "顯示書簽" let NERDTreeShowBookmarks=1 "設(shè)置忽略文件類型" let NERDTreeIgnore=['\~$', '\.pyc$', '\.swp$'] "窗口大小" let NERDTreeWinSize=25 "===設(shè)置indentLine插件===" "縮進(jìn)指示線" let g:indentLine_char='┆' let g:indentLine_enabled = 1 "===設(shè)置autopep8插件===" let g:autopep8_disable_show_diff=1 "===設(shè)置nerdcommenter插件===" "nerdcommenter的leader默認(rèn)為\,可以用下面的命令更改" let mapleader=',' "在normal模式下按v并移動(dòng)光標(biāo)選擇需要注釋的行,再按F4就可以為所有選中的行添加注釋" map <F4> <leader>ci <CR>
Debian的vim右鍵進(jìn)入visual模式:
Debian默認(rèn)裝好vim之后,右鍵不能粘貼,反而進(jìn)入了visual模式,甚是惱人,可通過如下方法修改。
vim版本:version 8.1.0875
修改方法:
編輯/usr/share/vim/vim81/defaults.vim文件,定位到第79行,在mouse=a的=前面加個(gè)-,如下:
if has('mouse') set mouse-=a endif
保存退出即可生效。
到此這篇關(guān)于Vim配置詳解的文章就介紹到這了,更多相關(guān)Vim配置詳解內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
科學(xué)知識(shí):時(shí)間復(fù)雜度計(jì)算方法
這篇文章主要介紹了科學(xué)知識(shí):時(shí)間復(fù)雜度計(jì)算方法,本文介紹了問題的定義、時(shí)間復(fù)雜度計(jì)算步驟、時(shí)間復(fù)雜度計(jì)算規(guī)則等內(nèi)容,需要的朋友可以參考下2015-05-05VSCode + WSL 2 + Ruby環(huán)境搭建圖文詳解
這篇文章主要介紹了VSCode + WSL 2 + Ruby環(huán)境搭建,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06Dreamweaver中如何設(shè)定文字大小、字體、顏色
這篇文章主要給大家介紹了關(guān)于Dreamweaver中如何設(shè)定文字大小、字體、顏色的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2007-06-06matlab讀取串口數(shù)據(jù)并顯示曲線的實(shí)現(xiàn)示例
這篇文章主要介紹了matlab讀取串口數(shù)據(jù)并顯示曲線的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08使用Visual Studio進(jìn)行文件差異比較的問題小結(jié)
這篇文章主要介紹了使用Visual Studio進(jìn)行文件差異比較,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07