How can I visually select all text highlighted by a search in order to run a command on it? And if it's possible, how can I capitalize the selection?
So you're referring to the search results that get highlighted when you have the 'hlsearch'
option set. So, in effect, these are the search results.
Your example has one more quirk to it, which is the fact that you're ending with a \zs
, so in effect what Vim is highlighting there is the character following the search term and not the match itself (highlighted the match would not be possible, since it has zero length, so it would be invisible.)
But assuming you're using a search that has matches with a length, such as:
/- \zs.
Then the question is how to manipulate them, perform and action on them.
You asked about Visual mode. In Vim, Visual mode is a contiguous block of text, so you couldn't really use a single Visual selection on all your matches. In theory, it should be possible to use a Visual selection for each match, one at a time, so you could manipulate it and move on to the next selection.
If going through the matches one by one is acceptable, then what you want is the gn
operator, which will find the next match (much like the n
motion does), but it will start Visual mode and select the match itself.
As :help gn
explains it:
Search forward for the last used search pattern, like with
n
, and start Visual mode to select the match. If the cursor is on the match, visually selects it. If an operator is pending, operates on the match. E.g.,dgn
deletes the text of the next match. If Visual mode is active, extends the selection until the end of the next match.
I'd say the more general answer to the question of "how to perform an action on search matches" is to use the :s
(: substitute
) command, which takes a pattern and substitutes matches with a replacement.
You can even omit the pattern, with :s//
followed by a replacement, in which case Vim will use the most recently used pattern, which is the one highlighting the matches on your text. So if you start with a search and decide you want to act on those results, you can simply use :%s//
and follow it with a replacement.
There are special replacements that you can use.
For starters, some characters are special, see :help sub-replace-special
which will tell you how &
will be replaced by the matched pattern and \U
will turn the next string into uppercase.
So to turn the matches of your previous search into uppercase, you could use:
:%s//\U&/g
The special characters and backslash sequences can take you so far, but sometimes you might need more than them. In those cases, you can use full Vimscript expressions. See :help sub-replace-expression
, which describes how to use them.
For the same example, of turning the matches into uppercase:
:%s//\=toupper(submatch(0))/g
This should take care of even more complex cases, assuming you're willing to learn more on Vimscript functions and expressions to help you express complex substitutions.
-
1For gn, I would use
gUgn....
, though the\U
is nicer and what I would have first thought of. – D. Ben Knoble♦ Sep 18 '20 at 12:01 -
1
:%s/- \zs./\U&
see:help sub-replace-special
for both\U
and&
. – filbranden♦ Sep 18 '20 at 4:14