Python Vim:当按enter键时,如何缩进到打开的括号或括号中?
我已经用Vim编写Python有一段时间了,但有一件事我还没有弄清楚如何将它设置为自动缩进到最后一个打开的paren级别 根据pep8,如果你有一个打开的paren,你需要将行打断以容纳80列,那么你应该在该打开的paren继续下一行。例如:Python Vim:当按enter键时,如何缩进到打开的括号或括号中?,python,vim,pep8,auto-indent,Python,Vim,Pep8,Auto Indent,我已经用Vim编写Python有一段时间了,但有一件事我还没有弄清楚如何将它设置为自动缩进到最后一个打开的paren级别 根据pep8,如果你有一个打开的paren,你需要将行打断以容纳80列,那么你应该在该打开的paren继续下一行。例如: calling_some_really_long_function(that, has, way, too, many, arguments, to, fit, on, one, line
calling_some_really_long_function(that, has, way, too, many, arguments, to, fit,
on, one, line)
显然,这是一个疯狂的例子,但这就是在python中如何中断行的方法
我真正想做的是设置Vim,这样当我键入fit,
时,它会将我的光标放在打开的paren右侧的下一行上,这样我就可以只键入on,
等,而不是事先键入
和
键的组合
我不认为我会信任Vim中python代码的自动格式化程序,但如果它也起作用的话,我会给它加分。使用
gq
,或者在整个选择中使用一个可视块,或者使用一个移动,比如gqq
或gqj这可以稍微细化一点,但应该可以在99%的时间内工作。将此添加到.vimrc中:
function! PythonEnterFunc()
let l:li = getline('.')
execute "normal! a\<Cr>"
if l:li =~ '([^)]*$'
let l:pos = stridx(l:li, '(') + 1
for i in range(l:pos)
execute "normal! a\<Space>"
endfor
endif
endfunction
au FileType python inoremap <Cr> <Esc>:call PythonEnterFunc()<CR>a
函数!PythonEnterFunc()
设l:li=getline('.'))
执行“正常!a\”
如果l:li=~'([^)]*$'
设l:pos=stridx(l:li,“(”)+1
对于范围内的i(l:pos)
执行“正常!a\”
外循环
恩迪夫
端功能
au FileType python inoremap:调用PythonEnterFunc()a
至少从V7.0开始,它就包含在Vim中:
请参阅usr/share/vim/vim80/indent/python.vim
(第74行)中的以下代码片段
函数GetPythonIndent(lnum)
...
括号内时:如果在括号下的第一行添加
“两个'shiftwidth',否则与前一行相同。
“i=(a)
“+b
“+c)
调用游标(a:lnum,1)
设p=searchpair(“(\\{\\\[”,“,”)\\\\\\\\],“bW”,
\“行('.')<”(a:lnum-s:maxoff)。”?虚拟:
\“synIDattr(synID(line('.')、col('.')、1)、‘name')”
\.“=~'\\(注释\\\\\\\\\\\\\\\\\\\\\\\\)$”)
如果p>0
如果p==plnum
“当开始在括号内时,只缩进一个‘shiftwidth’。
设pp=searchpair(“(\\{\\\[”,“,”)\\\\\\\\],“bW”,
\“行('.')<”(a:lnum-s:maxoff)。”?虚拟:
\“synIDattr(synID(line('.')、col('.')、1)、‘name')”
\.“=~'\\(注释\\\\\\\\\\\\\\\\\\\\\\\\)$”)
如果pp>0
返回缩进(plnum)+(存在(“g:pyindent\u nested\u paren”)?eval(g:pyindent\u nested\u paren):shiftwidth()
恩迪夫
返回缩进(plnum)+(存在(“g:pyindent\u open\u paren”)?求值(g:pyindent\u open\u paren):(shiftwidth()*2))
恩迪夫
如果plnumstart==p
返回缩进(plnum)
恩迪夫
返回点
恩迪夫
...
似乎对.vim/indent/python.vim
文件进行了修改,可以实现这一点。该插件对我来说效果很好。
function GetPythonIndent(lnum)
...
" When inside parenthesis: If at the first line below the parenthesis add
" two 'shiftwidth', otherwise same as previous line.
" i = (a
" + b
" + c)
call cursor(a:lnum, 1)
let p = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
\ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
\ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
\ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
if p > 0
if p == plnum
" When the start is inside parenthesis, only indent one 'shiftwidth'.
let pp = searchpair('(\|{\|\[', '', ')\|}\|\]', 'bW',
\ "line('.') < " . (a:lnum - s:maxoff) . " ? dummy :"
\ . " synIDattr(synID(line('.'), col('.'), 1), 'name')"
\ . " =~ '\\(Comment\\|Todo\\|String\\)$'")
if pp > 0
return indent(plnum) + (exists("g:pyindent_nested_paren") ? eval(g:pyindent_nested_paren) : shiftwidth())
endif
return indent(plnum) + (exists("g:pyindent_open_paren") ? eval(g:pyindent_open_paren) : (shiftwidth() * 2))
endif
if plnumstart == p
return indent(plnum)
endif
return plindent
endif
...