ChatGPT解决这个技术问题 Extra ChatGPT

Changing Ctrl + Tab behavior for moving between documents in Visual Studio

Is it possible to change how Ctrl + Tab and Shift + Ctrl + Tab work in Visual Studio? I have disabled the popup navigator window, because I only want to switch between items in the tab control. My problem is the inconsistency of what switching to the next and previous document do.

Every other program that uses a tab control for open document I have seen uses Ctrl + Tab to move from left to right and Shift + Ctrl + Tab to go right to left. Visual Studio breaks this with its jump to the last tab selected. You can never know what document you will end up on, and it is never the same way twice.

It is very counterintuitive. Is this a subtle way to encourage everyone to only ever have two document open at once?

Let's say I have a few files open. I am working in one, and I need to see what is in the next tab to the right. In every other single application on the face of the Earth, Ctrl + Tab will get me there. But in Visual Studio, I have no idea which of the other tabs it will take me to. If I only ever have two documents open, this works great. As soon as you go to three or more, all bets are off as to what tab Visual Studio has decided to send you to.

The problem with this is that I shouldn't have to think about the tool, it should fade into the background, and I should be thinking about the task. The current tab behavior keeps pulling me out of the task and makes me have to pay attention to the tool.

"I have disabled the popup navigator window"... How did you turn off the annoying select-window popup? It's so annoying just flickering every time I press Ctrl+Tab.
Ah got it, in Tools->Options->Keyboard I remapped Ctrl+Tab from Window.NextDocumentWindowNav to Window.NextDocumentWindow (removed the "Nav"). And the same with Ctrl+Shift+Tab with Window.PreviousDocumentWindowNav.
Is it wrong that I like this behavior, as it's just like alt-tab?
Visual Studio appears to navigate through tabs using the "most recently used" (MRU) behavior. Notepad++ offers an option to enable/disable this behavior (so it seems some people like it). I would really like to be able to switch to regular/intuitive behavior though.
Honestly, I don't like when a program cycles through in visual tab order. I agree with Visual studio's behaviour. For every ctrl+tab, it goes back to the one last used, then the one last used before that. The pattern has been there for a very long time (VB6 or before I think). I mean, use the pattern you prefer, but if you know how it works, it's easier to appreciate :)

J
Josh1billion

In Visual Studio 2015 (as well as previous versions of VS, but you must install Productivity Power Tools if you're using VS2013 or below), there are two new commands in Visual Studio:

Window.NextTab and Window.PreviousTab

Just go remap them from Ctrl+Alt+PageUp/Ctrl+Alt+PageDown to Ctrl+Tab/Ctrl+Shift+Tab in:

Menu Tools -> Options -> Environment -> Keyboard

Note: In earlier versions such as Visual Studio 2010, Window.NextTab and Window.PreviousTab were named Window.NextDocumentWellTab and Window.PreviousDocumentWellTab.


This is the best answer now for visual studio 2010. This was very helpful. The accepted macro solution is inferior to this one.
No version yet for Visual Studio 2012
This won't help if you use Visual Studio Express either
With Visual Studio 2012, the command names have changed slightly. It's now Window.NextTab and Window.PreviousTab.
@JP ten Berge - I've tested this in VS 2012, and the Window.PreviousTab shortcut (Ctrl+Alt+PgUp) doesn't work if you're on a Razor view (.cshtml file). Very annoying, although Ctrl+Alt+PgDown still works so you're not completely stuck. There's also Window.NextDocumentWindow (Ctrl+F6) although that seems to cycle tabs in a strange order.
R
Rohit Gupta

Visual Studio 2010 has, built in, a way to solve this.

By default, Ctrl+Tab and Ctrl+Shift+Tab are assigned to Window.[Previous/Next]..Document, but you can, through

Tools -> Options -> Environment -> Keyboard,

remove those key assignments and reassign them to Window.[Next/Previous]Tab to add the desired behavior.


Apparently someone has confused the reasoning behind a downvote- If an answer does not comply with the question or if it is actually negative it deserves one... But mine answers the question correctly, albeit less so than your answer. Is it just that mine expects people to actually understand an options menu rather than "Here, run this script" or, "Install this addon!"... Isn't this a site for programmers?
People may have been looking to switch between document tabs. Editing 'Window.[Next/Previuos]Tab' didn't affect the document tabs for me in VS2010 whereas @thepaulpage 's solution did. +1 for the clear and addon-free approach!
⁺¹, also I just had a thought why someone could downvote your answer: Visual Studio 2010 had some weird version that had no the «Keyboard» option. So probably the one had this version, looked at the options, and decided that the answer wrong since with their subjective point of view it is indeed so.
I had to remove the binding from the Window.[Previous|Next]DocumentWindow setting for this to work (in VS2015). By default, there was a separate text editor binding for it that seemed to take priority.
M
Mwiza

it can be changed, at least in VS 2012 (I think it should work for 2010 too).

1) TOOLS > Options > Environment > Keyboard

(Yes TOOLS, its VS2012 !) Now three shortcuts to check.

2) Window.NextDocumentWindow - you can reach there quickly by typing on the search pane on top. Now this is your enemy. Remove it if you dont like it. Change it to something else (and dont forget the Assign button) if want to have your own, but do remember that shortcut whatever it is in the end. It will come handy later.

(I mean this is the shortcut that remembers your last tab)

https://i.stack.imgur.com/GYO0E.png

3) Now look for Window.NextDocumentWindowNav - this is the same as above but shows a preview of opened tabs (you can navigate to other windows too quickly with this pop-up). I never found this helpful though. Do all that mentioned in step 2 (don't forget to remember).

4) Window.NextTab - your magic potion. This would let you cycle through tabs in the forward order. May be you want CTRL+TAB? Again step 2 and remember.

5) Now place cursor in the Press shortcut keys: textbox (doesn't matter what is selected currently, you're not going to Assign this time), and type first of the three (or two or one) shortcuts.

https://i.stack.imgur.com/PRiW9.png

You'll see Shortcut currently used by: listed. Ensure that you have no duplicate entry for the shortcut. In the pic, there are no duplicate entries. In case you have (a rarity), say X, then go to X, and remove the shortcut. Repeat this step for other shortcuts as well.

6) Now repeat 1-5 for Previous shortcuts as well (preferably adding Shift).

7) Bonus: Select VS2005 mapping scheme (at the top of the same box), so now you get F2 for Rename members and not CTRL+R+R, and F7 for View Code and not CTRL+ALT+0.

I'm of the opinion VS has got it right by default. I find it extremely useful that VS remembers what I used last, and makes switching easier, much like what the OS itself does (on ALT+TAB). My browser does the same too by default (Opera), though I know Firefox behaves differently.


I don't find this behavior anti-non-counter-intuitive too. ;-)
the opinion at the top of this answer has always bugged me as un-informative and (because of the large size) needlessly antagonistic.
@thepaulpage I have updated my answer. The reason I had to give an opinion is because many users (including OP) had given their opinion that it is counter-intuitive.
I've never used alt+tab due to how unintuitive it is for me. I use ctrl+tab constantly in browsers and np++ so the alt+tab-like default behaviour of VS is completely useless to me. I already know which tab I need to get to, I don't want the extra thought required to remember if I went to some other tab recently or not.
C
Colonel Panic

In Visual Studio 2012 or later (2013, 2015, 2017...):

Browse the menu Tools / Options / Environment / Keyboard. Search for the command 'Window.NextTab', set the shortcut to Ctrl+Tab Search for the command 'Window.PreviousTab', set the shortcut to Ctrl+Shift+Tab

https://i.stack.imgur.com/8WKmE.png


For reference, by default, Ctrl+Tab and Ctrl+Shift+Tab are mapped to the commands 'Window.NextDocumentWindowNav' and 'Window.PreviousDocumentWindowNav'.
P
Peter Mortensen

Navigate to the blog post Visual Studio Tab Un-stupidifier Macro and make use of the macro. After you apply the macro to your installation of Visual Studio you can bind your favorite keyboard shortcuts to them. Also notice the registry fix in the comments for not displaying the macro balloon since they might get annoying after a while.


It apparently still breaks under some conditions, but this is the clostest one to actually working as desired.
In VS 2010 you can configure Ctrl+Tab and Ctrl+Shift+Tab to change between text documents, but it does not work with designer tabs or the BinaryEditor. The macro from the above blog post is the only method that works with the majority of document window types. I also filed a bug with Microsoft that has more details ( goo.gl/K9rz ), and they said that they will change Window.NextTab and Window.PreviousTab to provide non-MRU switching for documents in the "next Visual Studio version".
F
FlashJordan

Ctl + Alt + PgUp or PgDn shortcuts worked to toggle next/prev tab out of the box for me...


The illogical thing is: If your tabs bar has only horizontal space for n Tabs, those shortcuts will not make the tab cycling for the tabs that are not visible at the moment!
M
Mwiza

After a couple of hours of searching I found a solution how to switch between open documents using CTRL+TAB which move from left to right and SHIFT+ CTRL+ TAB to go right to left.

In short you need to copy and paste this macro:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module TabCtrl

Public Sub TabForward()
    Dim i As Integer
    Dim activateNext As Boolean = False

    For i = 1 To DTE.Windows.Count
        If DTE.Windows().Item(i).Kind = "Document" Then

            If activateNext Then
                DTE.Windows().Item(i).Activate()
                GoTo done
            End If

            If DTE.Windows().Item(i) Is DTE.ActiveWindow Then
                activateNext = True
            End If
        End If
    Next

    ' Was the last window... go back to the first
    If activateNext Then
        For i = 1 To DTE.Windows.Count
            If DTE.Windows().Item(i).Kind = "Document" Then
                DTE.Windows().Item(i).Activate()
                GoTo done
            End If
        Next
    End If
done:

End Sub

Public Sub TabBackward()
    Dim i As Integer
    Dim activateNext As Boolean = False

    For i = DTE.Windows.Count To 1 Step -1
        If DTE.Windows().Item(i).Kind = "Document" Then

            If activateNext Then
                DTE.Windows().Item(i).Activate()
                GoTo done
            End If

            If DTE.Windows().Item(i) Is DTE.ActiveWindow Then
                activateNext = True
            End If
        End If
    Next

    ' Was the first window... go back to the last
    If activateNext Then
        For i = DTE.Windows.Count To 1 Step -1
            If DTE.Windows().Item(i).Kind = "Document" Then
                DTE.Windows().Item(i).Activate()
                GoTo done
            End If
        Next
    End If
done:

End Sub

End Module

The macro comes from: www.mrspeaker.net/2006/10/12/tab-un-stupidifier/

If you never add a macro to Visual Studio there is a very useful link how to do it.


Does it still take 10 seconds to do this the first time, and flash a system tray icon every subsequent time you use it? That's what VS2005 did, and I couldn't take it...
Romkyns: Yes, I'm afraid. Still not a viable solution.
Romkyns: yes it's true, every time I execute the macro, a balloon pops up in the system tray to indicate the macro is running. to get rid of that ballon you may edit the Windows Registry and add the following DWORD value: "DontShowMacrosBalloon" = 1, in that key: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0 (use 9.0 for Visual Studio 2008; 8.0 for 2005). The balloon disappears but system tray icon still will be appear. For me this solution is good enough I have not found a better and I hate orginalne ctrl+tab behavior.
P
Peter Mortensen

The philosophy of the Visual Studio tab order is very counterintuitive since the order of the displayed tabs differs from the tab-switching logic, rendering the ordering of the tabs completely useless.

So until a better solution arises, change the window layout (in Environment->General) from tabbed-documents to multiple-documents; it will not change the behaviour, but it reduces the confusion caused by the tabs.

That way you will also find the DocumentWindowNav more useful!


H
Hulk1991

I'm 100% in agreement with Jeff.

I had worked on Borland C++ Builder for several years and one of the features I miss most is the 'correct' document tabbing order with Ctrl-Tab. As Jeff said, "The current tab behavior keeps pulling me out of the task and makes me have to pay attention to the tool " is exactly how I feels about this, and I'm very much surprised by the fact that there aren't many people complaining about this.

I think Ctrl-F6 - NextDocumentWindowNav - navigates documents based on the document's last-activated time. This behavior is a lot like how MDI applications used to behave in old days.

With this taken this into account, I usually use Ctrl+F6 to switch between 2 documents (which is pretty handy in switching between .cpp and .h files when working on c++ project) even when there are more than 2 currently opened documents. For example, if you have 10 documents open (Tab1, Tab2, Tab3, ...., Tab10), I click on Tab1 and then Tab2. When I do Ctrl+F6 and release keys, I'll jump to Tab1. Pressing Ctrl+F6 again will take me back to Tab2.


c
chicks

I guess you want what VSS calls Next(Previous)DocumentWindow. By default, it's on Ctrl(-Shift)-F6 on my VSS 8. On Ctrl(-Shift)-Tab they have Next(Previous)DocumentWindowNav. You can change key assignments via Tools/Options/Keyboard.


Ctrl-(Shift-)F6 doesn't get you to the left or right tab. its the same order as Ctrl-Tab but without the annoying window.
Correct... Visual Studio 2010 has Window.NextTab and Window.PreviousTab, which does non-MRU based document changing, but it does not work with all document types: connect.microsoft.com/VisualStudio/feedback/details/571750/…
r
riffraff

In registry branch: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0 add DWORD named "UseMRUDocOrdering" with value of 1. It will order documents so most recently used are placed on the left. It's not perfect but better than the default misbehaviour.


J
JWCS

Updated to VS 2017+, where, according to @J-Bob's comment under @thepaulpage's answer, (emphasis added):

Looks like the commands have changed again. It's now 2017 and the keyboard shortcuts are called Open Next Editor and Open Previous Editor. You don't need any extensions for this.

You can find the options under Settings, which can be accessed via the gear symbol in the lower left, or by the [Ctrl]+, command.


j
joepcash

I feel the top answer at the moment is outdated. In Visual Studio 2021 (v1.56), you do not need to install any extensions or mess around with any configuration files. You simply need to do the following steps:

Click the gear icon in the bottom-left. Select 'Keyboard Shortcuts'. Search for 'workbench.action.previousEditor' and 'workbench.action.nextEditor' and edit their keybindings by clicking the pencil icon on the left side of the row.

If you do change to 'Ctrl+tab' or any other shortcut that is already in use by another command, it will let you know and give you the option to change those. I personally changed them to 'Ctrl+PgUp' and 'Ctrl+PgDn' so it was just a straight swap.


This answer is for VScode, although the question is for Visual Studio
P
Peter Mortensen

I don't use Visual Studio (yes, really, I don't use it), but AutoHotkey can remap any hotkey globally or in a particular application:

#IfWinActive Microsoft Excel (application specific remapping)

; Printing area in Excel (@ Ctrl+Alt+A)
^!a::
Send !ade
return

#IfWinActive


$f4::
; Closes the active window (make double tapping F4 works like ALT+F4)
if f4_cnt > 0 
{
    f4_cnt += 1
    return
}

f4_cnt = 1
SetTimer, f4_Handler, 250
return

f4_Handler:
SetTimer, f4_Handler, off

if (f4_cnt >= 2)    ; Pressed more than two times
{   
    SendInput !{f4}
} else {
    ; Resend f4 to the application
    Send {f4}
}

f4_cnt = 0
return

These are two remappings of my main AutoHotKey script. I think it's an excellent tool for this type of tasks.


Sorry, but the problem is that there is no key that does the right thing, so this won't help.