If you do not mind pressing a different combination than cw
, then I believe you can get what you want easily, by defining a shortcut. What you can do is to place the following
nmap <leader>a "1yiw:let @/=@1^Mciw
in your .vimrc
file. Note that ^M
is a capture of return key, and not the characters ^
and M
. You get it by pressing Ctrl+V in insert mode followed by Enter. Now, with this defined, pressing <leader>a
will do the same as cw
, but now the word (prior to the change) will be in your "last search register" (that is, the /
register), so pressing n after you have made the change and returned to normal mode will get you to next occurrence. Likewise, pressing N will get you to the previous occurrence.
The shortcut You can of course choose a different shortcut than <leader>a
, but I just find that this is a very accessible shortcut. Personally I have set <leader>
to ,
so that I just need to press , followed by a, but this is because I do not use an American keyboard, so that my \ is hidden away somewhat (I believe \ is the default <leader>
).
Explanation The idea is really simple. Yank the word under the cursor to the search register /
and then do cw
. Thus nmap <leader>a "/yciw
was what I was aiming for, but unfortunately I do not think that you can yank to the search register, so the workaround is to yank the word under the cursor to the 1
register, then set the /
register equal to that, and then do cw
. So "1yiw
is the part that yanks the inner word under the cursor to the register 1
. If you do not know about inner words, you can check out :help iw
. Afterwards, :let @/=@1
follows by a capture of Enter puts the contents of the 1
register into the /
. At this point n and N works as next and previous occurrence of the word under the cursor, and then a ciw
is done so that the word under the curser is deleted and Vim is put into insert mode.
word versus inner word I just realised that I assumed that you wanted the entire word under the curser, and not just from the point of the curser and on. This is why I used iw
instead of w
everywhere. If this is not the case, then you should use
nmap <leader>a "1yw:let @/=@1^Mcw
instead. If the cursor is on the first letter of the word, there is no difference.
disclaimer This solution will probably not work well if you try to use it on something that contains stuff different from letters, for example by having the cursor stand on a \
or by replacing iw
by iW
, ib
, is
or ip
or something (though I do not know why you would want this feature with whole sentences)
*
search. Especially it makes*
visually select the current word without moving your cursor. Once it is installed you could do*ciw
to put the old word in the search register and change the word. Then you can usen
and.
.n
won't work.