Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify "the word under the cursor" on VIM's commandline?

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 
like image 583
Nathan Fellman Avatar asked Sep 07 '08 18:09

Nathan Fellman


People also ask

How to replace word under cursor Vim?

Move the cursor to a word, then press F8 to change all occurrences of that word. Then move to another word and press F8 again.

How do I copy one word in Vim?

You can use a movement command or up, down, right, and left arrow keys. Press y to copy, or d to cut the selection. Move the cursor to the location where you want to paste the contents. Press P to paste the contents before the cursor, or p to paste it after the cursor.


1 Answers

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 
like image 96
César Alforde Avatar answered Oct 26 '22 12:10

César Alforde