commenting - Make a ascii box around the code block in Vim -


is possible create ascii comment box around code block?

  • ascii box should smart enough extend box around maximum of code width. should clear trailing spaces.
  • notice, should not have column line @ beginning of code.
  • in code shown below, ; comment character.
  • the code block may contain comment lines.
  • work flow may
    1. pick code block in visual mode
    2. apply changes.

here example.

before

  ; convert radians             theta45 = (theta + 45.)/!radeg   theta90 = (theta + 90.)/!radeg   theta   = theta / !radeg         ey = ey * normal                  ; engineering shear strain       gxy = shear * exy              

after

; -----------------------------------------;   ; convert radians                     ;   theta45 = (theta + 45.)/!radeg           ;   theta90 = (theta + 90.)/!radeg           ;   theta   = theta / !radeg                 ;   ey = ey * normal                         ;                                            ;   ; engineering shear strain               ;   gxy = shear * exy                        ; ; -----------------------------------------; 

what have tried far

'<,'>s/^\(\s*\)\(.*\)$/\=join([submatch(1), ';', submatch(2), repeat('-', 50-len(submatch(1)) - len(submatch(2))), ';'], '') 

note selected visual block first.

issues it

  • it adds '-' character every line, instead of first , last line
  • it starts first line , ends last line, prefer have block before , after selected lines.
  • the trailing spaces not removed.
  • since have search highlight enabled, highlights whole visual block, after operation.

here how looks:

  ;; convert radians          ------------------;   ;theta45 = (theta + 45.)/!radeg------------------;   ;theta90 = (theta + 90.)/!radeg------------------;   ;theta   = theta / !radeg      ------------------;   ;ey = ey * normal              ------------------; ;--------------------------------------------------;   ;; engineering shear strain    ------------------;   ;gxy = shear * exy       ------------------------; 

while, not close want get. far!

thanks reading , help.

how this:

<esc>'>o;<esc>'<o;<esc>v'>j:norm 51a <c-v><esc>51|dr;<cr>:'<s/ /-/g<cr>:'>&&<cr> 

explanation:

so first, need add lines. <esc> visual mode, jump end of our previous selection , add newline semicolon on it. '>o;<esc>. then, jump beginning of previous selection, , add newline semicolon on upwards '<o;<esc>. now, need start visual mode on line, jump previous end, , 1 more line v'>j. right now, buffer looks this:

; here whole bunch of uneven lines ; 

and it's visually selected. then, need make every line padded 50 spaces. so,

:norm 51a <c-v><esc>51dr; 

will first add 51 spaces, jump 51st column, delete end, , replace 50th semicolon. our buffer looks this:

;                                                ; here                                             ;                                              ; whole bunch of                                 ; uneven                                           ; lines                                            ; ;                                                ; 

now, substitute spaces dashes on beginning of selection:

:'<s/ /-/g<cr> 

and same substitution on end of selection:

:'>&&<cr> 

now buffer looks this:

;-------------------------------------------------; ;here                                             ; ;are                                              ; ;a whole bunch of                                 ; ;uneven                                           ; ;lines                                            ; ;-------------------------------------------------; 

obviously, want wrap in mapping, function, or macro convenience.


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -