0

I am searching for an easy way to do search and replace things in vim as a "workaround" for refactoring.

On some other stackoverflow-question I found this snippet of code:

nnoremap gR gD:%s/<C-R>///gc<left><left><left>

I thought gD selects the current word under the cursor in the whole document and %s then search and replaces the matches after I typed the replace pattern. gc stands for globally with confirmation?!

However: This does not work as expected. For example in some python class of mine I can easy rename occurencies of self in the whole file but it does not work on method identifieres or field identifieres with a length of one.

def __init__(self):
    self.test = []
    self.A = []
    ...

So self could be changed using this snippet, but doing gR on the field A simply does not work.

How can I get this work to do an easy search and replace on one file?

Any tips on intelligent refactoring would be also appreciated. After hours of searching I just found plugins and workarounds which work mostly with C files, but I would also need at least Java, Python and PHP compatibility.

Thanks for every answer :)

UPDATE: I recently found out it just works if the initial selected pattern is on the beginning of the line. If the pattern is somewhere in the middle of the line it does not work...any ideas?

1 Answer 1

1

gD is "Go to global declaration", see :help gD.

The mapping:

  • jumps to the 1st occurrence of the word under the cursor in the buffer with gD,

  • populates the command-line with %s/, meaning "substitute in the whole buffer",

  • followed by the search pattern used by gD, inserted with <C-r>/,

  • an empty pair of slashes, //, for the replacement,

  • the flags gc that mean "do your magic on every match on the line and ask for confirmation",

  • and goes <left><left><left> to place the cursor between the //, ready for you to type the replacement.

That mapping does exactly what it claims to do, here, no matter how long the word under the cursor is.


Vim is not an IDE, don't expect anything "intelligent".

Sign up to request clarification or add additional context in comments.

9 Comments

thanks for the explanation and quick answer :) but really, if I press gR while cursor is on self it works as expected, doing the same thing on A from self.A it just highlights every A field in the file but does not offer the command-line with the populated search and replace stuff. The command line is simply empty...?
edit: one length long variables work, too BUT only if the initial occurence is at the start of a line. In addition to this: also longer words like append or stuff do not work if they are somewhere in the middle of a line...any ideas?
gD errors when the word under the cursor is the first occurrence in the buffer.
oh yeah, indeed...how to fix that?
I've changed it to nnoremap gR *:%s/<C-R>///gc<left><left><left> and it seems to work...can I live with this or do I produce any side effects using * ?
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.