visual studio - Deleting lines of code in a text editor -
edit: question had been tagged "tolstoy" in appreciation of quality , length of writing:) reading first , last paragraph should enough:) if tend select , move code mouse, stuff in middle interesting you.
this question how use text editors in general. i’m looking best way delete plurality of lines of code (no intent patent it:) extends transposing lines, i.e. deleting , adding them somewhere else. importantly, don’t want creating blank lines have delete separately. sort of visual studio's shift+delete feature, working multiple lines @ once.
say want delete line 3 following code (tabs , newlines visualized well). naïve way select text between angle brackets:
if (true) {\n \t int = 1;\n \t <i *= 2;>\n \t += 3;\n }\n
then hit backspace. creates blank line. hit backspace twice more delete \t , \n.
you end with:
if (true) {\n \t int = 1;\n \t += 3;\n }\n
when try select whole line, visual studio doesn't let select trailing newline character. example, placing cursor on line , hitting shift+end not select newline @ end. neither select newline if use mouse, i.e. clicking in middle of line , dragging cursor way right. select trailing newline characters if make selection spans @ least 2 lines. editors use way; microsoft wordpad , word counter-examples (and newlines wrong when deleting text there; @ least word has way display end-of-line , end-of-paragraph characters explicitly).
when using visual studio , other editors in general, here’s solution works best me:
using mouse, select characters put between angle brackets:
if (true) {\n \t int = 1;<\n \t *= 2;>\n \t += 3;\n }\n
hitting backspace now, delete line in 1 go without having delete other characters. works several contiguous lines @ once. additionally, can used transposing lines. drag selection between angle brackets point marked caret:
if (true) {\n \t int = 1;<\n \t *= 2;>\n \t += 3;^\n }\n
this leaves with:
if (true) {\n \t int = 1;\n \t += 3;<\n \t *= 2;>\n }\n
where lines 3 , 4 have switched place.
there variations on theme. when want delete line 3, select following characters:
if (true) {\n \t int = 1;\n <\t *= 2;\n >\t += 3;\n }\n
in fact, visual studio if tell select complete line. clicking in margin between code , column red circles go indicate breakpoints. mouse pointer mirrored in area distinguish little better, think it's narrow , physically far removed code want select.
maybe method useful other people well, if serves make them aware of how newlines handled when selecting/deleting text:) works nicely non-specialized text editors. however, given vast amount of features , plugins visual studio (which use most), i'm sure there better way use delete , move lines of code. getting indentation right automatically when moving code between different blocks nice (i.e. without hitting "format document/selection"). i'm looking forward suggestions; no rants on micro-optimization, please:)
summary of answers
with respect visual studio: navigating cursor keys.
the solution best suit style of going on , editing code eclipse way:
you can select several consecutive lines of code, first , last selected line may selected partially. pressing alt+{up,down} moves complete lines (not selection) , down, fixing indentation go. hitting ctrl+d deletes lines (not selection) without leaving unwanted blank lines. love see in visual studio!
in vim:
- delete whole line including newline:
dd
- transpose lines:
dd p
you can prefix command number repeat it, delete 10 lines do:
10 dd
you can specify range of lines delete. instance, delete lines 10-15:
:10,15d
or can move lines, instance move lines 10-15 below line 20:
:10,15m20
or can copy lines:
:10,15t20
Comments
Post a Comment