文章部分內(nèi)容轉(zhuǎn)自《使用vim打造自己的python編輯器》https://www.cnblogs.com/linxiyue/p/7834817.html 感謝
參考文章:https://zhuanlan.zhihu.com/p/68111471
一、安裝
????yum -y install vim*
????git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
????如果~/.vim/bundle目錄不存在,則新建目錄:
? ? cd ~
????mkdir .vim
? ? cd .vim
????mkdir bundle
二、新建vim配置文件
????vim的配置是在用戶主目錄下的~/.vimrc文件中完成的,如果沒有的話,需要自己新建一下:
????cd ~
????touch .vimrc
????vim .vimrc
三、配置.vimrc
Vim 常用技巧:
將回車由默認(rèn)的8個(gè)空格改為4個(gè)空格:
命令:set sw=4
修改tab為4空格:
命令:set ts=4
設(shè)置每一級的縮進(jìn)長度:
命令:set shiftwidth=4
設(shè)置文件的編碼:
set fileencoding=utf-8
set? nocompatible "關(guān)閉與vi的兼容模式
set number "顯示行號
set nowrap??? "不自動(dòng)折行
set showmatch??? "顯示匹配的括號
set scrolloff=3??????? "距離頂部和底部3行"
set encoding=utf-8? "編碼
set? ?fenc=utf-8????? "編碼
set mouse-=a??????? "啟用鼠標(biāo)? -=a 右鍵可進(jìn)行黏貼處理等
set hlsearch??????? "搜索高亮
syntax on??? "語法高亮
為py文件添加下支持pep8風(fēng)格的配置:
au BufNewFile,BufRead *.py
\ settabstop=4?? "tab寬度
\ setsofttabstop=4?
\ setshiftwidth=4??
\ settextwidth=79? "行最大寬度
\ setexpandtab?????? "tab替換為空格鍵
\ setautoindent????? "自動(dòng)縮進(jìn)
\ setfileformat=unix?? "保存文件格式
分割窗口
vim在編輯的時(shí)候就可以打開多個(gè)文件:
:vs? 或者 :vsplit? 將當(dāng)前窗口豎直分割,并在上面新窗口中顯示當(dāng)前文件
:vs filename 將當(dāng)前窗口豎直分割,新文件在新窗口中顯示
:sp 或者:sv或者:split? 將當(dāng)前窗口水平分割,并在左邊新窗口中顯示當(dāng)前文件
:sp filename 將當(dāng)前窗口豎直分割,新文件在左邊新窗口中顯示
:new 新建文件并豎直分割
:vnew 新建文件并水平分割
如果想讓新窗口在右邊或者下方打開,添加配置:
set splitbelow
set splitright
在窗口之間切換可以用鼠標(biāo),如果不想用鼠標(biāo),切換按鍵如下:
Ctrl-w-j 切換到下方的分割窗口
Ctrl-w-k 切換到上方的分割窗口
Ctrl-w-l 切換到右側(cè)的分割窗口
Ctrl-w-h 切換到左側(cè)的分割窗口
覺得三個(gè)按鍵多的話可以設(shè)置快捷鍵:
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
這樣就不用按w鍵了。
代碼折疊
當(dāng)代碼行數(shù)很多的時(shí)候,代碼折疊是很必須的:
set foldmethod=indent
set foldlevel=99
使用zc按鍵來創(chuàng)建折疊,使用za來打開或者關(guān)閉折疊。
za經(jīng)常會誤輸入,可以用空格鍵來替代za:
nnoremap <space> za
一鍵執(zhí)行python代碼
如果想直接在vim中執(zhí)行python代碼,可以添加(來自https://www.zhihu.com/question/20271508):
map <F5> :call RunPython()<CR>
func! RunPython()
? ? exec "W"
? ? if &filetype == 'python'
? ? ? ? exec "!time python2.7 %"
? ? endif
endfunc
這樣,按F5鍵python代碼就可以自動(dòng)執(zhí)行了
插件
vim插件中最主要的就是vundle了,vundle用來管理vim的其它插件
Vundle
Vundle 是 Vim bundle 的簡稱,使用git來管理vim插件,有了它,安裝其它插件就方便很多。
項(xiàng)目地址https://github.com/VundleVim/Vundle.vim。
首先下載源碼:
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
如果~/.vim/bundle目錄不存在,則新建目錄:
cd ~
mkdir .vim
cd .vim
mkdir bundle
然后將下列配置放在.vimrc文件的開頭:
set nocompatible? ? ? ? ? ? ? " be iMproved, required
filetype off? ? ? ? ? ? ? ? ? " required
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" All of your Plugins must be added before the following line
call vundle#end()? ? ? ? ? ? " required
filetype plugin indent on? ? " required
如果想下載某個(gè)插件,比如自動(dòng)縮進(jìn)indentpython.vim插件,需要將
Plugin 'vim-scripts/indentpython.vim'
置于call vundle#begin()和call vundle#end()之間,保存配置后在vim中執(zhí)行
:PluginInstall
即可以自動(dòng)下載indentpython.vim插件了。
bundle可以管理下載幾種不同的插件,方式如下:
github上的插件
Plugin 'tpope/vim-fugitive'
來自于http://vim-scripts.org/vim/scripts.html的插件
Plugin 'L9'
非github上的git插件
Plugin 'git://git.wincent.com/command-t.git'
本地插件
Plugin 'file:///home/gmarik/path/to/plugin'
" The sparkup vim script is in a subdirectory of this repo called vim.
" Pass the path to set the runtimepath properly.
" Plugin 'rstacruz/sparkup', {'rtp': 'vim/'}
有舊插件的情況下,下載新的插件并重命名以避免沖突
Plugin 'ascenator/L9', {'name': 'newL9'}
下載方式除了在vim中運(yùn)行:PluginInstall外,還可以在命令行中運(yùn)行:
vim +PluginInstall +qall
其它常用的命令:
:PluginList? ? ? - lists configured plugins
:PluginInstall? ? - installs plugins; append `!` to update or just :PluginUpdate
:PluginSearch foo - searches for foo; append `!` to refresh local cache
:PluginClean? ? ? - confirms removal of unused plugins; append `!` to auto-approve removal
常用插件:
10 Plugin 'VundleVim/Vundle.vim'
11 Plugin 'vim-scripts/indentpython.vim'? ? ? ? ? "自動(dòng)縮進(jìn)"
12 Plugin 'vim-syntastic/syntastic'? ? ? ? ? ? ? ? "語法檢查"
13 Plugin 'altercation/vim-colors-solarized'? ? ? "配色方案"
14 Plugin 'scrooloose/nerdtree'? ? ? ? ? ? "樹形目錄"
15 Plugin 'Lokaltog/vim-powerline'? ? ? ? "狀態(tài)欄"
16 Plugin 'Yggdroot/indentLine'? ? ? ? ? ? "縮進(jìn)指示線 開關(guān) :IndentLinesToggle"
17 Plugin 'jiangmiao/auto-pairs'? ? ? ? ? "自動(dòng)補(bǔ)全括號和引號等"
18 Plugin 'kien/ctrlp.vim'? ? ? ? ? ? ? ? "搜索插件,在vim normal模式下,按下ctrl+p,然后輸入你要尋找的文件就行了"