2012-02-29 5 views
12

मैं थोड़ी देर के लिए विम के साथ प्रोग्रामिंग पाइथन रहा हूं लेकिन एक बात यह नहीं है कि यह कैसे पता चल सके कि इसे अंतिम खुले माता-पिता के स्तर पर ऑटो इंडेंट में कैसे सेट किया जाए।विम: प्रवेश करते समय खुले माता-पिता या ब्रैकेट को कैसे इंडेंट करें?

पेप 8 के मुताबिक यदि आपके पास खुले माता-पिता हैं और आपको 80 कॉलम में फिट करने के लिए लाइन तोड़ने की जरूरत है तो आपको उस खुले माता-पिता की अगली पंक्ति जारी रखना होगा। उदाहरण:

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

जाहिर है यह एक पागल उदाहरण है, लेकिन है कि कैसे आप अजगर में अपने लाइनों को तोड़ने के लिए माना जाता कर रहे हैं।

मैं वास्तव में ऐसा करने में सक्षम होना चाहता हूं जो कि मैं वास्तव में करना चाहता हूं ताकि जब मैं fit,<cr> टाइप करूं और यह मेरे कर्सर को खुले माता-पिता के दाईं ओर अगली पंक्ति पर रखे, तो मैं केवल on, टाइप कर सकता हूं आदि <tab> और <space> कुंजी के कुछ संयोजन के बजाय पहले।

मुझे नहीं लगता कि मैं कभी भी विम में पायथन कोड के लिए ऑटो-फॉर्मेटर पर भरोसा करता हूं लेकिन बोनस अंक भी काम करता है।

+1

से निम्नलिखित स्निपेट मैं इस सवाल को ऑफ-विषय के रूप में बंद करने के लिए मतदान कर रहा हूं क्योंकि यह केवल लिंक को आकर्षित कर रहा है। –

+0

[हाइनेक/विम-पायथन-पेप 8-इंडेंट] (https://github.com/hynek/vim-python-pep8-indent) प्लगइन मेरे लिए अच्छा काम करता है। – Matt

+0

[Ydgrasil] (http://orchistro.tistory.com/236 "Ygdrasil") में '.vim/indent/python.vim' फ़ाइल के लिए एक संशोधन है, जो ऐसा ही करेगा। – BurntSushi5

उत्तर

0

उपयोग gq, या तो एक दृश्य ब्लॉक के साथ पूरे चयन से अधिक है, या एक आंदोलन है, जैसे gqq या gqj

1

साथ इसमें कुछ समय परिष्कृत किया जा सकता है, लेकिन समय के 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 
0

यह V7.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 
    ... 
संबंधित मुद्दे