Visual Studio Tip: Locate Current File in Solution Explorer on Demand

Visual Studio doesn’t offer an easy way to locate the current file you’re editing in the Solution Explorer on demand. You can set the solution explorer to always stay in sync with this simple setting:

Tools > Options > Projects and Solutions > General. Check “Track active item in Solution Explorer”.

However, the constant jumping around in Solution Explorer is distracting and be a hindrance when you don’t want to lose your current location in the explorer. So getting this behavior on demand is the way to go.

Now third party add-ons like ReSharper offer a convenient context menu for locating the current file in the Solution Explorer on demand, but we can pull it off quickly in Visual Studio via a simple macro bound to a keyboard shortcut.

Step 1 – Open Macro Explorer

Press Alt+F8 to open Macro Explorer
Visual Studio Macro Explorer

Step 2 – Rename default module

Right click on Module1 and select “Rename”. Rename Module1 to “LocateCurrentFileInSolutionExplorer”.
Visual Studio Macro Explorer - Renamed Macro

Step 3 – Write Macro

Double click the “LocateCurrentFileInSolutionExplorer” you just created. This will open the editor. Paste the code below into the Public Module “LocateCurrentFileInSolutionExplorer” body:

Public Sub LocateFileInSolutionExplorer()
DTE.ExecuteCommand("View.TrackActivityinSolutionExplorer")
DTE.ExecuteCommand("View.TrackActivityinSolutionExplorer")
DTE.ExecuteCommand("View.SolutionExplorer")
End Sub

Visual Studio Macro Editor

Be sure to hit Ctrl+S to save.

Step 4 – Bind Keyboard Shortcut

Navigate to Tools > Options > Environment > Keyboard. Under “Show commands containing”, type “Locate”. This will select the “LocateCurrentFileInSolutionExplorer” macro you just created in blue. Bind to Shift+Alt+F (or other combo as desired) by pressing this key combination in the “Press shortcut keys” input box and clicking “Assign”.

Visual Studio Bind Macro Keyboard Shortcut

Step 5 – Enjoy!

Hit the keyboard shortcut and the file will be immediately highlighted in the Solution Explorer. You may now resume slinging code like a boss.

Visual Studio located file in Solution Explorer

7 replies on “Visual Studio Tip: Locate Current File in Solution Explorer on Demand”

  1. Thank you. This is a very nice article and a great help to me.

    One observation is that your Step 2 is not necessary. I preferred not to name the module based on the macro that it contains.

    Thanks again!

  2. Instead of spending time on writing code for this behaviour, you could use the built in functionality.

    Assign a shortcut to SolutionExplorer.SyncWithActiveDocument
    (in the general shortcut scheme it has a pre-assigned key combo – probably so in the other shortcut schemes too) and you have got what you need with no custom code at all.

  3. Thanks for giving help for “Track active item in Solution Explorer”

    Its works in VS 2013

Comments are closed.