本文转自https://blog.csdn.net/lord_is_layuping/article/details/7706874?utm_source=blogxgwz6
关键是使Vim在发现所编辑的文件是Python文件时自动加载python的缩进文件。默认的缩进方式很烂,不符合Python的编码习惯。但我 们又不能在全局的.vimrc(Windows中为_vimrc)修改,这会影响全局。使用autocmd只能靠后缀来识别Python文件,也不方便。 于是最好的方法是自定义Python缩进文件,放到~/.vim/indent/(Windows下是$VIMDIR$\vimfiles\indent \ 如C:\Program Files\Vim\vimfiles\indent)中,Vim发现Python文件后,会自动加载这个plugin,而不是用默认的缩进文件。可是,呵呵,编写这些是有些麻烦的(唉,对我很困难)。但 不用担心,有人以为我们写好了,直接到这个页面下载即可http://www.vim.org/scripts/script.php?script_id=974
package | script version | date | Vim version | user | release notes |
---|---|---|---|---|---|
python.vim | 0.3 | 2005-05-23 | 6.0 | Eric Mc Sween | Changes: - Update one shiftwidth instead of aligning with parens that stand at the end of a line. |
python.vim | 0.2 | 2004-06-07 | 6.0 | Eric Mc Sween | Changes: - Fix: skip parentheses in strings and comments. - Line up elif/else and except/finally with the most probable corresponding if or try statement. - Dedent after 'pass'. (Jeffrey Collins) |
python.vim | 0.1 | 2004-04-26 | 6.0 | Eric Mc Sween | Initial upload |
源文件的注释缩进有些问题,作如下修改:
setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except
改为:
setlocal indentkeys=!^F,o,O,<:>,0),0],0},=elif,=except,0#
另外,在.vimrc(Windows中为_vimrc如C:\Program Files\Vim\_vimrc)中加入以下语句以确定插件功能打开:filetype plugin indent on好了,现在就可以享受舒服的Python形式的自动缩进了。
----------------------------------------------------------------------------------------------------------------------------------------------------------
在vim中显示的都是一个tab,对齐的很工整,
一运行就出错“unindent does not match any outer indentation level”,搞了半天就是缩进不统一啊,
python又把缩进作为语法之一(的确挺好)。
解决办法:
set tabstop=8
其他关于缩进的:
set shiftwidth=4
set tabstop=4
set expandtab
set softtabstop=4
set pastetoggle=<f7>
set autoindent
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
如何拷贝代码而不破坏缩进?
开始拷贝前按F7,拷贝结束后再按一次F7。
如何改变整段代码的缩进?
按”v”进入视图模式,选中要移动的代码块。然后用“<”左缩进,或用“>”右缩进。缩进宽度默认为1个shiftwidth宽。如果要缩进多个shiftwidth宽,在按“<”或”>”之前先按相应数字。
ChangeLog:
Mon Aug 4 10:46:30 CST 2008,修正“如何”写成“如果”的笔误。
Sun Jul 8 CST 2007,添加改变整段代码缩进的方法。
Tue Jul 3 CST 2007,加入pastetoggle设置
详细出处参考:http://www.jb51.net/softjc/7832.html
----------------------------------------------------------------------------------------------------------------------------------------------------------
用vim写python代码
在自己的home目录下编辑.vimrc文件,如果没有新建一个
下面的配置能让自己过得舒服一点
" 不要使用vi的键盘模式,而是vim自己的
set nocompatible
" 设定解码
if has(“multi_byte”)
" When ‘fileencodings’ starts with ‘ucs-bom’, don’t do this manually
"set bomb
set fileencodings=ucs-bom,utf-8,chinese,taiwan,japan,korea,latin1
" CJK environment detection and corresponding setting
if v:lang =~ “^zh_CN”
" Simplified Chinese, on Unix euc-cn, on MS-Windows cp936
set encoding=utf-8
set termencoding=utf-8
if &fileencoding == ‘’
set fileencoding=utf-8
endif
elseif v:lang =~ “^zh_TW”
" Traditional Chinese, on Unix euc-tw, on MS-Windows cp950
set encoding=euc-tw
set termencoding=euc-tw
if &fileencoding == ‘’
set fileencoding=euc-tw
endif
elseif v:lang =~ “^ja_JP”
" Japanese, on Unix euc-jp, on MS-Windows cp932
set encoding=euc-jp
set termencoding=euc-jp
if &fileencoding == ‘’
set fileencoding=euc-jp
endif
elseif v:lang =~ “^ko”
" Korean on Unix euc-kr, on MS-Windows cp949
set encoding=euc-kr
set termencoding=euc-kr
if &fileencoding == ‘’
set fileencoding=ecu-kr
endif
endif
" Detect UTF-8 locale, and override CJK setting if needed
if v:lang =~ “utf8
”
set encoding=utf-8
endif
else
echoerr ‘Sorry, this version of (g)Vim was not compiled with “multi_byte”’
endif
" 自动格式化设置
filetype indent on
set autoindent
set smartindent
" 显示未完成命令
set showcmd
" 侦测文件类型
filetype on
" 载入文件类型插件
filetype plugin on
" 为特定文件类型载入相关缩进文件
filetype indent on
" 语法高亮
syntax on
" 显示行号
set number
" tab宽度
set tabstop=4
set cindent shiftwidth=4
set autoindent shiftwidth=4
" 保存文件格式
set fileformats=unix,dos
" 文件被其他程序修改时自动载入
set autoread
" 命令行补全
set wildmenu
" 打开文件时,总是跳到退出之前的光标处
autocmd BufReadPost *
\ if line("’"") > 0 && line("’"") <= line("$") |
\ exe “normal! g`”" |
\ endif
filetype plugin on "允许使用ftplugin目录下的文件类型特定脚本
filetype indent on "允许使用indent目录下的文件类型缩进
“”""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" PYTHON 相关的设置 "
“”""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"Python 文件的一般设置,比如不要 tab 等
"设置自动缩进为4,插入模式里: 插入 <Tab> 时使用合适数量的空格。
"要插入实际的制表,可用 CTRL-V<Tab>
autocmd FileType python setlocal expandtab | setlocal shiftwidth=4 |
\setlocal softtabstop=4 | setlocal textwidth=76 |
\setlocal tabstop=4
"搜索逐字符高亮
set hlsearch
set incsearch
"设置代码样式
colorscheme desert
"设置tags查找位置
set tags=tags;
set autochdir
==========================================================
pydiction补全代码
官方地址http://www.vim.org/scripts/script.php?script_id=850
下载zip包,在home目录下查找.vim文件夹,如果没有创建这个目录
官网有安装说明”install details”
完成后.vim的文件结构如下:
.vim └── after └── ftplugin ├── pydiction │ └── complete-dict └── python_pydiction.vim
然后配置.vimrc,添加语句
let g:pydiction_location = '~/.vim/after/ftplugin/pydiction/complete-dict'
后面的值是complete-dict文件的路径
用vim编辑一个py文件,import os.<TAB>,这时候应该出现提示,证明成功了
ctrl+n ctrl+p选择列表里的提示项
===========================================================
其实7.2版本的vim自身已经提供了比较强悍的补全功能, vim的OMNI补全(也叫”全能补全”)
os.<CTRL+x , CTRL+o>,如果开启了vim的python模块,现在应该有一个分割窗口显示函数的参数,以及__doc__信息
如果需要动态输入刷新提示内容,在配置文件中加入
set completeopt=longest,menu
============================================================
ctags提示
首先确定安装了ctags软件,运行tags生成脚本
ctags -R *.py
这时会生成tags文件
安装taglist插件,网址http://www.vim.org/scripts/script.php?script_id=273
安装完成后,编辑py文件,执行vim命令
:Tlist
会出现taglist窗口,如果需要tags文件中的关键词补全,CTRL+n,如果需要跟踪关键词文件CTRL+],跳回来CTRL+t
============================================================
代码模板,主页地址 http://www.vim.org/scripts/script.php?script_id=2540
安装后,这个插件默认的快捷键是<TAB>
但是pydiction_location默认的快捷键也是<TAB>,这里修改 pydiction_location的快捷键
找到.vim/after/ftplugin/python_pydiction.vim文件
修改
" Make the Tab key do python code completion: inoremap <silent> <buffer> <TAB>
为
" Make the Tab key do python code completion: inoremap <silent> <buffer> <C-P>
这样就把pydiction_location的快捷键修改为CTRL+p了
然后编辑py文件,输入 cl<TAB>,就会出现class的定义模板了,
这些模板定义在.vim/syntax文件夹下,可自行修改
==============================================================
py语法检查插件 http://www.vim.org/scripts/script.php?script_id=2441
安装以后会用红色,提示py代码的错误