<p>How do I duplicate a whole line in <strong>Vim</strong> in a similar way to <kbd>Ctrl</kbd>+<kbd>D</kbd> in IntelliJ IDEA/ Resharper or <kbd>Ctrl</kbd>+<kbd>Alt</kbd>+<kbd>&#8593;</kbd>/<kbd>&#8595;</kbd> in <strong>Eclipse</strong>?</p>

[{'answer_id': 73329, 'author': 'Eric Z Beard', 'author_id': 1219, 'author_profile': 'https://Stackoverflow.com/users/1219', 'pm_score': 6, 'selected': False, 'text': '<p>Do this:    </p>\n\n<p>First, <kbd>y</kbd><kbd>y</kbd> to copy the current line, and then <kbd>p</kbd> to paste.</p>\n'}, {'answer_id': 73330, 'author': 'pjz', 'author_id': 8002, 'author_profile': 'https://Stackoverflow.com/users/8002', 'pm_score': 6, 'selected': False, 'text': '<p><kbd>y</kbd><kbd>y</kbd></p>\n\n<p>will yank the current line without deleting it</p>\n\n<p><kbd>d</kbd><kbd>d</kbd></p>\n\n<p>will delete the current line</p>\n\n<p><kbd>p</kbd></p>\n\n<p>will <code>put</code> a line grabbed by either of the previous methods</p>\n'}, {'answer_id': 73356, 'author': 'cori', 'author_id': 8151, 'author_profile': 'https://Stackoverflow.com/users/8151', 'pm_score': 2, 'selected': False, 'text': '<p>1 gotcha: when you use "p" to put the line, it puts it <em>after</em> the line your cursor is on, so if you want to add the line after the line you\'re yanking, don\'t move the cursor down a line before putting the new line.</p>\n'}, {'answer_id': 73357, 'author': 'Mark Biek', 'author_id': 305, 'author_profile': 'https://Stackoverflow.com/users/305', 'pm_score': 13, 'selected': True, 'text': '<p><kbd>y</kbd><kbd>y</kbd> or <kbd>Y</kbd> to copy the line (mnemonic: <strong><em>y</strong>ank</em>)\n<br />or\n<br /><kbd>d</kbd><kbd>d</kbd> to <strong><em>d</strong>elete</em> the line (Vim copies what you deleted into a clipboard-like "register", like a <em>cut</em> operation)</p>\n\n<p>then</p>\n\n<p><kbd>p</kbd> to <strong><em>p</strong>aste</em> the copied or deleted text <em>after</em> the current line\n<br />or\n<br /><kbd>P</kbd> to <strong><em>p</strong>aste</em> the copied or deleted text <em>before</em> the current line</p>\n'}, {'answer_id': 73362, 'author': 'Linulin', 'author_id': 12481, 'author_profile': 'https://Stackoverflow.com/users/12481', 'pm_score': 8, 'selected': False, 'text': '<p><code>YP</code> or <code>Yp</code> or <code>yyp</code>.</p>\n\n<p></p>\n'}, {'answer_id': 73440, 'author': 'theschmitzer', 'author_id': 2167252, 'author_profile': 'https://Stackoverflow.com/users/2167252', 'pm_score': 6, 'selected': False, 'text': '<p><kbd>y</kbd><kbd>y</kbd><kbd>p</kbd> - remember it with "yippee!"</p>\n\n<p>Multiple lines with a number in between:</p>\n\n<p><kbd>y</kbd><kbd>7</kbd><kbd>y</kbd><kbd>p</kbd></p>\n'}, {'answer_id': 73682, 'author': 'Kwondri', 'author_id': 7691, 'author_profile': 'https://Stackoverflow.com/users/7691', 'pm_score': 6, 'selected': False, 'text': '<p>If you want another way:</p>\n\n<p><code>"ayy</code>:\nThis will store the line in buffer <code>a</code>.</p>\n\n<p><code>"ap</code>:\nThis will put the contents of buffer <code>a</code> at the cursor.</p>\n\n<p>There are many variations on this.</p>\n\n<p><code>"a5yy</code>:\nThis will store the 5 lines in buffer <code>a</code>.</p>\n\n<p>See "<a href="https://vimhelp.org/" rel="noreferrer">Vim help files</a> for more fun.</p>\n'}, {'answer_id': 96605, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>You can also try &lt;C-x&gt;&lt;C-l&gt; which will repeat the last line from insert mode and brings you a completion window with all of the lines. It works almost like &lt;C-p&gt;</p>\n'}, {'answer_id': 1123441, 'author': 'Rook', 'author_id': 62699, 'author_profile': 'https://Stackoverflow.com/users/62699', 'pm_score': 4, 'selected': False, 'text': '<p>Another option would be to go with:</p>\n\n<pre><code>nmap &lt;C-d&gt; mzyyp`z\n</code></pre>\n\n<p>gives you the advantage of preserving the cursor position.</p>\n'}, {'answer_id': 1123462, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 4, 'selected': False, 'text': '<p>I like:\n<kbd>Shift</kbd>+<kbd>v</kbd> (to select the whole line immediately and let you select other lines if you want), y, p</p>\n'}, {'answer_id': 10194014, 'author': 'Benoit', 'author_id': 457352, 'author_profile': 'https://Stackoverflow.com/users/457352', 'pm_score': 9, 'selected': False, 'text': '<p>Normal mode: see other answers.</p>\n\n<p>The <a href="https://en.wikipedia.org/wiki/Ex_%28text_editor%29" rel="noreferrer" title="text editor">Ex</a> way:</p>\n\n<ul>\n<li><code>:t.</code> will duplicate the line,</li>\n<li><code>:t 7</code> will copy it after line 7,</li>\n<li><code>:,+t0</code> will copy current and next line at the beginning of the file (<code>,+</code> is a synonym for the range <code>.,.+1</code>),</li>\n<li><code>:1,t$</code> will copy lines from beginning till cursor position to the end (<code>1,</code> is a synonym for the range <code>1,.</code>).</li>\n</ul>\n\n<p>If you need to move instead of copying, use <code>:m</code> instead of <code>:t</code>.</p>\n\n<p>This can be really powerful if you combine it with <code>:g</code> or <code>:v</code>: </p>\n\n<ul>\n<li><code>:v/foo/m$</code> will move all lines not matching the pattern “foo” to the end of the file.</li>\n<li><code>:+,$g/^\\s*class\\s\\+\\i\\+/t.</code> will copy all subsequent lines of the form <code>class xxx</code> right after the cursor.</li>\n</ul>\n\n<p>Reference: <code>:help range</code>, <code>:help :t</code>, <code>:help :g</code>, <code>:help :m</code> and <code>:help :v</code></p>\n'}, {'answer_id': 19337648, 'author': 'yemu', 'author_id': 2006977, 'author_profile': 'https://Stackoverflow.com/users/2006977', 'pm_score': 5, 'selected': False, 'text': '<p><kbd>y</kbd><kbd>y</kbd><kbd>p</kbd> - paste after</p>\n\n<p><kbd>y</kbd><kbd>y</kbd><kbd>P</kbd> - paste before</p>\n'}, {'answer_id': 21638849, 'author': 'Adam', 'author_id': 313211, 'author_profile': 'https://Stackoverflow.com/users/313211', 'pm_score': 8, 'selected': False, 'text': '<p><img src="https://i.stack.imgur.com/dz2pb.gif" alt="copy and paste in vim"></p>\n\n<p>Doesn\'t get any simpler than this! From normal mode:</p>\n\n<pre><code>yy\n</code></pre>\n\n<p>then move to the line you want to paste at and</p>\n\n<pre><code>p\n</code></pre>\n'}, {'answer_id': 29739390, 'author': 'Chris Penner', 'author_id': 3907685, 'author_profile': 'https://Stackoverflow.com/users/3907685', 'pm_score': 3, 'selected': False, 'text': '<p>Default is <kbd>y</kbd><kbd>y</kbd><kbd>p</kbd>, but I\'ve been using this rebinding for a year or so and love it:</p>\n\n<p><code>" set Y to duplicate lines, works in visual mode as well.\nnnoremap Y yyp\nvnoremap Y y`&gt;pgv\n</code></p>\n'}, {'answer_id': 35316491, 'author': 'ap-osd', 'author_id': 3209308, 'author_profile': 'https://Stackoverflow.com/users/3209308', 'pm_score': 2, 'selected': False, 'text': '<p>For those starting to learn vi, here is a good introduction to vi by listing side by side vi commands to typical Windows GUI Editor cursor movement and shortcut keys. It lists all the basic commands including <kbd>yy</kbd> (copy line) and <kbd>p</kbd> (paste after) or <kbd>P</kbd>(paste before).</p>\n\n<p><a href="https://cognitivewaves.wordpress.com/vi-editor/" rel="nofollow">vi (Vim) for Windows Users</a></p>\n'}, {'answer_id': 37190575, 'author': 'Michael', 'author_id': 916379, 'author_profile': 'https://Stackoverflow.com/users/916379', 'pm_score': 3, 'selected': False, 'text': '<p>For someone who doesn\'t know vi, some answers from above might mislead him with phrases like "paste ... after/before <b><i>current line</b></i>".\n<br />It\'s actually "paste ... after/before <b><i>cursor</b></i>".\n<br /><br />\n<kbd>y</kbd><kbd>y</kbd> or <kbd>Y</kbd> to copy the line\n<br />or\n<br /><kbd>d</kbd><kbd>d</kbd> to delete the line</p>\n\n<p>then</p>\n\n<p><kbd>p</kbd> to paste the copied or deleted text <em>after</em> the <b>cursor</b>\n<br />or\n<br /><kbd>P</kbd> to paste the copied or deleted text <em>before</em> the <b>cursor</b></p>\n\n<p><br />\nFor more key bindings, you can visit this site: <a href="http://hea-www.harvard.edu/~fine/Tech/vi.html" rel="noreferrer">vi Complete Key Binding List</a></p>\n'}, {'answer_id': 48734229, 'author': 'jedi', 'author_id': 4741620, 'author_profile': 'https://Stackoverflow.com/users/4741620', 'pm_score': 2, 'selected': False, 'text': '<p>If you would like to duplicate a line and paste it right away below the current like, just like in Sublime <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>D</kbd>, then you can add this to your <code>.vimrc</code> file.</p>\n\n<p><code>nmap &lt;S-C-d&gt; &lt;Esc&gt;Yp</code></p>\n\n<p>Or, for Insert mode:</p>\n\n<p><code>imap &lt;S-C-d&gt; &lt;Esc&gt;Ypa</code></p>\n'}, {'answer_id': 49780313, 'author': 'yolenoyer', 'author_id': 3271687, 'author_profile': 'https://Stackoverflow.com/users/3271687', 'pm_score': 1, 'selected': False, 'text': '<p>I like to use this mapping:</p>\n\n<pre><code>:nnoremap yp Yp\n</code></pre>\n\n<p>because it makes it consistent to use alongside the native <code>YP</code> command.</p>\n'}, {'answer_id': 49811528, 'author': 'DarkWiiPlayer', 'author_id': 4984564, 'author_profile': 'https://Stackoverflow.com/users/4984564', 'pm_score': 3, 'selected': False, 'text': "<p>I know I'm late to the party, but whatever; I have this in my .vimrc:</p>\n\n<pre><code>nnoremap &lt;C-d&gt; :copy .&lt;CR&gt;\nvnoremap &lt;C-d&gt; :copy '&gt;&lt;CR&gt;\n</code></pre>\n\n<p>the <code>:copy</code> command just copies the selected line or the range (always whole lines) to below the line number given as its argument.</p>\n\n<p>In normal mode what this does is <code>copy .</code> <em>copy this line to just below this line</em>.</p>\n\n<p>And in visual mode it turns into <code>'&lt;,'&gt; copy '&gt;</code> <em>copy from start of selection to end of selection to the line below end of selection</em>.</p>\n"}, {'answer_id': 67353204, 'author': 'frfernandezdev', 'author_id': 9309561, 'author_profile': 'https://Stackoverflow.com/users/9309561', 'pm_score': 0, 'selected': False, 'text': '<p>I use this mapping, which is similar to vscode. I hope it is useful!!!.</p>\n<pre><code>nnoremap &lt;A-d&gt; :t. &lt;CR&gt;==\ninoremap &lt;A-d&gt; &lt;Esc&gt;:t. &lt;CR&gt;==gi\nvnoremap &lt;A-d&gt; :t$ &lt;CR&gt;gv=gv\n</code></pre>\n'}, {'answer_id': 68621271, 'author': 'Savrige', 'author_id': 2583490, 'author_profile': 'https://Stackoverflow.com/users/2583490', 'pm_score': 2, 'selected': False, 'text': '<p>I prefer to define a custom keymap <kbd>Ctrl</kbd>+<kbd>D</kbd> in <code>.vimrc</code> to duplicate the current line both in <em>normal</em> mode and <em>insert</em> mode:</p>\n<pre><code>&quot; duplicate line in normal mode:\nnnoremap &lt;C-D&gt; Yp\n&quot; duplicate line in insert mode:\ninoremap &lt;C-D&gt; &lt;Esc&gt; Ypi\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/73319', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11439/']