Home > Uncategorized > Replacing multi-line text in Visual Studio solutions

Replacing multi-line text in Visual Studio solutions

I was editing a Visual C++ project of Flight Sim Labs recently and came across conditional defines of the following form at many of their C++ header files:

#if _MSC_VER > 1000
    #pragma once

That would be obviously faster to parse (taking in mind that the solution had a big number of C++ header files) and would result in less noise to anyone reading that source code to understand it and work with it, if it was converted to a simpler form:

#pragma once

Obviously checking for the Visual C++ compiler version after which #pragma once was introduced is only useful if you write ultra-portable source code (between C++ compilers). If you only target VC++ it doesn’t make sense to still also target very old versions of that compiler, unless you’re authoring some freeware C++ library that you want to also compile on some very older Visual Studio IDE version.

Having less clutter at the top of a source code file frees the programmer from having to scroll down a lot after opening the file to examine or work with it.

Having a big number of header files though meant that I had to find some way to replace all that automatically through out the solution. So I used the powerful "Replace in Files" action from Visual Studio’s Edit menu, using the intuitive "Wildcards" mode with the settings shown at the side-image. Note the \# and \n to escape the special # char and to define line change respectively.

Could also have used "Regular Expression" mode, but would need a different and a bit non-intuitive syntax for the search string (but this is more a matter of taste I guess).

To avoid having lots of open file tabs after the "Replace All" operation, I unchecked the option "Keep modified files open after Replace All". This results in the IDE doing the replace and saving the changed file without keeping the file open in a "dirty" state for you to save.

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: