104

I want to write a command that specifies "the word under the cursor" in VIM. For instance, let's say I have the cursor on a word and I make it appear twice. For instance, if the word is "abc" and I want "abcabc" then I could type:

:s/\(abc\)/\1\1/

But then I'd like to be able to move the cursor to "def" and use the same command to change it to "defdef":

:s/\(def\)/\1\1/

How can I write the command in the commandline so that it does this?

:s/\(*whatever is under the commandline*\)/\1\1

8 Answers 8

162

While in command-line mode, CTRL+R CTRL+W will insert the word under the cursor.

See the help for c_CTRL-R for a listing of all the other special registers:

:help c_CTRL-R
0
83

<cword> is the word under the cursor (:help <cword>).

You can nmap a command to it, or this series of keystrokes for the lazy will work:

b #go to beginning of current word
yw #yank to register

Then, when you are typing in your pattern you can hit <control-r>0<enter> which will paste in your command the contents of the 0-th register.

You can also make a command for this like:

:nmap <leader>w :s/\(<c-r>=expand("<cword>")<cr>\)/

Which will map hitting '' and 'w' at the same time to replace your command line with

:s/\(<currentword>\)/
2
  • 20
    I do appreciate the great answer, but I would probably suggest the yiw over byw only because of muscle memory. In the scenario where your cursor already happens to be on the first character in the word, you must omit the b and simply type yw vs you may omit the i in yiw. Accidents can happen, and fixing a mistake is certainly more keypresses :) Commented Jan 3, 2012 at 23:41
  • Years later, an addition: you might consider binding a key to just the part that expands <cword>, using cnoremap. I just found this answer, and I have bound <C-W> to <C-R>=expand("<cword>")<CR> which makes it possible to type, e.g., :s/\(<C-W>\)/\1\1/ and have the cword inserted for me.
    – aghast
    Commented Jan 1, 2022 at 9:02
34
yiwP

yiw: Yank inner word (the word under the cursor). This command also moves the cursor to the beginning of the word.

P: Paste before the cursor.

You can then map the e.g.: < ALT > - D to this command:

:nmap < ALT >-D yiwP
1
  • 4
    The question was specifically about specifying "the word under the cursor" from Vim's commandline, not from command mode Commented Dec 9, 2014 at 8:13
15

Another easy way to do this is to use the * command.

In regular mode, when over a word, type

*:s//\0\0<Enter>

* makes the search pattern the current word (e.g. \<abc\>).

:s// does a substitution using the current search pattern, and \0 in the replacement section is the matched string.

You can then repeat this behaviour, say over word "def", by either typing the same again, or by typing

*@:

@: just repeats the last ex command, without a need for an <Enter>, in this case the substitution.

You can also record a quick macro to do this using the q command

qd*:s//\0\0<Enter>q

Then repeat it to your hearts content by typing

@d

when over a word you want to double. As this is only one character less than the prior solution, it may not be worth it to you - unless you will be doing other ex-commands between the word-doubling, which would change the behaviour of @:

1

You need to escape the backslashes within the mapping. You can also include the substitution string within the mapping.

:nmap <leader>w :s/\\(<c-r>=expand("<cword>")<cr>\\)/\\1\\1<cr>
1

@user11211 has the most straightforward way to duplicate the word under cursor:

yiwP

yank inner word (moves cursor to start of word), paste (before cursor).

eg. straigh[t]forward ----> straightforwar[d]straightforward

[] is cursor

To elaborate...

You probably want to have the cursor following your duplicated word:

yiwPea

straigh[t]forward ----> straightforwardstraightforward[]

NOTE:

yiw

is yank inner word (without whitespace)

yaw

is yank all word (including trailing whitespace).

yawPea

is therefore duplicate word including whitespace, and position cursor.

straigh[t]forward ----> straightforward straightforward[]

2
  • The question was specifically about specifying "the word under the cursor" from Vim's commandline, not from command mode. Commented Dec 9, 2014 at 8:12
  • 1
    You can use any of the normal (command mode) commands above within command mode, by simply preceding them with :normal ... for example, :normal yiwP will do the same as yiwP in normal mode. This is of course, the slower way to do what you were asking. Commented Dec 9, 2014 at 17:40
1
ywPx

will do what you describe.

ywPxw

will also advance the cursor to the next word.

2
  • The question was specifically about specifying "the word under the cursor" from Vim's commandline, not from command mode Commented Dec 9, 2014 at 8:13
  • 1
    Please describe answers like that, every letter in the command. Commented Jun 11, 2019 at 13:46
-1
" count word  (case sensitive)
nmap <F4> :%s/\(<c-r>=expand("<cword>")<cr>\)//gn<cr>
2
  • could you comment on what this does? Commented May 15, 2009 at 16:59
  • 1
    This uses the n flag to the :substitute command to print out how many times the word under the cursor appears in the current buffer. Quite clever, but not actually an answer to this question.
    – Jacob
    Commented May 26, 2012 at 14:00

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.