2022年8月14日 星期日

Modify multiple lines after matching lines in Vim

Just note down issues i faced in vim.

Problem: (How to add prefix "#" before ever lines between "#For testing" and " ; ?)

Command: 
 insert # prefix      :g/^# For testing/+1,/^"/ norm! I#
 remove # prefix   :g/^# For testing/+1,/#^"/ norm! ^x

power g: :g/Pattern/cmd
pattern here is "^# For testing"

cmd here is +1, /^"/ norm! I#,  where +1, /^"/ is a ranges command which means that from next line of current line to line of matching pattern (which is ^" here).
norm! I# means that insert "#" at each lines in normal mode.
norm! ^x means that remove first character at each lines in normal mode . (^: begin of a line,  x: delete character)


Reference: 
    https://stackoverflow.com/questions/73308475/vim-match-multiple-lines-and-substitute-on-each-line#comment129516114_73313112
    https://vim.fandom.com/wiki/Ranges (Ranges )
    https://vim.fandom.com/wiki/Power_of_g (Power of G)

``` Before modification 
# For testing
Space 5 ;
Width 8 ;
Length 10 ;
" ;

# normal line
Space 5 ;
Width 8 ;
Length 10 ;
" ;

# For testing
Space 5 ;
Width 8 ;
Length 10 ; 

" ; 
```


``` After modification 
# For testing
#Space 5 ;
#Width 8 ;
#Length 10 ;
#" ;

# normal line
Space 5 ;
Width 8 ;
Length 10 ;
" ;

# For testing
#Space 5 ;
#Width 8 ;
#Length 10 ; 
#" ; 

```

沒有留言:

張貼留言