2012-02-29 7 views
12

私はPythonをしばらくの間Vimでプログラミングしてきましたが、私はそれを行う方法を理解できていませんでした。最後に開いたparenのレベルに自動インデントするように設定しました。Vim:打つときに開いているパンやブラケットにどのようにインデントするのですか?

pep8によれば、開いているparenがあり、80列に収まるように行を分割する必要がある場合、開いているparenで次の行を続けることになっています。例:

calling_some_really_long_function(that, has, way, too, many, arguments, to, fit, 
            on, one, line) 

これは明らかに狂った例であるが、それはあなたがpythonであなたの線を破ることになっている方法です。

私が実際にできることは、Vimを設定して、fit,<cr>と入力すると、開いているparenの右側の次の行にカーソルが置かれるので、入力することができます。on,あらかじめ<tab><space>の組み合わせではなく、

私はVimでPythonコードの自動フォーマッタを信頼するとは思えませんが、それがうまくいくとすればボーナスポイントもあります。

+1

から次のスニペット私はリンクの唯一の答えを集めているので、この質問を話題として閉じようとしています。 –

+0

[hynek/vim-python-pep8-indent](https://github.com/hynek/vim-python-pep8-indent)プラグインはうまく動作します。 – Matt

+0

[Ydgrasil](http://orchistro.tistory.com/236 "Ygdrasil")は、これを行う '.vim/indent/python.vim'ファイルを修正したようです。 – BurntSushi5

答えて

0

VISUALブロックと全セレクションにわたって、又は移動のいずれかを使用gqgqq又はgqj

1

ように、このビットを改良することができるが、時間の99%を動作する必要があります。あなたの.vimrcでこれを追加します。

それはV7.0以降のVimには含まれてい
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 
0

少なくとも:

を参照してくださいusr/share/vim/vim80/indent/python.vim(ライン74)https://github.com/vim/vim/blob/master/runtime/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 
    ... 
関連する問題