2013-06-02 12 views
9

Per le lingue con {} per indicare i blocchi, vim ha l'onnipotente chiave %. Qual è l'equivalente movimento per il codice Python? O almeno per passare alla riga successiva/precedente con lo stesso trattino.Equivalente di% movement per file Python

+0

Perché non scaricare un plug-in Python? – jamylak

+0

* O almeno per passare alla riga successiva/precedente con lo stesso trattino * - potresti elaborare ciò che intendi con questo? – timss

risposta

4

Il vim wiki suggeriscono attacchi come ad esempio:

nnoremap <M-,> k:call search('^'. matchstr(getline(line('.')+1), '\(\s*\)') .'\S', 'b')<CR>^ 
nnoremap <M-.> :call search('^'. matchstr(getline(line('.')), '\(\s*\)') .'\S')<CR>^ 

Oltre ad offrire una soluzione più completa:

" Jump to the next or previous line that has the same level or a lower 
" level of indentation than the current line. 
" 
" exclusive (bool): true: Motion is exclusive 
" false: Motion is inclusive 
" fwd (bool): true: Go to next line 
" false: Go to previous line 
" lowerlevel (bool): true: Go to line with lower indentation level 
" false: Go to line with the same indentation level 
" skipblanks (bool): true: Skip blank lines 
" false: Don't skip blank lines 
function! NextIndent(exclusive, fwd, lowerlevel, skipblanks) 
    let line = line('.') 
    let column = col('.') 
    let lastline = line('$') 
    let indent = indent(line) 
    let stepvalue = a:fwd ? 1 : -1 
    while (line > 0 && line <= lastline) 
    let line = line + stepvalue 
    if (! a:lowerlevel && indent(line) == indent || 
      \ a:lowerlevel && indent(line) < indent) 
     if (! a:skipblanks || strlen(getline(line)) > 0) 
     if (a:exclusive) 
      let line = line - stepvalue 
     endif 
     exe line 
     exe "normal " column . "|" 
     return 
     endif 
    endif 
    endwhile 
endfunction 

" Moving back and forth between lines of same or lower indentation. 
nnoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR> 
nnoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR> 
nnoremap <silent> [L :call NextIndent(0, 0, 1, 1)<CR> 
nnoremap <silent> ]L :call NextIndent(0, 1, 1, 1)<CR> 
vnoremap <silent> [l <Esc>:call NextIndent(0, 0, 0, 1)<CR>m'gv'' 
vnoremap <silent> ]l <Esc>:call NextIndent(0, 1, 0, 1)<CR>m'gv'' 
vnoremap <silent> [L <Esc>:call NextIndent(0, 0, 1, 1)<CR>m'gv'' 
vnoremap <silent> ]L <Esc>:call NextIndent(0, 1, 1, 1)<CR>m'gv'' 
onoremap <silent> [l :call NextIndent(0, 0, 0, 1)<CR> 
onoremap <silent> ]l :call NextIndent(0, 1, 0, 1)<CR> 
onoremap <silent> [L :call NextIndent(1, 0, 1, 1)<CR> 
onoremap <silent> ]L :call NextIndent(1, 1, 1, 1)<CR> 

che utilizza:

  • [l e ]l salto al precedente o la riga successiva con lo stesso livello di indentazione della corrente linea.
  • [L e ]L passare alla riga precedente o successiva con un livello di indentazione inferiore rispetto alla riga corrente.
0

È sempre possibile utilizzare i commenti per inserire {} (non farebbe questo se altri condividono):

class Game(object): # { 
    def __init__(self, num, end): # { 
     pass 
    # } 
# } 
+9

Ugh, spero di non incontrare mai uno stile di codifica del genere. – timss

+0

Appena ottenuto un voto negativo senza commenti. Capisco che lo stile di codifica non è piacevole. Ho detto che non lo farei se condivido il codice. Lo stile di codifica consente il movimento '%' in vim. – cforbish

3

Vim ha le { e } movimenti per spostarsi tra "punti" e il corrispondente p di testo -object (vip, dap).

Il ftplugin predefinito Python anche (ri) definisce [[ e ]] per passare alla classe successiva e precedente e ]m e [m per passare al metodo successivo e precedente.

Il indent-object è molto utile pure per Python.

+0

perdonami, ma che cos'è un oggetto-indent? Link per favore? – Ayman

+0

Mi dispiace, ho avuto l'URL nella mia appunti ma ho dimenticato di metterlo nella risposta. È molto facile da trovare, però. – romainl

+0

Piuttosto interessante ma l'oggetto indent non funziona in modalità normale. C'è un modo per costringerlo a lavorare in modalità normale? –