天天看点

vim配置python开发环境_Vim - 配置IDE一般的python环境

Vim 作为一个经典的编辑器,如果配置合适,可以成为一个编辑python脚本非常给力的工具。这篇文章主要目的是介绍如何打造一个强大的vim编辑环境。

第一部分:软件安装:

在终端中执行:

vim --version

得到:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Oct 5 2017 04:42:50)

MacOS X (unix) version

Included patches: 1-1175

Compiled by [email protected]

Huge version with MacVim GUI. Features included (+) or not (-):

+acl +find_in_path -mouse_sysmouse -tag_any_white

+arabic +float +mouse_urxvt -tcl

+autocmd +folding +mouse_xterm +termguicolors

+balloon_eval -footer +multi_byte +terminal

+browse +fork() +multi_lang +terminfo

++builtin_terms +fullscreen -mzscheme +termresponse

+byte_offset -gettext +netbeans_intg +textobjects

+channel -hangul_input +num64 +timers

+cindent +iconv +odbeditor +title

+clientserver +insert_expand +packages +toolbar

+clipboard +job +path_extra +transparency

+cmdline_compl +jumplist +perl/dyn +user_commands

+cmdline_hist +keymap +persistent_undo +vertsplit

+cmdline_info +lambda +postscript +virtualedit

+comments +langmap +printer +visual

+conceal +libcall +profile +visualextra

+cryptv +linebreak +python/dyn +viminfo

+cscope +lispindent +python3/dyn +vreplace

+cursorbind +listcmds +quickfix +wildignore

+cursorshape +localmap +reltime +wildmenu

+dialog_con_gui +lua/dyn +rightleft +windows

+diff +menu +ruby/dyn +writebackup

+digraphs +mksession +scrollbind -X11

+dnd +modify_fname +signs -xfontset

-ebcdic +mouse +smartindent +xim

+emacs_tags +mouseshape +startuptime -xpm

+eval +mouse_dec +statusline -xsmp

+ex_extra -mouse_gpm -sun_workshop -xterm_clipboard

+extra_search -mouse_jsbterm +syntax -xterm_save

+farsi +mouse_netterm +tag_binary

+file_in_path +mouse_sgr +tag_old_static

...

这里要确认两件事:Vim的版本要求不低于7.3

要求支持python,确保列表中有+python

不同系统下Vim的安装方式如下:

OSX:

安装homebrew,然后在终端中执行:

brew update

brew install vim

*NIX:

在Debian或者Ubuntu上,执行:

sudo apt-get remove vim-tiny

sudo apt-get update

sudo apt-get install vim

Windows:

第二部分:配置文件

vim的配置文件是

~/.vimrc

你可以讲需要的配置加入配置文件中,重新启动vim或者在normal环境中执行

:source %

便可加载配置文件

编辑vimrc文件,可以执行:

vim ~/.vimrc

有用的配置内容有:

set nocompatible

syntax on

filetype plugin indent on

set ic

set hlsearch

set encoding=utf-8

set fileencodings=utf-8,ucs-bom,GB2312,big5

set cursorline

set autoindent

set smartindent

set scrolloff=4

set showmatch

set nu

窗口移动:

vim的默认窗口移动方式是Ctrl+w Ctrl+hjkl,但是这样太过复杂,对此进行绑定:

nnoremap

nnoremap

nnoremap

nnoremap

为python的特殊配置有:

let python_highlight_all=1

au Filetype python set tabstop=4

au Filetype python set softtabstop=4

au Filetype python set shiftwidth=4

au Filetype python set textwidth=79

au Filetype python set expandtab

au Filetype python set autoindent

au Filetype python set fileformat=unix

autocmd Filetype python set foldmethod=indent

autocmd Filetype python set foldlevel=99

自动执行:按一下F5,自动执行代码:

map :call CompileRunGcc()

func! CompileRunGcc()

exec "w"

if &filetype == 'c'

exec "!g++ % -o %<"

exec "!time ./%<"

elseif &filetype == 'cpp'

exec "!g++ % -o %<"

exec "!time ./%<"

elseif &filetype == 'java'

exec "!javac %"

exec "!time java %<"

elseif &filetype == 'sh'

:!time bash %

elseif &filetype == 'python'

exec "!clear"

exec "!time python3 %"

elseif &filetype == 'html'

exec "!firefox % &"

elseif &filetype == 'go'

" exec "!go build %<"

exec "!time go run %"

elseif &filetype == 'mkd'

exec "!~/.vim/markdown.pl % > %.html &"

exec "!firefox %.html &"

endif

endfunc

第三部分:有用的插件

安装插件强烈建议使用插件管理工具,这里推荐的是vundle

首先安装vundle:

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

然后在vimrc中加入:

set nocompatible " required

filetype off " required

" set the runtime path to include Vundle and initialize

set rtp+=~/.vim/bundle/Vundle.vim

call vundle#begin()

" alternatively, pass a path where Vundle should install plugins

"call vundle#begin('~/some/path/here')

" let Vundle manage Vundle, required

Plugin 'gmarik/Vundle.vim'

" Add all your plugins here (note older versions of Vundle used Bundle instead of Plugin)

" All of your Plugins must be added before the following line

call vundle#end() " required

filetype plugin indent on " required

关闭后重新打开vim,在normal模式下执行:

:PluginInstall

插件管理工具会自动的安装配置文件中写到的插件。

在vimrc中加入

Plugin 'Chiel92/vim-autoformat'

关闭后重新打开vim,在normal模式下执行下述代码即可:

Plugin 'Chiel92/vim-autoformat'

第四部分:推荐的插件:

1、YouCompleteMe

为了成为一个IDE,必须要有自动补全工具,这里强烈推荐YouCompleteMe强烈建议按照官方说明一步一步进行安装,史上最难安装的vim插件并非名副其实

2、Autoformat

这个插件vim-autoformat能够自动的一键格式化代码

Before:

After:

安装方式:

这款插件需要已经安装有代码格式化工具,安装方式是在终端下执行:

pip install autopep8

推荐的配置是:

Plugin 'Chiel92/vim-autoformat'

nnoremap :Autoformat

let g:autoformat_autoindent = 0

let g:autoformat_retab = 0

let g:autoformat_remove_trailing_spaces = 0

每次在normal环境下按F6便可以格式化代码

3、文件树

在vim中浏览文件夹:nerdtree

推荐配置是:

Plugin 'https://github.com/scrooloose/nerdtree'

nnoremap :NERDTreeToggle

autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif

通过按开关文件树

4、不同颜色区分括号匹配

这个插件使用不同的颜色高亮匹配的括号:rainbow_parentheses

Plugin 'kien/rainbow_parentheses.vim'

let g:rbpt_colorpairs = [

\ ['brown', 'RoyalBlue3'],

\ ['Darkblue', 'SeaGreen3'],

\ ['darkgray', 'DarkOrchid3'],

\ ['darkgreen', 'firebrick3'],

\ ['darkcyan', 'RoyalBlue3'],

\ ['darkred', 'SeaGreen3'],

\ ['darkmagenta', 'DarkOrchid3'],

\ ['brown', 'firebrick3'],

\ ['gray', 'RoyalBlue3'],

\ ['darkmagenta', 'DarkOrchid3'],

\ ['Darkblue', 'firebrick3'],

\ ['darkgreen', 'RoyalBlue3'],

\ ['darkcyan', 'SeaGreen3'],

\ ['darkred', 'DarkOrchid3'],

\ ['red', 'firebrick3'],

\ ]

let g:rbpt_max = 16

let g:rbpt_loadcmd_toggle = 0

au VimEnter * RainbowParenthesesToggle

au Syntax * RainbowParenthesesLoadRound

au Syntax * RainbowParenthesesLoadSquare

au Syntax * RainbowParenthesesLoadBraces

5、强大的状态栏

这款插件提供了加强版的状态栏:vim-airline

推荐配置:

Plugin 'https://github.com/bling/vim-airline'

6、代码检查工具

感谢丁春的评论:ale代替synatastic,有奇效

尝试了ale,确实很不错,它利用了vim8的异步处理功能,用起来不会有syntastic的卡顿现象

确保你的vim版本不低于8.0

安装方式:

和syntastic一样,它需要代码检测工具支持:

pip install flake8

推荐配置是:

Plugin 'w0rp/ale'

let g:ale_fix_on_save = 1

let g:ale_completion_enabled = 1

let g:ale_sign_column_always = 1

let g:airline#extensions#ale#enabled = 1

syntastic提供了自动代码检查功能,再也不用担心变量写错了。

安装方式:

这款插件需要已经安装好代码检测工具,python代码检查工具的安装方式是在终端下执行:

pip install flake8

推荐配置是:

Plugin 'scrooloose/syntastic'

set statusline+=%#warningmsg#

set statusline+=%{SyntasticStatuslineFlag()}

set statusline+=%*

let g:syntastic_always_populate_loc_list = 1

let g:syntastic_auto_loc_list = 1

let g:syntastic_check_on_open = 1

let g:syntastic_check_on_wq = 0

第五部分:推荐

它的功能是按一下F12,自动在所在行上方加入set_trace(),并且在文件开头加入相应的模块导入

按F12之前:

按一下F12:

再按一下F12,就回到第一张图的情况了

推荐配置:

Plugin 'sillybun/setbreakpoints_python'

autocmd FileType python nnoremap :call ToggleBreakPoint()

这是我实现的第二个插件,它实现了python自动格式化代码功能,每次输入完一条语句,上一条语句就会自动的调用autopep8进行格式化

它实现的功能是每写完一条语句比如

a.num=a.num+1

在输入完这条语句按下回车后,插件会自动的将它拍版成PEP 8标准的python风格代码:

a.num = a.num + 1

安装方式:

在配置文件中放入:

Plugin 'sillybun/autoformatpythonstatement'

autocmd FileType python let g:autoformatpython_enabled = 1插件使用python3完成,需要vim的python3支持

我的vim配置文件是vimrc