您的位置:首页 > 编程语言 > Python开发

Ubuntu16.04下安装Python3的VIM IDE

2017-11-25 15:08 661 查看
本文从《Vim与Python真乃天作之合:将Vim打造为强大的Python开发环境》摘录。

原文连接

译文连接

安装

首先要确认编辑器是否成功安装:

vim --version


如果已经安装了,你应该看到类似下面的文字:

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Nov 24 2016 16:44:48)
包含补丁: 1-1689
另外的补丁: 8.0.0056
修改者 pkg-vim-maintainers@lists.alioth.debian.org
编译者 pkg-vim-maintainers@lists.alioth.debian.org
巨型版本 无图形界面。  可使用(+)与不可使用(-)的功能:
+acl             +farsi           +mouse_netterm   +tag_binary
+arabic          +file_in_path    +mouse_sgr       +tag_old_static
+autocmd         +find_in_path    -mouse_sysmouse  -tag_any_white
-balloon_eval    +float           +mouse_urxvt     -tcl
-browse          +folding         +mouse_xterm     +terminfo
++builtin_terms  -footer          +multi_byte      +termresponse
+byte_offset     +fork()          +multi_lang      +textobjects
+channel         +gettext         -mzscheme        +timers
+cindent         -hangul_input    +netbeans_intg   +title
-clientserver    +iconv           +packages        -toolbar
-clipboard       +insert_expand   +path_extra      +user_commands
+cmdline_compl   +job             -perl            +vertsplit
+cmdline_hist    +jumplist        +persistent_undo +virtualedit
+cmdline_info    +keymap          +postscript      +visual
+comments        +langmap         +printer         +visualextra
+conceal         +libcall         +profile         +viminfo
+cryptv          +linebreak       -python          +vreplace
+cscope          +lispindent      +python3         +wildignore
+cursorbind      +listcmds        +quickfix        +wildmenu
+cursorshape     +localmap        +reltime         +windows
+dialog_con      -lua             +rightleft       +writebackup
+diff            +menu            -ruby            -X11
+digraphs        +mksession       +scrollbind      -xfontset
-dnd             +modify_fname    +signs           -xim
-ebcdic          +mouse           +smartindent     -xsmp
+emacs_tags      -mouseshape      +startuptime     -xterm_clipboard
+eval            +mouse_dec       +statusline      -xterm_save
+ex_extra        +mouse_gpm       -sun_workshop    -xpm
+extra_search    -mouse_jsbterm   +syntax
系统 vimrc 文件: "$VIM/vimrc"
用户 vimrc 文件: "$HOME/.vimrc"
第二用户 vimrc 文件: "~/.vim/vimrc"
用户 exrc 文件: "$HOME/.exrc"
$VIM 预设值: "/usr/share/vim"
编译方式: gcc -c -I. -Iproto -DHAVE_CONFIG_H   -Wdate-time  -g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1
链接方式: gcc   -Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -o vim        -lm -ltinfo -lnsl  -lselinux  -lacl -lattr -lgpm -ldl     -L/usr/lib/python3.5/config-3.5m-x86_64-linux-gnu -lpython3.5m -lpthread -ldl -lutil -lm


在这一步,你要确保已经满足以下两点要求:

Vim编辑版本应该大于7.3。

支持Python语言。在所选编辑器的功能中,确保你看到了+python。

如果满足上述要求,接下来可以安装Vim扩展了。如果不满足,则需要安装/升级。

安装vim:

sudo apt-get remove vim-tiny
sudo apt-get update
sudo apt-get install vim


验证安装

确保你已经安装了7.3版本以上、支持Python的Vim编辑器。你可以再次运行vim –version进行确认。如果你想知道Vim中使用的Python版本,你可以在编辑器中运行:

python3 import sys; print(sys.version)


运行结果:

3.5.2 (default, Sep 14 2017, 22:51:06)
[GCC 5.4.0 20160609]


这行命令会输出你的编辑器当前的Python版本。如果报错,那么你的编辑器就不支持Python语言,需要重装或重新编译。

Vim编辑器安装完成后,我们来看看如何将其设置为Python开发的强大环境。

Vim扩展

Vim本身能够满足开发人员的很多需求,但是它的可扩展性也极强,并且已经有一些杀手级的扩展,可以让Vim拥有“现代”集成开发环境的特性。所以,你所需要的第一件东西就是一个好用的扩展管理器。

Vim的扩展通常也被成为bundle或插件。

Vundle

Vundle

Vim有多个扩展管理器,但是我们强烈推荐Vundle。你可以把它想象成Vim的pip。有了Vundle,安装和更新包这种事情不费吹灰之力。

我们现在来安装Vundle:

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


该命令将下载Vundle插件管理器,并将它放置在你的Vim编辑器bundles文件夹中。现在,你可以通过.vimrc配置文件来管理所有扩展了。

将配置文件添加到你的用户的home文件夹中:

touch ~/.vimrc


接下来,把下来的Vundle配置添加到配置文件的顶部:

set nocompatible              " 必须的
filetype off                  " 必须的

" 设置Vundle路径并进行初始化
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')

" 让Vundle管理Vundle,必需的
Plugin 'gmarik/Vundle.vim'

" 在这里添加所有你的插件(注意Vundle的较早版本使用包而不是插件)

" 你所有的插件之前必须添加以下行
call vundle#end()            " required
filetype plugin indent on    " required


这样,你就完成了使用Vundle前的设置。之后,你就可以在配置文件中添加希望安装的插件,然后打开Vim编辑器,运行下面的命令:

:PluginInstall


常用插件推荐

代码折叠(Code Folding)

" Enable folding
set foldmethod=indent
set foldlevel=99


将折叠快捷键更改为空格:

" Enable folding with the spacebar
nnoremap <space> za


安装SimplyFold来改善折叠效果

Plugin 'tmhedberg/SimpylFold'


PEP8

PEP8风格的缩进

这些设置将让Vim中的Tab键就相当于4个标准的空格符,确保每行代码长度不超过80个字符,并且会以unix格式储存文件,避免在推送到Github或分享给其他用户时出现文件转换问题。

au BufNewFile,BufRead *.py
\ set tabstop=4
\ set softtabstop=4
\ set shiftwidth=4
\ set textwidth=79
\ set expandtab
\ set autoindent
\ set fileformat=unix


PEP8代码风格检查

Plugin 'nvie/vim-flake8'


自动缩进

Plugin 'vim-scripts/indentpython.vim'


标示不必要的空白字符

au BufRead,BufNewFile *.py,*.pyw,*.c,*.h match BadWhitespace /\s\+$/


支持UTF-8编码

set encoding=utf-8


自动补全

Bundle 'Valloric/YouCompleteMe'


语法检查/高亮

Plugin 'scrooloose/syntastic'


让你的代码变得更漂亮

let python_highlight_all=1
syntax on


配色方案

Plugin 'jnurmine/Zenburn'
Plugin 'altercation/vim-colors-solarized'
if has('gui_running')
set background=dark
colorscheme solarized
else
colorscheme Zenburn
endif


文件浏览

Plugin 'scrooloose/nerdtree'
Plugin 'jistr/vim-nerdtree-tabs'


超级搜索

Plugin 'kien/ctrlp.vim'


显示行号

set nu


Git集成

Plugin 'tpope/vim-fugitive'


Shell开启Vim编辑模式

在~/.inputrc文件中添加这行代码:

set editing-mode vi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: