美文网首页
为python编程设置vim

为python编程设置vim

作者: beeworkshop | 来源:发表于2019-06-15 16:59 被阅读0次
  1. 全局常见配置(可选)
vim /etc/vim/vimrc  # 全局配置

set number  # 设置显示行号
set ts=4    # 设置tab为4个空格
set expandtab  
set autoindent 
  1. 一般在用户的Home目录下配置~/.vimrc
cd ~
touch .vimrc
  • 基本配置
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    "语法高亮
  • 为py文件添加下支持pep8风格的配置:
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

使用zc按键来创建折叠,使用za来打开或者关闭折叠。
za经常会误输入,可以用空格键来替代za:
nnoremap <space> za
  • 一键执行python代码
map <F5> :call RunPython()<CR>
func! RunPython()
    exec "W"
    if &filetype == 'python'
        exec "!time python2.7 %"
    endif
endfunc
按F5键python代码就可以自动执行了
  • Vundle
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
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
  • 插件安装
如果想下载某个插件,比如自动缩进indentpython.vim插件,需要将
Plugin 'vim-scripts/indentpython.vim'
置于call vundle#begin()和call vundle#end()之间,保存配置后在vim中执行
:PluginInstall
即可以自动下载indentpython.vim插件了。
命令行中运行方式:
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
  1. python常用配置
    安装pydiction(实现python代码补全)
下载安装Pydiction
mkdir -p ~/.vim/bundle
cd ~/.vim/bundle
git clone https://github.com/rkulla/pydiction.git
cp -r ~/.vim/bundle/pydiction/after/ ~/.vim

新建.vimrc文件
vim ~/.vimrc

在~/.vimrc文件添加如下配置则完成功能开启:
filetype plugin on
let g:pydiction_location = '~/.vim/bundle/pydiction/complete-dict'
let g:pydiction_menu_height = 3

完整的配置如下(vim ~/.vimrc):
注意:"是注释

filetype indent plugin on
filetype plugin on

let g:pydiction_location = '~/.vim/bundle/pydiction/complete-dict'
let g:pydiction_menu_height = 3

nnoremap <F2> :set nonumber!<CR>:set foldcolumn=0<CR>

autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascrīpt set omnifunc=javascrīptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
autocmd FileType c set omnifunc=ccomplete#Complete

set pastetoggle=<F12>

set autoindent
set tabstop=4
set shiftwidth=4
set expandtab
set number
set encoding=utf-8
set fenc=utf-8
set scrolloff=3
set mouse=a
set mouse=v
syntax on
syntax enable

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

map <F5> :call RunPython()<CR>
func! RunPython()
    exec "W"
    if &filetype == 'python'
        exec "!time python3 %"
    endif
endfunc

上边python3处可以为python2.7

  1. 为vim添加molokai主题

github地址:https://github.com/tomasr/molokai
安装:

cd ~/.vim
git clone https://github.com/tomasr/molokai.git
会发现在~/.vim目录下面多个目录colors
cp ~/.vim/molokai/colors/molokai.vim ~/.vim/colors/molokai.vim

vim ~/.vimrc
增加以下配置
colorscheme  molokai  
set t_Co=256
set background=dark
  1. 格式化xml文件
function Xml()
    set filetype=xml
    :%s/></>\r</g "把><替换成>回车<
    :normal gg=G<cr>
endfunction
map <F10> :call Xml()<CR>

相关文章

网友评论

      本文标题:为python编程设置vim

      本文链接:https://www.haomeiwen.com/subject/wtkgfctx.html