129

This is a feature I have grown accustomed to in Eclipse (Ctrl+Tab). Is there an equivalent in Visual C++?

2
  • See also this question
    – idbrii
    Sep 24, 2011 at 0:43
  • @LưuVĩnhPhúc When you're in a header or source code in Eclipse, press Alt-Tab and Eclipse should switch you between the source and header, assuming you've got the project indexed properly.
    – MrMas
    Apr 15, 2016 at 15:20

16 Answers 16

161

In Visual Studio 2013 and later there is a default keyboard shortcut for this: Ctrl+K, Ctrl+O (You will need to hold down Ctrl and type ko and then release Ctrl)

In earlier versions, see:

Visual Studio Macro to switch between CPP and H files

or

Open Corresponding File in Visual Assist

12
  • 2
    Visual Assist is a great tool - I'd recommend it for all developers using MSVC Apr 9, 2010 at 0:56
  • 12
    For lazy VisualAssist users: Alt + O
    – nimcap
    Feb 4, 2012 at 20:45
  • 2
    does anyone have a solution for VS2013? Nov 19, 2013 at 3:00
  • 5
    It's built in to VS 2013. blogs.msdn.com/b/vcblog/archive/2013/08/23/…
    – Kyle Alons
    Nov 19, 2013 at 14:39
  • 3
    Ok, my bad. Didn't realize Ctrl+K & Ctrl+O is actually a SINGLE shortcut command; not two options of the same. Might be worth mentioning in the answer for people who are new to VS :) May 31, 2015 at 10:46
33

In Visual Studio 2013 a default keyboard shortcut for this is Ctrl+K, Ctrl+O

2
  • doesn't work for me.2013 Community Edition. General Settings. What is commad name in Customize/Keyboard window?
    – Jurlie
    Apr 21, 2015 at 8:30
  • 17
    Command name: EditorContextMenus.CodeWindow.ToggleHeaderCodeFile
    – codekaizen
    Apr 28, 2015 at 23:44
24

You could add this macro to your VS config (via Tools -> Macros -> Macro Explorer) then assign a hotkey to it (via Tools -> Options -> Environment -> Keyboard).

I only just wrote it (been meaning to try this for ages!) but it seems to work so far, in both VS2008 and VS2010.

Since it's a macro you can edit it to include whatever rules you want (e.g. looking in other folders, or special naming rules if you have a single header shared by multiple cpp files or similar).

Here's the macro (I'm sure it could be better written; I'm unfamiliar with the VS objects and only realised macros were using .Net about half-way through writing the thing :)):

Sub FileSwitch()
    Try
        Dim CurrentPath As String = DTE.ActiveDocument.FullName
        Dim OtherPath As String

        If (IO.Path.HasExtension(CurrentPath)) Then
            Dim CurrentExtension As String = IO.Path.GetExtension(CurrentPath)

            Select Case CurrentExtension
                Case ".h", ".hpp", ".hxx"
                    OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cpp")
                    If (Not IO.File.Exists(OtherPath)) Then
                        OtherPath = IO.Path.ChangeExtension(CurrentPath, ".c")
                        If (Not IO.File.Exists(OtherPath)) Then
                            OtherPath = IO.Path.ChangeExtension(CurrentPath, ".cxx")
                        End If
                    End If
                Case ".cpp", ".c", ".cxx"
                    OtherPath = IO.Path.ChangeExtension(CurrentPath, ".h")
                    If (Not IO.File.Exists(OtherPath)) Then
                        OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hpp")
                        If (Not IO.File.Exists(OtherPath)) Then
                            OtherPath = IO.Path.ChangeExtension(CurrentPath, ".hxx")
                        End If
                    End If
                Case Else
            End Select
            If (OtherPath <> Nothing) Then
                DTE.ItemOperations.OpenFile(OtherPath)
            End If
        End If

    Catch ex As System.Exception
        MsgBox(ex.Message)
    End Try
End Sub

Here's a (very wide :)) screenshot showing what the macro editor and hotkey/options dialogs should look like, to help those not familiar with them:

enter image description here

6
  • 1
    Another favourite VS macro trick: Record a macro of attaching the debugger to the exe you're always working on, then stick that on the toolbar. One-click attachment to that running process whenever you need to debug it. Nov 29, 2010 at 23:30
  • wow, that's great! Works better than the Nifty Solutions Plugin, because the Plugin can only switch between .h and .cpp, it doesn't switch from .hpp to .cpp
    – j00hi
    Nov 25, 2011 at 15:55
  • But where to put the macro? For some reason my VS2010 already has a "Module1" macro... so I double-clicked to edit it and inserted the macro code inside "Public Module Module1". From there Tools => Options => Environment => Keyboard to set up a shortcut in Text Editor.
    – Qwertie
    May 25, 2012 at 19:03
  • Excellent, tested and working. Answer posted in Nov 2010 - It still works in July 2012. Perfect!
    – Puttaraju
    Jul 12, 2012 at 17:43
  • 1
    Alex: Macros are global to Visual Studio. They are saved in "macro projects" (which let you save and load groups of macros) but those are unrelated to the usual type of solution/project, as far as I can tell. I have several macros defined in the default macro project which are available before (and after) I open any solution or project. Feb 23, 2013 at 17:05
9

Try PhatStudio. It's free and comes with an easy installer.

  • ALT + S = Switch between header/source file

  • ALT + O = Open a file (supports instant search via typing, like the start menu in Windows Vista/7).

1
  • Thanks, this is the easiest solution of the bunch, and free as well. Nov 27, 2011 at 2:29
8

Try Visual Assist, which sports this very feature (amongst others):

http://www.wholetomato.com/

The code browsing functionality -- of which the header/cpp swap is one part -- are really good.

(I also really rated its intellisense and refactoring features, but not everybody I've spoken to has agreed with me.)

EDIT: just remembered, the Nifty Solution Plugin also does this -- plus another handly Visual Assist-like thing, though nothing else -- and they're free:

http://code.google.com/p/niftyplugins/

(The guy's perforce plugin is great, too. Much better than the default VSSCC rubbish.)

1
  • I’ll give Visual Assist a look. I had assumed it was for .NET languages only.
    – Nate
    Nov 29, 2010 at 23:36
7

For Visual Studio 2013, as mentioned by others it's the command named:

EditorContextMenus.CodeWindow.ToggleHeaderCodeFile

and it has as default combination of keys: Ctrl+K,Ctrl+O, but it can be changed if you introduce the new combination of keys that you like in the

Press shortcut keys:

under

Tools -> Options -> Environment -> Keyboard.

So you can choose a custom keys combination as my favorite for .h to .cpp switch is Ctrl+Tab.

5

In Visual Studio 2008 and 2010, you can right click in your .cpp file and choose Go To Header File ... that will take you in one direction. For the other direction, if you right click something you're declaring in the header, and choose Go To Definition, that will take you in the other direction. You might have to go through an ambiguity resolution dialog if you choose the constructor, because the function name matches the class name, but if you choose anything else, you'll go straight where you want. I know this is a two-click approach, rather than one keystroke, but it does do what you want.

5

If you position your mouse over a function declaration in the header and press F12, the cpp file will be opened at the definition of the cpp file... I use this feature extensively!

0
5

I don't see this answer here, but at least in Visual Studio 2012 (Express included!), you can just assign your own keyboard command to go to the header file (NOTE: Only goes one way -- you can't go back to the source file unfortunately...)

  1. Go to Tools/Options/Environment/Keyboard.
  2. Find the following command: EditorContextMenus.CodeWindow.GoToHeaderFile
  3. Assign whatever key combination you want (Alt-S works)
  4. Profit

Not sure which versions of VS this works in, but it doesn't require any add-ins and seems to do the trick in at least one direction.

1
  • In Visual Studio 2013 there is a command called EditorContextMenus.CodeWindow.ToggleHeaderCodeFile which does exactly that, toggles between .h and .cpp files. Nov 4, 2013 at 17:26
4

Try using Switch - it's an addin that lets you flick between source and header, code and designer, XAML and codebehind etc etc:

http://www.dwmkerr.com/switch/ or directly from Products and Extensions for Visual Studio

1
3

I'm a fan of Visual Assist for doing this. Its not cheap but it provides a lot more functionality than switching between header and source. I also use its open file in project and class browsing features a lot. Of course the macro is free...

2

There's also a macro listed on the Whole Tomato support forum which has a few more file mappings.

1

Visual assist also does not support Visual studio express editions. So you are stuck with the macro if you are using that IDE>

0

In Visual Studio 2008 it's Alt + O.

1
  • 8
    You must have Visual Assist installed - that's the Visual Assist command keybinding.
    – sean e
    Oct 31, 2010 at 16:00
0

In their (in)finite wisdom, MS decided to remove macros in MSVS 2012, so the macro above won't work.

For MSVS 2012, I found this:

http://www.dwmkerr.com/switch/

It's highly configurable + if you want to help improving it, you can do so on GitHub.

0
0

In Visual Studio 2013 it's ALT + O

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.