201

How do I replace a word under the cursor in Vim.

So instead of using dw then i then the word and then Esc, is there a simpler combination to replace the word under the cursor?

Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
sayth
  • 6,696
  • 12
  • 58
  • 100

6 Answers6

346
ciw

(change inner word) will change the whole word under the cursor. Compare with

cw

which will only change the word from the current cursor position. For more info see this SO question/answer.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 1
    How to deal with the situation if the word contains dots or slashes (e.g. `/usr/bin/bash` or `1222.333`)? It looks like vim interprets `/` and `.` as end of the word and won't select the entire string. – Alexander Cska May 01 '20 at 21:47
  • 3
    @AlexanderCska you might want to try `cW` or `ciW` (notice uppercase W) – ShellCode Jan 17 '22 at 13:26
112

I often find myself wanting to replace one word with another that I have yanked from elsewhere. The problem with the other solutions is that if you attempt to change a word, then your next paste will be that word that you deleted with cw.

Try the following:

1 "This first word should overwrite the second"

yiw     yank inner word (copy word under cursor, say "first").
...     Move the cursor to another word (say "second").
viwp    select "second", then replace it with "first". 

Hope that's what you were looking for.

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
Ethereal
  • 2,604
  • 1
  • 20
  • 20
  • 8
    It works nicely but only if replacement has to be done `once`. If i have to replace couple of `second' by `first` (in your example), copied word becomes `second` after the first replacement. – ViFI Aug 06 '18 at 20:53
  • 2
    @ViFI, `viw"0p` instead of just `viwp` would help you if want to paste more than once. Basically, the same as `viwp` but with `"0` before pasting, which won't overwrite the buffer – Peter Jul 18 '21 at 00:10
34

ciw

c   change
iw  inner word

This will delete the word under the cursor (even if the cursor is somewhere in the middle of the word) and enter insert mode.


Also see Vim's documentation for reference:

:help c
:help text-objects
Xophmeister
  • 8,884
  • 4
  • 44
  • 87
19

If you want to change a word with a previously yanked word, there's another solution to viwp (once you have yanked the first word).

ciw removes the previous word and puts you in insert mode where it was. But then you can use ctrl+r, 0 to insert the contents of register 0 (which contain the previously yanked word).

So:

yiw
[move to next word]
ciw
ctrl+r
0

This works better than viwp because after the first usage you can then repeatedly perform the replacement with .. It also doesn't switch into visual mode and highlight briefly.

Connor
  • 1,226
  • 1
  • 10
  • 18
  • 1
    Not sure what you mean that it does not change modes, opposite seems to be the case, but maybe that's what you wanted, to remain in insert mode. But a big advantage is that this (contrary to `viwp`) can be done several times with just one yanking. Thanks! – Palo Jul 28 '18 at 12:37
  • Oh you're right, I was thinking of going into visual mode, edited. I didn't realize that was a limitation of `viwp` - even `.` doesn't duplicate it. – Connor Aug 01 '18 at 15:58
  • 3
    This is by far the best solution when we already have yanked the replacement word. – mljrg Jul 04 '19 at 16:02
  • 1
    This is the answer! Ability to use `.` with this is great! – izzy Nov 03 '19 at 15:18
1

Try cw - as in 'change word'.

Use http://linuxmoz.com/vi-commands-cheat-sheet/ or any other cheat sheet as a reference.

0

To copy word under cursor press: bvey

To replace the word under cursor press: bvep

ABN
  • 1,024
  • 13
  • 26