2012-03-16 17 views
5

Quindi, consente di dire che ho un pezzo di codiceVim commento continuazione alla fine della riga

int i = 0; // Do some junk here<cursor is here> 

if(i == 0){ 
    blahblahblahblah; 
} 

blahblahblah; 

E 'possibile dire a Vim che quando il colpo entra, lo voglio fare risultato in quanto segue:

int i = 0; // Do some junk here 
      // <cursor is here> 

if(i == 0){ 
    blahblahblahblah; 
} 

blahblahblah; 

So che lo farà per un commento che si trova su una linea tutto da solo, ma non riesco a capire in questo modo.

+0

Avete alcuni plugin per quel tipo di codice installato? È solo Java, C, C++, ...? – mliebelt

+0

Principalmente C. Nessun plugin per la gestione speciale –

risposta

4

Non so se ci sia un plugin per questo (ma ce ne può essere uno), ma la seguente mappatura dovrebbe fare il trucco per aggiungere una linea premendo enter (ci sono molti più modi per aggiungere la linea):

" Function that adds new line starting with comment symbol if line does not 
" start with comment, but contains it. 
function! s:NewLine(comsymb) 
    let line=getline('.') 
    " Check whether we are in comment. Assumes syntax highlighting is working 
    " correctly. Remove these lines if you never write “//” in a string literal 
    if empty(filter(synstack(line('.'), min([col('.'), col('$')-1])), 
       \ 'stridx(tolower(synIDattr(v:val, "name")), "comment")!=-1')) 
     return "\n" 
    endif 
    let cidx=stridx(line, a:comsymb) 
    if cidx==-1 
     " No comments 
     return "\n" 
    elseif cidx==0 || line[:(cidx-1)]!~#'\S' 
     " This assumes that vim own continuation works correctly: do not do work 
     " that can be done by something else 
     return "\n" 
    endif 
    " Preserve tab indentation if any, correctly replace non-indent tabs with 
    " spaces 
    let nextline=substitute(line[:(cidx-1)], '\v^(\s*)(\S.*)$', 
       \   '\=submatch(1).'. 
       \    'repeat(" ", strdisplaywidth(submatch(2), '. 
       \           indent('.').'))', 
       \   'g').a:comsymb 
    " Preserve presence of a space after comment start mark 
    if line[cidx+len(a:comsymb)] is# ' ' 
     let nextline.=' ' 
    endif 
    return "\n".((col('.')<col('$'))?("\e\"_c0"):("\<C-\>\<C-o>\"_d0")).nextline 
endfunction 

inoremap <expr> <CR> <SID>NewLine('//') 
+0

Soluzione superba, ZyX, bravo! –

+0

Questo è abbastanza sorprendente. Grazie! E mi dispiace per la risposta ritardata. Molto occupato –

Problemi correlati