python学习-vim插件安装

centos7上自带python2.7,我们需要优化一下python环境。

  1. 一、使用豆瓣源加速软件安装

pip install -i   flask    #使用-i 选项
mkdir ~./pip && vim pip.conf        #修改pip的配置文件
[global]
index-url = https://pypi.douban.com/simple/

image.png

  1. 二、修改.vimrc文件

    主要增加一些配置选项,例如显示行号,一键执行等

  2. vim .vimrc
  3. set nocompatible "关闭与vi的兼容模式
    set number "显示行号
    set nowrap    "不自动折行
    set showmatch    "显示匹配的括号
    set scrolloff=3        "距离顶部和底部3行"
    set encoding=utf-8  "编码
    set fenc=utf-8      "编码
    set mouse=a        "启用鼠标
    set hlsearch        "搜索高亮
    syntax on    "语法高亮
    au BufNewFile,BufRead *.py
    \ set tabstop=4   "tab宽度
    \ set softtabstop=4  
    \ set shiftwidth=4   
    \ set textwidth=79  "行最大宽度
    \ set expandtab       "tab替换为空格键
    \ set autoindent      "自动缩进
    \ set fileformat=unix   "保存文件格式
    set foldmethod=indent
    set foldlevel=99
    map <F8> :call RunPython()<CR>
    func! RunPython()
        exec "W"
        if &filetype == 'python'
            exec "!time python2.7 %"
        endif
    endfunc



一键F8执行效果


image.png



三、配置vim插件管理vundle

Vundle 是 Vim bundle 的简称,使用git来管理vim插件,有了它,安装其它插件就方便很多。

创建目录

cd ~
mkdir .vim
cd .vim
mkdir bundle

进入目录,下载文件

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

然后将下列配置放在.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

如果想下载某个插件,比如自动缩进indentpython.vim插件,需要将

Plugin 'vim-scripts/indentpython.vim'

置于call vundle#begin()和call vundle#end()之间,保存配置后在vim中执行

:PluginInstall

image.png

image.png

四、多窗口编辑



五、安装常用python插件

  1. 编程提示插件jedi-vim

pip install jedi
git clone --recursive https://github.com/davidhalter/jedi-vim.git ~/.vim/bundle/jedi-vim

image.png


2.语法检查syntastic

git clone https://github.com/vim-syntastic/syntastic.git
Plugin 'vim-syntastic/syntastic' #.vimrc中增加

3.flak8代码风格检查

pip install flake8
https://github.com/nvie/vim-flake8.git
Plugin 'nvie/vim-flake8'    #.vimrc中添加

按F7进行检查·

image.png


4.添加树形目录

git clone  
Plugin 'scrooloose/nerdtree'            #.vimrc中添加
map <C-n> :NERDTreeToggle<CR>            #.vimrc中添加 使用Ctrl+n快捷键

image.png


5.缩进显示 indentline

git clone https://github.com/Yggdroot/indentLine
Plugin 'Yggdroot/indentLine'    #vimrc中添加

image.png


6.vim-autopep8自动格式化代码

 pip install autopep8
 git clone https://github.com/tell-k/vim-autopep8.git
 Plugin 'tell-k/vim-autopep8' #vimrc中添加
 autocmd FileType python noremap <buffer> <F9> :call Autopep8()<CR>        #使用快捷键F9

image.png


7.auto-pairs自动补全括号和引号

git clone  
Plugin 'jiangmiao/auto-pairs'

8.ctrlp.vim搜索文件

在vim普通模式下搜索ctl+P即可

Plugin 'kien/ctrlp.vim'

image.png


最终,整个配置文件.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'
Plugin 'davidhalter/jedi-vim'
Plugin 'vim-syntastic/syntastic'
Plugin 'nvie/vim-flake8'
Plugin 'scrooloose/nerdtree'
Plugin 'Yggdroot/indentLine'
Plugin 'tell-k/vim-autopep8'
Plugin 'vim-scripts/indentpython.vim'
Plugin 'kien/ctrlp.vim'
Plugin 'jiangmiao/auto-pairs'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required


set nocompatible "关闭与vi的兼容模式
set number "显示行号
set nowrap    "不自动折行
set showmatch    "显示匹配的括号
set scrolloff=3        "距离顶部和底部3行"
set encoding=utf-8  "编码
set fenc=utf-8      "编码
set mouse=a        "启用鼠标
set hlsearch        "搜索高亮
syntax on    "语法高亮
au BufNewFile,BufRead *.py
\ set tabstop=2   "tab宽度
\ set softtabstop=4  
\ set shiftwidth=4   
\ set textwidth=79  "行最大宽度
\ set expandtab       "tab替换为空格键
\ set autoindent      "自动缩进
\ set fileformat=unix   "保存文件格式
set foldmethod=indent
set foldlevel=50
map <F8> :call RunPython()<CR>
func! RunPython()
    exec "W"
    if &filetype == 'python'
        exec "!time python2.7 %"
    endif
endfunc
map <C-n> :NERDTreeToggle<CR>

autocmd FileType python noremap <buffer> <F9> :call Autopep8()<CR>



猜你喜欢

转载自blog.51cto.com/11555417/2150626