I know there is one way to search and replace variable occurrences across a source file.
But is there any better way by which if I rename a variable at one place it gets renamed at other places as well without search and replace or substitute?
I know there is one way to search and replace variable occurrences across a source file.
But is there any better way by which if I rename a variable at one place it gets renamed at other places as well without search and replace or substitute?
You can just use the usual workflow search
and replace
:
/original
cwreplaced
n.n.
You can take also advantage of the gn
motion:
/original<CR>
cgnreplaced<ESC>
.....
cgn
will c
hange the n
ext matched pattern, so instead of using n.n.
to go to next
and repeat
you can just ..
which means replace next
.
gn
means "search for next occurence and start a visual selection over it", you can read more about it on the doc: :h gn
.
#
and *
. With :set hlsearch
you will see what is highlighted and then do usual cgn
thing.
Commented
May 21, 2021 at 5:56
t
or f
motion to replace prefix you want and n.
to move to the next occurance
Commented
May 21, 2021 at 5:59
You can use a substitute:
:%s/original/replaced/g
Will s
ubstitute across the entire file (%
) original
by replaced
. And will replace multiple occurrences on the same line g
.
If you are not confident about automatically replacing, you can add the c
flag to ask for confirmation each time.
I'd like to add another option.
You can do /original
and then do :%s//replacement
.
(Use /c
at the end if you want confirmation).
Although you are pressing 3 more keys, this has an advantage over doing just %s/original/replacement
because it provides a visual cue that your search is correct.