I was trying to come up with something fairly short and found this novel approach (novel to me anyways...I'd never tried it before).
There is an inverse counterpart to :g
(global command): :v
. This will run specified commands on all lines that don't match the given pattern. Because it's really easy to invoke when a pattern is already in use (i.e. :v// {cmds}
, I was wondering if there's a way to break out of it easily at the first line it operates on (i.e. the first line below the cursor without the offending pattern). I found that :visual
does the trick. (See update, though.)
One added requirement, though, is that you need to give a range or the process will begin at the start of the file which we definitely don't want. So what you want is a range starting with the current line (.
) and going as far as the end of the buffer ($
)...
:.,$v// vis
and that's it. You'll be on the next line that doesn't have the pattern. It's easy to map it, too...
:nnoremap <leader>x :.,$v// vis<cr>
The nice thing with this approach is that you don't need to use a different search and thus you won't have to deal with getting your original search back as the active one. If there is a downside to it it may be a little slow depending on various factors (i.e. YMMV).
Update: A twist. :visual
is actually behaving as :e
and the above will not work cleanly if the file has unsaved changes. Do :w
before running the above and it will work as described.