VIM,手的延伸

团长: 赶紧把这个Python脚本给我改好!

萌新: 好的,先等我把Pycharm打开。

休闲玩家: 你就不能用VIM

用好一种可配置可扩展可编程的编辑器。学习它,扩展它,拥有它!

本文参考自:VIM and Python – A Match Made in Heaven,增加了Python-mode等插件。

安装插件

利用VIM 8的原生包管理功能,从GitHub上克隆以下插件到~/.vim/pack/foo/start目录:

mkdir -p $HOME/.vim/pack/foo/start
cd $HOME/.vim/pack/foo/start

sudo apt-get install -yqq --no-install-recommends build-essential cmake python3-dev
git clone --recurse-submodules "https://github.com/Valloric/YouCompleteMe.git"

git clone --recurse-submodules "https://github.com/python-mode/python-mode.git"
git clone "https://github.com/mattn/emmet-vim.git"
git clone "https://github.com/scrooloose/nerdtree.git"
git clone "https://github.com/vim-airline/vim-airline.git"
git clone "https://github.com/tpope/vim-fugitive.git"
git clone "https://github.com/yianwillis/vimcdoc.git"

配置VIM

添加插件的帮助文档:

vim -u NONE \
        -c "helptags YouCompleteMe/doc" \
        -c "helptags python-mode/doc" \
        -c "helptags emmet-vim/doc" \
        -c "helptags nerdtree/doc" \
        -c "helptags vim-airline/doc" \
        -c "helptags vim-fugitive/doc" \
        -c "helptags vimcdoc/doc" \
        -c q

$HOME目录下,添加.vimrc文件,代码如下:

set nocompatible
syntax on
filetype off

set expandtab
set tabstop=4 shiftwidth=4 softtabstop=4
set autoindent

au BufNewFile,BufRead *.js,*.html,*.scss,*.yml,*.rb,Rakefile
    \ set expandtab |
    \ set tabstop=2 shiftwidth=2 softtabstop=2|
    \ set autoindent

set splitbelow
set splitright

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>

set foldmethod=indent
set foldlevel=99
nnoremap <space> za

set encoding=utf-8
set nu
set hls is
set clipboard=unnamed

" python-mode
let g:pymode_python = 'python3'
let g:pymode_folding = 1
let g:pymode_lint_on_unmodified = 1
let g:pymode_lint_on_fly = 1
let g:pymode_syntax = 1
let g:pymode_syntax_all = 1
let g:pymode_run_bind = '<F5>'

" NERDTree
autocmd vimenter * NERDTree
map <C-n> :NERDTreeToggle<CR>
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
let NERDTreeIgnore=['^.git$', '^__pycache__$', '\.pyc$', '\~$']

" YouCompleteMe
let g:ycm_autoclose_preview_window_after_completion=1
map <leader>g  :YcmCompleter GoToDefinitionElseDeclaration<CR>

" emmet-vim
let g:user_emmet_settings = {
\   'html': {
\       'snippets': {
\           'html:5': "<!DOCTYPE html>\n"
\               ."<html lang=\"${lang}\">\n"
\               ."<head>\n"
\               ."\t<meta charset=\"${charset}\">\n"
\               ."\t<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">\n"
\               ."\t<meta http-equiv=\"X-UA-Compatible\" content=\"ie=edge\">\n"
\               ."\t<title></title>\n"
\               ."</head>\n"
\               ."<body>\n\t${child}|\n</body>\n"
\               ."</html>",
\       },
\   },
\}

filetype plugin indent on