Monday, September 22, 2008

Tabs and formatting in VS 2005


If you coded for any length of time you've come across code that was written with an editor that defines different tab spacing than what you use. The result is is usually a mess of unevenly spaced code.

A simple fix to this is to replace tabs with spaces.

So the first step is to set up your editor to enter several spaces instead of a tab character. 4 spaces is recommended.

In Visual 2005 (this is probably true or similar for other versions)

Select Tools from the menu

Options...

Under "Text Editor" expand the section for the type of coding you're currently doing and open "Tabs". You'll want to change the setting to "Insert spaces" to "Keep tabs" and make sure the "Tab size:" value is set to 4 (or your preferred setting).

The second step is to replace your existing tabs with spaces. This is easier than it sounds - any good editor will allow you to select one tab although it will look like you're selecting a section of nothing. Copy the "tab", open search and replace (Ctrl-H on VS 2005) and paste into the "Find what:" section. Then enter 4 spaces into the "Replace with:" section and start replacing.

You may have some minor alignment errors you'll need to fix after the search and replace but this should help keep things tidier in the long run.

No comments: