Yen's Blog

Lens, Wheels, Skates, Keyboard

Visual Studio Line Number Macros

In Visual Studio, it’s easy enough to turn on and off line numbers.  You’ll have to select Tools > Options > Text Editor, expand the language of choice, select General, then toggle Line numbers under the Display section.  This can get tedious if you want to be able to show and hide line numbers at will.  To turn this process into a single click, you can record custom macros to show and hide line numbers.  However, you may not realize that the macros to do just that are already included with Visual Studio. Further, these macros will turn on and off line numbers for a number of editor types, including C#, VB, HTML and plain text.

For easy access, you can put frequently used macros into a custom toolbar. This can be done as follows:

  • Create a new toolbar by right-clicking on menu bar and selecting Customize

  • Select New and enter a name

  • On the newly created toolbar, which is empty at the moment, open the drop-down menu and select Add or Remove Buttons > Customize
  • On the Customize dialog, select Add Command
  • On the Add Command dialog, select Macros in the Categories pane.  In the Commands pane, select Macros.Samples.Utilities.TurnOnLineNumbers

  • Do the same to add the TurnOffLineNumbers macro, or any other useful macro
  • Back on the Add Command dialog, select Modify Selection to change the macro’s display name to something more readable

  • The macros are now added to the new toolbar

If you’re interested in viewing or modifying the sample macros that come with Visual Studio, open the Macros editor by selecting Tools > Macros > Macros IDE.  If you’ve written VBA macros for Excel or Word, this should look familiar. The Samples project contains modules for a number of useful macros.  The line number macros above are defined in the Utilities module.