:help :global
is an incredibly cool command.
One thing I like to do with :global
is to list lines matching a given pattern in the current file and use that to move around. It looks like this:
:g/let/#
7 let &path .= 'src/**,public/**,static/**'
31 unlet b:gqview
33 nmap <silent> GQ :let b:gqview = winsaveview()<CR>:set opfunc=Format<CR>g@
[...]
:
I like it. I use it all the time.
What if there was a way to persist the result of :[range]g/foo/#
and use it to move around?
Well, that's what :[range]Global <pattern>
does: it simulates :[range]g/<pattern>/#
and populates the location list with the result, allowing us to navigate that list with :help :lnext
, :help :lprevious
, filter it with :help :Lfilter
, operate on each line with :help :ldo
, etc.
And :[range]Global! <pattern>
does the same but for :[range]g!/<pattern>/#
/:[range]v/<pattern>/#
.
Neat.
List all lines with foo
:
:[range]Global foo
List all lines without foo
:
:[range]Global! foo
FWIW, I made a significant update to this snippet: