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
<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:
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:
Which will map hitting ” and ‘w’ at the same time to replace your command line with