<p>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:  </p>

<pre><code>:s/\(abc\)/\1\1/
</code></pre>

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

<pre><code>:s/\(def\)/\1\1/
</code></pre>

<p>How can I write the command in the commandline so that it does this?</p>

<pre><code>:s/\(*whatever is under the commandline*\)/\1\1
</code></pre>

[{'answer_id': 48657, 'author': 'hazzen', 'author_id': 5066, 'author_profile': 'https://Stackoverflow.com/users/5066', 'pm_score': 7, 'selected': True, 'text': '<p><code>&lt;cword&gt;</code> is the word under the cursor (:help <code>&lt;cword&gt;</code>).</p>\n\n<p>Sorry, I should have been more complete in this answer.</p>\n\n<p>You can nmap a command to it, or this series of keystrokes for the lazy will work:</p>\n\n<pre><code>b #go to beginning of current word\nyw #yank to register\n</code></pre>\n\n<p>Then, when you are typing in your pattern you can hit <code>&lt;control-r&gt;0&lt;enter&gt;</code> which will paste in your command the contents of the 0-th register.</p>\n\n<p>You can also make a command for this like:</p>\n\n<pre><code>:nmap &lt;leader&gt;w :s/\\(&lt;c-r&gt;=expand("&lt;cword&gt;")&lt;cr&gt;\\)/\n</code></pre>\n\n<p>Which will map hitting \'\\\' and \'w\' at the same time to replace your command line with</p>\n\n<pre><code>:s/\\(&lt;currentword&gt;\\)/\n</code></pre>\n'}, {'answer_id': 76316, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': '<p>You need to escape the backslashes within the mapping. You can also include the substitution string within the mapping.</p>\n\n<pre><code>:nmap &lt;leader&gt;w :s/\\\\(&lt;c-r&gt;=expand("&lt;cword&gt;")&lt;cr&gt;\\\\)/\\\\1\\\\1&lt;cr&gt;\n</code></pre>\n'}, {'answer_id': 80683, 'author': 'bmb', 'author_id': 5298, 'author_profile': 'https://Stackoverflow.com/users/5298', 'pm_score': 1, 'selected': False, 'text': '<pre><code>ywPx\n</code></pre>\n\n<p>will do what you describe.</p>\n\n<pre><code>ywPxw\n</code></pre>\n\n<p>will also advance the cursor to the next word.</p>\n'}, {'answer_id': 82120, 'author': 'user11211', 'author_id': 11211, 'author_profile': 'https://Stackoverflow.com/users/11211', 'pm_score': 5, 'selected': False, 'text': '<pre><code>yiwP\n</code></pre>\n\n<p><code>yiw</code>: Yank inner word (the word under the cursor). This command also moves the cursor to the beginning of the word.</p>\n\n<p><code>P</code>: Paste before the cursor.</p>\n\n<p>You can then map the e.g.: <code>&lt; ALT &gt; - D</code> to this command:</p>\n\n<pre><code>:nmap &lt; ALT &gt;-D yiwP\n</code></pre>\n'}, {'answer_id': 82158, 'author': 'rampion', 'author_id': 9859, 'author_profile': 'https://Stackoverflow.com/users/9859', 'pm_score': 4, 'selected': False, 'text': '<p>Another easy way to do this is to use the <code>*</code> command.</p>\n\n<p>In regular mode, when over a word, type </p>\n\n<pre><code>*:s//\\0\\0&lt;Enter&gt;\n</code></pre>\n\n<p><code>*</code> makes the search pattern the current word (e.g. \\&lt;abc\\>).  </p>\n\n<p><code>:s//</code> does a substitution using the current search pattern, and <code>\\0</code> in the replacement\nsection is the matched string.</p>\n\n<p>You can then repeat this behaviour, say over word "def", by either typing the same again, or by typing </p>\n\n<pre><code>*@:\n</code></pre>\n\n<p><code>@:</code> just repeats the last ex command, without a need for an &lt;Enter>, in this case the substitution.</p>\n\n<p>You can also record a quick macro to do this using the <code>q</code> command </p>\n\n<pre><code>qd*:s//\\0\\0&lt;Enter&gt;q\n</code></pre>\n\n<p>Then repeat it to your hearts content by typing</p>\n\n<pre><code>@d\n</code></pre>\n\n<p>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 <code>@:</code></p>\n'}, {'answer_id': 628865, 'author': 'César Alforde', 'author_id': 75950, 'author_profile': 'https://Stackoverflow.com/users/75950', 'pm_score': 7, 'selected': False, 'text': '<p>While in command-line mode, <kbd>CTRL</kbd>+<kbd>R</kbd> <kbd>CTRL</kbd>+<kbd>W</kbd> will insert the word under the cursor.</p>\n\n<p>See the help for <code>c_CTRL-R</code> for a listing of all the other special registers:</p>\n\n<pre><code>:help c_CTRL-R\n</code></pre>\n'}, {'answer_id': 869112, 'author': 'SergioAraujo', 'author_id': 2571881, 'author_profile': 'https://Stackoverflow.com/users/2571881', 'pm_score': -1, 'selected': False, 'text': '<pre><code>" count word  (case sensitive)\nnmap &lt;F4&gt; :%s/\\(&lt;c-r&gt;=expand("&lt;cword&gt;")&lt;cr&gt;\\)//gn&lt;cr&gt;\n</code></pre>\n'}, {'answer_id': 27369655, 'author': 'BBW Before Windows', 'author_id': 3186686, 'author_profile': 'https://Stackoverflow.com/users/3186686', 'pm_score': 0, 'selected': False, 'text': '<p>@<a href="https://stackoverflow.com/users/11211/user11211">user11211</a> <a href="https://stackoverflow.com/a/82120/1084">has the most straightforward way</a> to duplicate the word under cursor:</p>\n\n<pre><code>yiwP\n</code></pre>\n\n<p>yank inner word (moves cursor to start of word), paste (before cursor).</p>\n\n<p>eg. straigh[t]forward ----> straightforwar[d]straightforward</p>\n\n<p>[] is cursor</p>\n\n<p>To elaborate...</p>\n\n<p>You probably want to have the cursor following your duplicated word:</p>\n\n<pre><code>yiwPea\n</code></pre>\n\n<p>straigh[t]forward ----> straightforwardstraightforward[]</p>\n\n<p>NOTE:</p>\n\n<pre><code>yiw\n</code></pre>\n\n<p>is yank inner word (without whitespace)</p>\n\n<pre><code>yaw\n</code></pre>\n\n<p>is yank all word (including trailing whitespace).</p>\n\n<pre><code>yawPea\n</code></pre>\n\n<p>is therefore duplicate word including whitespace, and position cursor.</p>\n\n<p>straigh[t]forward ----> straightforward straightforward[]</p>\n'}]
['https://Stackoverflow.com/questions/48642', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1084/']