<p><strong><em>Edit:</em></strong> This question had been tagged "Tolstoy" in appreciation of the quality and length of my writing:) Just reading the first and the last paragraph should be enough:) If you tend to select and move code with the mouse, the stuff in middle  could be interesting to you.</p>

<p>This question is about how you use text editors in general. I’m looking for the best way to <em>delete</em> a plurality of lines of code (no intent to patent it:) This extends to <em>transposing</em> lines, i.e. deleting and adding them somewhere else. Most importantly, I don’t want to be creating any blank lines that I have to delete separately. Sort of like Visual Studio's SHIFT+DELETE feature, but working for multiple lines at once.</p>

<p>Say you want to delete line 3 from following code (tabs and newlines visualized as well). The naïve way would be to select the text between angle brackets:</p>

<pre>
if (true) {\n
\t int i = 1;\n
\t &lt;i *= 2;&gt;\n
\t i += 3;\n
}\n
</pre>

<p>Then hit backspace. This creates a blank line. Hit backspace twice more to delete \t and \n. </p>

<p>You end up with:</p>

<pre>
if (true) {\n
\t int i = 1;\n
\t i += 3;\n
}\n
</pre>

<p>When you try to select a whole line, Visual Studio doesn't let you select the trailing newline character. For example, placing the cursor on a line and hitting SHIFT+END will not select the newline at the end. Neither will you select the newline if you use your mouse, i.e. clicking in the middle of a line and dragging the cursor all the way to the right. You only select the trailing newline characters if you make a selection that spans at least two lines. Most editors I use do it this way; Microsoft WordPad and Word are counter-examples (and I frequently get newlines wrong when deleting text there; at least Word has a way to display end-of-line and end-of-paragraph characters explicitly).</p>

<p>When using Visual Studio and other editors in general, here’s the solution that currently works best for me:</p>

<p>Using the mouse, I select the characters that I put between angle brackets:</p>

<pre>
if (true) {\n
\t int i = 1;&lt;\n
\t i *= 2;&gt;\n
\t i += 3;\n
}\n
</pre>

<p>Hitting backspace now, you delete the line in one go without having to delete any other characters. This works for several contiguous lines at once. Additionally, it can be used for transposing lines. You could drag the selection between the angle brackets to the point marked with a caret:</p>

<pre>
if (true) {\n
\t int i = 1;&lt;\n
\t i *= 2;&gt;\n
\t i += 3;^\n
}\n
</pre>

<p>This leaves you with:</p>

<pre>
if (true) {\n
\t int i = 1;\n
\t i += 3;&lt;\n
\t i *= 2;&gt;\n
}\n
</pre>

<p>where lines 3 and 4 have switched place.</p>

<p>There are variations on this theme. When you want to delete line 3, you could also select the following characters:</p>

<pre>
if (true) {\n
\t int i = 1;\n
&lt;\t i *= 2;\n
&gt;\t i += 3;\n
}\n
</pre>

<p>In fact, this is what Visual Studio does if you tell it to select a complete line. You do this by clicking in the margin between your code and the column where the red circles go which indicate breakpoints. The mouse pointer is mirrored in that area to distinguish it a little better, but I think it's too narrow and physically too far removed from the code I want to select.</p>

<p>Maybe this method is useful to other people as well, even if it only serves to make them aware of how newlines are handled when selecting/deleting text:) It works nicely for most non-specialized text editors. However, given the vast amount of features and plugins for Visual Studio (which I use most), I'm sure there is better way to use it to delete and move lines of code. Getting the indentation right automatically when moving code between different blocks would be nice (i.e. without hitting "Format Document/Selection"). I'm looking forward to suggestions; no rants on micro-optimization, please:)</p>

<hr>

<p><strong><em>Summary of Answers</em></strong></p>

<p>With respect to Visual Studio: Navigating well with the cursor keys.</p>

<p>The solution that would best suit my style of going over and editing code is the <em>Eclipse</em> way:</p>

<p>You can select several consecutive lines of code, where the first and the last selected line may be selected only partially. Pressing ALT+{up,down} moves the complete lines (not just the selection) up and down, fixing indentation as you go. Hitting CTRL+D deletes the lines completely (not just the selection) without leaving any unwanted blank lines. I would love to see this in Visual Studio!</p>

[{'answer_id': 60697, 'author': 'jfs', 'author_id': 4279, 'author_profile': 'https://Stackoverflow.com/users/4279', 'pm_score': 2, 'selected': False, 'text': '<p>In Emacs:</p>\n\n<ul>\n<li>kill-line C-k </li>\n<li>transpose-lines C-x C-t</li>\n</ul>\n\n<p>C-a C-k C-k -- kill whole line including newline (or <code>kill-whole-line</code> by C-S-backspace).</p>\n\n<p>C-u &lt;number> C-k -- kill &lt;number> of lines (including newlines).</p>\n\n<p>C-y -- yank back the most recently killed text (aka paste)</p>\n'}, {'answer_id': 60704, 'author': 'Pedro', 'author_id': 5488, 'author_profile': 'https://Stackoverflow.com/users/5488', 'pm_score': 2, 'selected': False, 'text': "<p>In Eclipse you can <kbd>ALT</kbd>-<kbd>&darr;</kbd> or <kbd>ALT</kbd>-<kbd>&uarr;</kbd> to move a line. I find this incredibly useful, as well as <kbd>ALT</kbd>-<kbd>SHIFT</kbd>-{<kbd>&darr;</kbd>, <kbd>&uarr;</kbd>} to copy a line. In addition, it doesn't wreck your clipboard. It even corrects indentation as the line is moving!</p>\n"}, {'answer_id': 60709, 'author': 'Nathan Fellman', 'author_id': 1084, 'author_profile': 'https://Stackoverflow.com/users/1084', 'pm_score': 2, 'selected': False, 'text': '<p>In VIM:</p>\n<ul>\n<li>Delete the whole line including the newline: <code>dd</code></li>\n<li>Transpose lines: <code>dd p</code></li>\n</ul>\n<p>You can always prefix any command with a number to repeat it, so to delete 10 lines do:</p>\n<pre><code>10 dd\n</code></pre>\n<p>You can also specify a range of lines to delete.  For instance, to delete lines 10-15:</p>\n<pre><code>:10,15d\n</code></pre>\n<p>Or you can move the lines, for instance move lines 10-15 below line 20:</p>\n<pre><code>:10,15m20\n</code></pre>\n<p>Or you can <em>copy</em> the lines:</p>\n<pre><code>:10,15t20\n</code></pre>\n'}, {'answer_id': 60717, 'author': 'Sam Hasler', 'author_id': 2541, 'author_profile': 'https://Stackoverflow.com/users/2541', 'pm_score': 1, 'selected': False, 'text': "<p>Learn to use your cursor keys.</p>\n\n<p>For moving lines I do the following:</p>\n\n<ol>\n<li>Use <kbd>&uarr;</kbd>/<kbd>&darr;</kbd>to move to the line you want to copy.</li>\n<li>Hit <kbd>Home</kbd> if not there already, and again if it places the cursor after whitespace.</li>\n<li>Then press <strong><kbd>Shift</kbd>+<kbd>&darr;</kbd></strong> to select the line (or lines) you want to move</li>\n<li><kbd>Ctrl</kbd>+<kbd>X</kbd> to cut the line.</li>\n<li>Move Up/Down to the line you want to insert</li>\n<li><kbd>Ctrl</kbd>+<kbd>V</kbd></li>\n</ol>\n\n<p>This should work in pretty much any text editor on Windows.</p>\n\n<p>When deleting lines I still tend to use <kbd>Ctrl</kbd>+<kbd>X</kbd> (although I guess I also use backspace) as the above is so ingrained in how I edit, and it's also more forgiving.</p>\n\n<p>(Although I find them disorienting on the occasions I use Macs, I think Apple might have been on to something with the way they've set up the Home/End, skip word shortcuts on Macs)</p>\n"}, {'answer_id': 60718, 'author': 'Andres', 'author_id': 1815, 'author_profile': 'https://Stackoverflow.com/users/1815', 'pm_score': 2, 'selected': False, 'text': '<p>What I do is, starting with the cursor at the start of the line (in some editors you have to press home twice to do this), hold shift and press down until all lines that I want to delete are selected. Then I press delete.</p>\n'}, {'answer_id': 89783, 'author': 'jussij', 'author_id': 14738, 'author_profile': 'https://Stackoverflow.com/users/14738', 'pm_score': 0, 'selected': False, 'text': '<p>Using the <strong>Brief</strong> keyboard mapping this is done using the <strong><kbd>Alt</kbd>+<kbd>L</kbd></strong> to mark the line and the <strong><kbd>-</kbd></strong> key on the numeric keypad (or <strong><kbd>Alt</kbd>+<kbd>D</kbd></strong>) to cut the line to clipboard. The cut will remove the line entirely, including the newline character.</p>\n\n<p>Hitting the <strong><kbd>Ins</kbd></strong> key on the numeric keypad would put the line back into the document including the newline character. </p>\n\n<p>IMHO <strong>Brief</strong> is a really well designed keyboard mapping.</p>\n\n<p><strong>PS:</strong> I think MSVC has an option to emulate the Brief keyboard mapping.</p>\n'}, {'answer_id': 116861, 'author': 'Matt', 'author_id': 20630, 'author_profile': 'https://Stackoverflow.com/users/20630', 'pm_score': 0, 'selected': False, 'text': "<p>in Eclipse i use <kbd>CTRL</kbd>+ <kbd>D</kbd> to delete a single line (or a couple) \nfor many lines i'll select them with the mouse or with <kbd>SHIFT</kbd> + <kbd>ARROW</kbd> then press the <kbd>DEL</kbd> key.</p>\n"}, {'answer_id': 147676, 'author': 'Ted', 'author_id': 9344, 'author_profile': 'https://Stackoverflow.com/users/9344', 'pm_score': 0, 'selected': False, 'text': '<p>In addition to the above, use <a href="http://www.jetbrains.com/resharper/" rel="nofollow noreferrer">Resharper</a> for Visual Studio to do what you want. Best VS plugin you will find ever. It provides a bunch of different commands that help with moving/deleting/copying code here there and everywhere. Not to mention refactor/generate/etc code.</p>\n\n<p><kbd>Ctrl</kbd>-<kbd>Shift</kbd>-<kbd>Alt</kbd> <kbd>&uarr;</kbd>or <kbd>&darr;</kbd> will move a method up or down, line up or down, etc.\n<kbd>Shift</kbd>-<kbd>Del</kbd> - deletes the current line, but puts it in the clipboard (unless you modify your settings to not do this - I\'m trying to recall if this is a VS standard shortcut, not just Resharper - been too long).\n<kbd>Ctrl</kbd>-<kbd>C</kbd>, <kbd>Ctrl</kbd>-<kbd>X</kbd> without selecting copies/cuts the current line.\nAnd on and on...</p>\n\n<p>See <a href="http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap2.pdf" rel="nofollow noreferrer">http://www.jetbrains.com/resharper/docs/ReSharper40DefaultKeymap2.pdf</a> for a full list.</p>\n'}, {'answer_id': 147709, 'author': 'Dave Sherohman', 'author_id': 18914, 'author_profile': 'https://Stackoverflow.com/users/18914', 'pm_score': 1, 'selected': False, 'text': '<p>Adding to the existing vim answer, you can use d along with any cursor movement command to delete from the cursor\'s current position to the new position.  For example, to delete...</p>\n\n<ul>\n<li>...to end-of-paragraph (usually meaning "to the next blank line"): d}</li>\n<li>...the line containing the cursor and the next 5 lines: d5j</li>\n<li>...a set of parentheses, braces, etc. and its contents: d% (with the cursor on the opening or closing paren/brace/etc.)</li>\n<li>...to the third appearance of the word "foo": d3/foo</li>\n</ul>\n\n<p>It\'s quite flexible.</p>\n'}, {'answer_id': 5565739, 'author': 'Vasyl Boroviak', 'author_id': 188475, 'author_profile': 'https://Stackoverflow.com/users/188475', 'pm_score': 0, 'selected': False, 'text': '<p><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>L</kbd> removes line without copying it to the buffer.</p>\n'}, {'answer_id': 20648989, 'author': 'Drew', 'author_id': 729907, 'author_profile': 'https://Stackoverflow.com/users/729907', 'pm_score': 0, 'selected': False, 'text': '<p>In Emacs, in addition to <code>C-k</code> (and <code>C-k</code> with a numeric prefix arg, to kill N lines), you can just use the mouse:</p>\n\n<ul>\n<li><p><em><strong>To kill one line:</em></strong> triple-click it, then right-click twice</p></li>\n<li><p><em><strong>To kill multiple lines:</em></strong> triple-click the first line, then right-click on the last line ("first" line can be after the "last" line -- "first" here means first one you click)</p></li>\n</ul>\n'}]
['https://Stackoverflow.com/questions/60684', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/6103/']