A good programmer is a lazy programmer, because he writes minimum code.

Wednesday, December 16, 2009

Visual Studio Code Snippets - Get Away from Highlighting

Code Snippets are important productivity tool in Visual Studio.NET. Simply stated, code snippets are ready made code blocks, which you can use in your code. They can be accessed in Visual Studio either by right clicking in code editor and selecting Insert Snippet or by simply typing in the short cut for the snippet and then pressing Tab key (You will need to press Tab twice, if the intelisense menu is active). To demonstrate the second method, in a C# project, type in svm and press tab. You will get a declaration for the static void Main. Easy, no? Spend some time to explore the existing code snippets. It will definitely worth.

There is a problem with snippets, though. Most of the snippets come with replacable parameters. For example, if you try the for snippet in C#, there will be two replacable parameters. One for the index variable (default value is i) and the length variable. Even if you change these parameters for your requirements, the highlighting will be there until you close the file. Lot of people get irritated by this behaviour and some decides not to use the code snippets altogether.

Visual Studio 2008 comes with a solution to this problem. There are Show Snippet Highlighting and Hide Snippet Highlighting options. By default, only the last snippet inserted will be highlighted. If you want to highlight all snippets inserted since the file is opened, you can right click in code editor and select Show Snippet Highlighting. If you select Hide Snippet Highlighting, all highlightings will be hidden.

However, I can't find this feature in C#, even though MSDN says it is available. Moreover, show and hide snippet highlighting feature is not available in Visual Studio 2005. In those cases what we can do? Do we need to close and open a file each time we insert a snippet?

No, there is an easy and simple solution. Eventhough it is that much simple, I took some time to figure it out. Here is the solution.
  1. Cut the code lines, which include snippet highlighting.
  2. Paste it again.
This will simply remove the highlighting.

Hope it helps some one.

No comments:

Post a Comment