vim - Capture Line numbers of lines matching a search -


example:

search:

/lastword\s*\n\zs\(\s*\n\)\{6}\ze\s*startword 

this search searches 6 empty lines between line ending "lastword" , line starting "startword"

i catch linenumbers of empty lines matching search.

is there way in vim?

you can use :g , :number (or :# short) print out lines line numbers.

:g/lastword\s*\n\zs\(\s*\n\)\{6}\ze\s*startword/# 

to capture content have use :redir redirect output somewhere else. in case below redirect unamed register (@")

:redir @"|execute 'g/pattern/#'|redir end 

note: must use :execute :g otherwise redir end executed on each matching line :g command.

now going print via :# starting line not want (we want empty lines between foo , bar). can use range :# command accomplish this.

:redir @"|execute 'g/foo\_.*bar/+,/bar/-#'|redir end 

the range +,/bar/- translate start next line (+) , search bar (/bar/) subtract 1 line (-). can simplify know number of empty lines should 6.

:redir @"|execute 'g/foo\_.*bar/+#6'|redir end 

we can take further putting content new buffer , remove lines.

:redir @"|exe 'g/pattern/+#6'|redir end|new|pu|%s/\d\+\zs.*//|%le|g/^$/d 

there lot going on here, idea capture output, open new buffer, paste content, , clean output.

alternative

alternatively awk might make bit easier. run following on current vim buffer:

:%!awk '/lastword\s*$/,/^\s*startword/ { if($0 ~ /^\s*$/) { print nr} }' 

for more see:

:h :g :h :redir :h :exe :h :range :h :new :h :pu :h /\zs :h :le :h :d 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -