2

Background

It's nice to be able to type * in normal mode and automatically have Vim search for the word under the cursor.

Question

Is there an analogous command for executing a find/replace?

E.g. the cursor is on "the" and the result is :%s/\<the\>/ so that I can easily type the only/g for the full find/replace command :%s/\<the\>/the only/g.

Update/clarification

I have learned from a comment on this question that a solution might involve, to quote that comment,

<C-r><C-w> and <C-r><C-a> on the : command-line to insert the word (respectively the WORD) under the cursor.

This certainly works and could be used to write a custom command to do what I am asking (e.g., nnoremap ** :%s/<c-r><c-w>).

Is that the best way to do this? Or is there a native Vim command that already does what I am looking for?

2
  • 4
    I believe that's the best way to do it (:nnoremap <leader>s :%s/\<<C-r><C-w>\>/) (or alternatively, <C-r><C-a> to use WORD)
    – r_31415
    Commented Oct 2, 2022 at 4:40
  • 3
    An alternative way is to do your search first (using *) and then do your replace without search pattern :%s//the only/g it will default to the previous search. Commented Oct 2, 2022 at 6:55

2 Answers 2

4

You could do the replace in two steps:

  1. You prepare a search command using * (/\<the\>)
  2. You do your replace with an empty search (:%s//the only/g)

The replace will default the search to the last one.

It has the advantage over the CtrlrCtrlw trick that the word boundary are included in the search.

This way you can test your search before you use it for doing the replacement which I like.

3

One nice version of this is *cgn replacementEsc. This is repeatable with ., or n. to see what will change before changing it (kind of like /gc flags for :substitute). If you wanted to do a global replacement for sure, the tricks with :%substitute/<C-r><C-w>/…/… are better, but you could also use the cgn command with (e.g.) 9999..

3
  • Sorry for my confusion, but I don't quite understand your answer. In particular, what is *cgn? (I tried :help *cgn and :help cgn, but nothing came up.) And what role does the Escape key play in the solution? Commented Oct 3, 2022 at 16:33
  • 1
    @AlexRoberts try :help gn, which selects or operates on the next match. So cgn changes the next match (like ciw; it leaves you in Insert mode, so Escape is necessary to "end" the edit by returning to Normal mode). Then *cgn is the same but after searching for the word under the cursor, so that gn selects matches for the same word. I encourage you to try it out. The important property is that sequences <operator>gn are repeatable for any search pattern
    – D. Ben Knoble
    Commented Oct 3, 2022 at 17:22
  • Thank you! Now I understand. That does seem very useful, especially the .-repeatability, as you say. And now I understand what you meant by 9999.; that should usually be enough to do the trick! Commented Oct 3, 2022 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.