Saturday, March 26, 2011

Visual Studio 2010 Tips and Tricks # 5 : Block Editing, Multi-line Coding

  • This is one of my favorite tricks in VS 2010 IDE. This one is more cool than my previous post [Visual Studio 2010 Tips and Tricks #4: Code Snippets ]
  • Did you know you can select any vertical area of your code by using “ALT” key and your “Mouse” in the Code area inside the editor.
  • Say we need to create Model class with lots of string variables and “Get”, “Set”. You can follow the tip#4 and use prop code snippet (or) you can use this Block Editing tip.

Editor_1

  • First select a vertical region of your code by pressing the “Alt” key and selecting a vertical line as shown above.

Editor_2

  • Start typing “public string” and see it fills in all the lines not just one line (as shown above).

Editor_3

  • Now press “Esc” to go out of this mode and start typing your Member names

Editor_4

  • Now again vertical select a region by pressing “Alt” key and use your mouse.

Editor_5

  • And enter “{ get; set; }” and your properties are done.

Editor_6

  • Yes but it looks ugly with lot of spaces in between, so use my previous tip [ Tip to autoformat code ] and select these lines and press “Ctrl + E + Ctrl F” to tidy up the code.

Editor_7

  • When there are lot of similar lines this approach is very helpful to code faster.
  • Say you want to convert all the properties from “public” to “internal”, this can be done very easily using our “Block” editing trick.
  • First use “ALT” key and select all the public word in all the lines.

Editor_8

  • Now start typing “internal” and you just renamed multiple lines. You may ask why not use “Quick Replace” but this is very helpful if you only want to apply the changes to only few lines and not the entire document.

Editor_9

  • This approach is very useful in situations say to enter comments for all lines, say to prefix “M_” or “_” to convert local variables to global etc..

No comments: