Posts tagged 'Code Snippets'

Wednesday, October 22, 2014

Tip 2.44: How to insert a code snippet around a block of code

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



C# and XML support the Edit.SurroundWith command that will insert the desired snippet around the selected code (whether it is just a selected word, selected line, or entire function). The keyboard shortcut is Ctrl+K, Ctrl+S.

For example, the following illustration shows how to use the Surround With feature to insert a for loop around an existing for loop.



The result is a for loop inside a for loop, as shown here:



Happy Programming! =)
Posted by Nils-Holger at 12:13 PM with 444 comments.

Tuesday, October 21, 2014

Tip 2.43: How to share code snippets with your team

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



She likes this tip because it isn't really about how to use features but rather how to combine features to do new things.


To share code snippets among others on your team:
  • Go to Tools–Code Snippets Manager, press the Add button, and type in the UNC share name.
  • Go to Tools–Import And Export Settings, Export, and choose to export just the Code Snippets Locations to a file.
  • Send out that .vssettings file to those on your team. They can go to Tools–Import And Export Settings and choose Import to retrieve it.




Happy Programming! =)
Posted by Nils-Holger at 12:47 PM with 445 comments.

Monday, October 20, 2014

Tip 2.42: How to change default values and variables in a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity, in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



When you insert a code snippet, the editor highlights the fields (variables, values, and so on) you can modify depending on how the code snippet was written. The idea is you modify the contents of the field and then press Tab to navigate to the next field. When you press Tab, that particular field is updated throughout the entire snippet. You can also navigate outside the field via the arrow keys to invoke the update.



If you press Enter, however, you will be committing both the current change (if any) and the entire snippet, meaning you can't use undo to get back to those highlighted fields. You would have to use your favorite refactoring method to make any additional updates.

Happy Programming! =)
Posted by Nils-Holger at 1:04 PM with 644 comments.

Sunday, October 19, 2014

Tip 2.41: How to browse code snippets and add new ones

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



All code snippets are found in the Code Snippets Manager. It is found at Tools–Code Snippets Manager. If you are using the General Development Settings, you can use Ctrl+K, Ctrl+B to bring up the dialog box.

In her opinion, the most useful aspect of this dialog box is to browse through your current snippets to learn what the shortcuts are to quickly insert the snippet into the editor.

Additionally, this is where you add (a directory of snippets) and import (a single snippet or multiple-selected snippets to a specified folder via the Import Code Snippet dialog box).



You should always check to see what type of snippets you are browsing by looking at the Language combo box found at the top of the dialog box. The Code Snippets Manager Language combo box is an MRU list, meaning that the last set of snippets you looked at (let's say XML) will come up the next time you bring up the dialog box.

Sara Aside

For her as a tester, it was critical that she always confirmed where she was before using the dialog box; otherwise, she would end up logging incorrect bugs. Maybe you don't need this warning, but old habits die hard.


Happy Programming! =)
Posted by Nils-Holger at 12:48 PM with 435 comments.

Saturday, October 18, 2014

Tip 2.40: You can insert a snippet by pressing Tab Tab

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



Sara Aside

A few months ago, she had dinner with some Microsoft MVPs and other Visual Studio users who were on campus for the Microsoft certification exams. One of the developers said, "Hey Sara, you need to blog about Snippet Tab Tab." She was blown away. She had tested this feature inside and out and never had heard of "Tab Tab." It turns out that he was just referring to the sequence of keystrokes you use to insert a snippet, but she never even thought to refer to it as the "Tab Tab" feature.


She's not a C# developer. She spent the majority of her time writing code in Visual Basic during her software testing days. Whenever she needs to use C#, she heavily relies upon code snippets because she doesn't recall the syntax off the top of her head.

When in the appropriate place in the editor, you can type in the keyboard shortcut of a snippet, like for.



In this state, you can simply press Tab twice to insert the snippet.

Why twice? Press it once to autocomplete statement completion. If you just type f for "for", and for is highlighted, you can just press Tab once to complete the word for. Press it twice to generate the snippet, since the cursor will be at the end of the word for in the editor.



And the moral of the story is " Tab Tab."

Happy Programming! =)
Posted by Nils-Holger at 12:46 PM with 447 comments.

Friday, October 17, 2014

Tip 2.39: You can insert a code snippet via its shortcut keyword

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



Code snippets have the support to be given a "shortcut," usually an abbreviated version of the code snippet name that you can type into the editor and hit Tab to insert.

To insert, simply type in the name of the snippet—for example, for—and then hit Tab. Note that if statement completion is open, you'll have to hit Tab twice to insert the snippet.

In Visual Studio 2005, 2008 and 2012, you'll be able to see C# Code Snippet shortcuts in the statement completion window. The following screen shot is the for snippet displayed within the statement completion window. Note the snippet icon to the left.



In both Visual Studio 2008 and 2012, you won't see Visual Basic snippets in the statement completion window, but you will see a note in the ToolTip when you can hit Tab twice to insert the corresponding snippet.



Happy Programming! =)
Posted by Nils-Holger at 12:33 PM with 444 comments.

Thursday, October 16, 2014

Tip 2.38: You can use Ctrl+K, Ctrl+X to insert a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



The keyboard shortcut to insert a code snippet is Ctrl+K, Ctrl+X. It is bound to the command Edit.InsertSnippet, in case your mileage varies, depending on your configuration settings.

She's going to use C# for this tip, but this tip applies to all languages that support code snippets. When she invokes the Edit.InsertSnippet command, the code snippet insertion UI pops up. The real tip here is that the snippet picker allows for type-ahead selection. Note how she started typing "#re" on the line. (Oh, the fun she had testing this . . . but she digresses.)



Additional keystrokes:
  • You can hit Tab to autocomplete the word. If the word happens to be the code snippet (and not a folder), pressing Tab will insert it.
  • You can also hit Shift+Tab to navigate back to the previous word (her contribution to the insertion UI).


Happy Programming! =)
Posted by Nils-Holger at 12:17 PM with 440 comments.

Tuesday, February 11, 2014

Tip 2.44: How to insert a code snippet around a block of code

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog

C# and XML support the Edit.SurroundWith command that will insert the desired snippet around the selected code (whether it is just a selected word, selected line, or entire function). The keyboard shortcut is Ctrl+K, Ctrl+S.

For example, the following illustration shows how to use the Surround With feature to insert a for loop around an existing for loop.



The result is a for loop inside a for loop, as shown here:



Happy Programming! =)
Posted by Nils-Holger at 1:44 PM with 0 comments.

Monday, February 10, 2014

Tip 2.43: How to share code snippets with your team

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



She likes this tip because it isn't really about how to use features but rather how to combine features to do new things.
To share code snippets among others on your team:
  • Go to Tools–Code Snippets Manager, press the Add button, and type in the UNC share name.
  • Go to Tools–Import And Export Settings, Export, and choose to export just the Code Snippets Locations to a file.
  • Send out that .vssettings file to those on your team. They can go to Tools–Import And Export Settings and choose Import to retrieve it.




Happy Programming! =)
Posted by Nils-Holger at 11:48 AM with 443 comments.

Sunday, February 9, 2014

Tip 2.42: How to change default values and variables in a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity, in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



When you insert a code snippet, the editor highlights the fields (variables, values, and so on) you can modify depending on how the code snippet was written. The idea is you modify the contents of the field and then press Tab to navigate to the next field. When you press Tab, that particular field is updated throughout the entire snippet. You can also navigate outside the field via the arrow keys to invoke the update.



If you press Enter, however, you will be committing both the current change (if any) and the entire snippet, meaning you can't use undo to get back to those highlighted fields. You would have to use your favorite refactoring method to make any additional updates.

Happy Programming! =)
Posted by Nils-Holger at 12:53 PM with 438 comments.

Saturday, February 8, 2014

Tip 2.41: How to browse code snippets and add new ones

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



All code snippets are found in the Code Snippets Manager. It is found at Tools–Code Snippets Manager. If you are using the General Development Settings, you can use Ctrl+K, Ctrl+B to bring up the dialog box. In her opinion, the most useful aspect of this dialog box is to browse through your current snippets to learn what the shortcuts are to quickly insert the snippet into the editor. Additionally, this is where you add (a directory of snippets) and import (a single snippet or multiple-selected snippets to a specified folder via the Import Code Snippet dialog box).



You should always check to see what type of snippets you are browsing by looking at the Language combo box found at the top of the dialog box. The Code Snippets Manager Language combo box is an MRU list, meaning that the last set of snippets you looked at (let's say XML) will come up the next time you bring up the dialog box.

Sara Aside

For her as a tester, it was critical that she always confirmed where she was before using the dialog box; otherwise, she would end up logging incorrect bugs. Maybe you don't need this warning, but old habits die hard.


Happy Programming! =)
Posted by Nils-Holger at 1:17 PM with 476 comments.

Friday, February 7, 2014

Tip 2.40: You can insert a snippet by pressing Tab Tab

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog





Sara Aside

A few months ago, she had dinner with some Microsoft MVPs and other Visual Studio users who were on campus for the Microsoft certification exams. One of the developers said, "Hey Sara, you need to blog about Snippet Tab Tab." She was blown away. She had tested this feature inside and out and never had heard of "Tab Tab." It turns out that he was just referring to the sequence of keystrokes you use to insert a snippet, but she never even thought to refer to it as the "Tab Tab" feature.


She's not a C# developer. She spent the majority of her time writing code in Visual Basic during her software testing days. Whenever she needs to use C#, she heavily relies upon code snippets because she doesn't recall the syntax off the top of her head.

When in the appropriate place in the editor, you can type in the keyboard shortcut of a snippet, like for.



In this state, you can simply press Tab twice to insert the snippet. Why twice? Press it once to autocomplete statement completion. If you just type f for "for", and for is highlighted, you can just press Tab once to complete the word for. Press it twice to generate the snippet, since the cursor will be at the end of the word for in the editor.



And the moral of the story is " Tab Tab."

Happy Programming! =)
Posted by Nils-Holger at 12:38 PM with 441 comments.

Thursday, February 6, 2014

Tip 2.39: You can insert a code snippet via its shortcut keyword

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



Code snippets have the support to be given a "shortcut," usually an abbreviated version of the code snippet name that you can type into the editor and hit Tab to insert. To insert, simply type in the name of the snippet—for example, for—and then hit Tab. Note that if statement completion is open, you'll have to hit Tab twice to insert the snippet. In Visual Studio 2005, 2008 and 2012, you'll be able to see C# Code Snippet shortcuts in the statement completion window. The following screen shot is the for snippet displayed within the statement completion window. Note the snippet icon to the left.



In both Visual Studio 2008 and 2012, you won't see Visual Basic snippets in the statement completion window, but you will see a note in the ToolTip when you can hit Tab twice to insert the corresponding snippet.



Happy Programming! =)
Posted by Nils-Holger at 12:50 PM with 440 comments.

Wednesday, February 5, 2014

Tip 2.38: You can use Ctrl+K, Ctrl+X to insert a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



The keyboard shortcut to insert a code snippet is Ctrl+K, Ctrl+X. It is bound to the command Edit.InsertSnippet, in case your mileage varies, depending on your configuration settings.

She's going to use C# for this tip, but this tip applies to all languages that support code snippets. When she invokes the Edit.InsertSnippet command, the code snippet insertion UI pops up. The real tip here is that the snippet picker allows for type-ahead selection. Note how she started typing "#re" on the line. (Oh, the fun she had testing this . . . but she digresses.)



Additional keystrokes:
  • You can hit Tab to autocomplete the word. If the word happens to be the code snippet (and not a folder), pressing Tab will insert it.
  • You can also hit Shift+Tab to navigate back to the previous word (her contribution to the insertion UI).


Happy Programming! =)
Posted by Nils-Holger at 1:18 PM with 445 comments.

Tuesday, June 4, 2013

Tip 2.44: How to insert a code snippet around a block of code

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



C# and XML support the Edit.SurroundWith command that will insert the desired snippet around the selected code (whether it is just a selected word, selected line, or entire function). The keyboard shortcut is Ctrl+K, Ctrl+S. For example, the following illustration shows how to use the Surround With feature to insert a for loop around an existing for loop.



The result is a for loop inside a for loop, as shown here:



Happy Programming! =)
Posted by Nils-Holger at 1:16 PM with 494 comments.

Monday, June 3, 2013

Tip 2.43: How to share code snippets with your team

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



She likes this tip because it isn't really about how to use features but rather how to combine features to do new things.


To share code snippets among others on your team:
  • Go to Tools–Code Snippets Manager, press the Add button, and type in the UNC share name.
  • Go to Tools–Import And Export Settings, Export, and choose to export just the Code Snippets Locations to a file.
  • Send out that .vssettings file to those on your team. They can go to Tools–Import And Export Settings and choose Import to retrieve it.




Happy Programming! =)
Posted by Nils-Holger at 12:51 PM with 437 comments.

Sunday, June 2, 2013

Tip 2.42: How to change default values and variables in a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity, in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



When you insert a code snippet, the editor highlights the fields (variables, values, and so on) you can modify depending on how the code snippet was written. The idea is you modify the contents of the field and then press Tab to navigate to the next field. When you press Tab, that particular field is updated throughout the entire snippet. You can also navigate outside the field via the arrow keys to invoke the update.



If you press Enter, however, you will be committing both the current change (if any) and the entire snippet, meaning you can't use undo to get back to those highlighted fields. You would have to use your favorite refactoring method to make any additional updates.

Happy Programming! =)
Posted by Nils-Holger at 12:04 PM with 1314 comments.

Saturday, June 1, 2013

Tip 2.41: How to browse code snippets and add new ones

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



All code snippets are found in the Code Snippets Manager. It is found at Tools–Code Snippets Manager. If you are using the General Development Settings, you can use Ctrl+K, Ctrl+B to bring up the dialog box. In her opinion, the most useful aspect of this dialog box is to browse through your current snippets to learn what the shortcuts are to quickly insert the snippet into the editor. Additionally, this is where you add (a directory of snippets) and import (a single snippet or multiple-selected snippets to a specified folder via the Import Code Snippet dialog box).



You should always check to see what type of snippets you are browsing by looking at the Language combo box found at the top of the dialog box. The Code Snippets Manager Language combo box is an MRU list, meaning that the last set of snippets you looked at (let's say XML) will come up the next time you bring up the dialog box.

Sara Aside

For her as a tester, it was critical that she always confirmed where she was before using the dialog box; otherwise, she would end up logging incorrect bugs. Maybe you don't need this warning, but old habits die hard.


Happy Programming! =)
Posted by Nils-Holger at 12:21 PM with 441 comments.

Friday, May 31, 2013

Tip 2.40: You can insert a snippet by pressing Tab Tab

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



Sara Aside

A few months ago, she had dinner with some Microsoft MVPs and other Visual Studio users who were on campus for the Microsoft certification exams. One of the developers said, "Hey Sara, you need to blog about Snippet Tab Tab." She was blown away. She had tested this feature inside and out and never had heard of "Tab Tab." It turns out that he was just referring to the sequence of keystrokes you use to insert a snippet, but she never even thought to refer to it as the "Tab Tab" feature.


She's not a C# developer. She spent the majority of her time writing code in Visual Basic during her software testing days. Whenever she needs to use C#, she heavily relies upon code snippets because she doesn't recall the syntax off the top of her head. When in the appropriate place in the editor, you can type in the keyboard shortcut of a snippet, like for.



In this state, you can simply press Tab twice to insert the snippet. Why twice? Press it once to autocomplete statement completion. If you just type f for "for", and for is highlighted, you can just press Tab once to complete the word for. Press it twice to generate the snippet, since the cursor will be at the end of the word for in the editor.



And the moral of the story is " Tab Tab."

Happy Programming! =)
Posted by Nils-Holger at 12:24 PM with 445 comments.

Thursday, May 30, 2013

Tip 2.39: You can insert a code snippet via its shortcut keyword

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



Code snippets have the support to be given a "shortcut," usually an abbreviated version of the code snippet name that you can type into the editor and hit Tab to insert. To insert, simply type in the name of the snippet—for example, for—and then hit Tab. Note that if statement completion is open, you'll have to hit Tab twice to insert the snippet. In Visual Studio 2005, 2008 and 2012, you'll be able to see C# Code Snippet shortcuts in the statement completion window. The following screen shot is the for snippet displayed within the statement completion window. Note the snippet icon to the left.



In both Visual Studio 2008 and 2012, you won't see Visual Basic snippets in the statement completion window, but you will see a note in the ToolTip when you can hit Tab twice to insert the corresponding snippet.



Happy Programming! =)
Posted by Nils-Holger at 12:17 PM with 444 comments.

Wednesday, May 29, 2013

Tip 2.38: You can use Ctrl+K, Ctrl+X to insert a code snippet

"Visual Studio Tips, 251 ways to improve your Productivity in Visual Studio", courtesy of 'Sara Ford'

Sara Ford's Blog



The keyboard shortcut to insert a code snippet is Ctrl+K, Ctrl+X. It is bound to the command Edit.InsertSnippet, in case your mileage varies, depending on your configuration settings. She's going to use C# for this tip, but this tip applies to all languages that support code snippets. When she invokes the Edit.InsertSnippet command, the code snippet insertion UI pops up. The real tip here is that the snippet picker allows for type-ahead selection. Note how she started typing "#re" on the line. (Oh, the fun she had testing this . . . but she digresses.)



Additional keystrokes:
  • You can hit Tab to autocomplete the word. If the word happens to be the code snippet (and not a folder), pressing Tab will insert it.
  • You can also hit Shift+Tab to navigate back to the previous word (her contribution to the insertion UI).


Happy Programming! =)
Posted by Nils-Holger at 12:11 PM with 479 comments.