Mr Speaker

Visual Studio Tab Un-stupidifier Macro

Necessity is the mother of dodgy macros, they say. Here's one I made to fix the stupid most recently- to last recently-used document tab order of Visual Studio. But before the code, here is my harrowing tale....

I remember doing user interface design courses in my undergrad study, and thinking "I don't need to pay attention to this - by the time I'm out in the real world UI standards will be firmly in place."

I remember this today as I try to tab to an open document in Visual Studio.

I can see the tab - it is right in front of me, but ctrl-tab as I might I can not get to it. The active document jumps around from document to document until finally, like a really really frustrating game of roulette, I land on the code page I seek. Seeked. Sake?

By this time I had forgotten why I was even going to the page, and realised that every time I hit ctrl-tab (or ctrl-f6 in VS 2005), I ground my teeth just a little bit more.

Don't get me wrong, being able to go to the last file you used is pretty handy. It's just not as handy as being able to scroll through your open documents without having to yell rude words at your co-workers.

mru vs normal tab orderIf fact, the MRU order is good if you only ever need to access the last file you were working on. Which is unlikely to be the case if you are working on a chunky project with lots of documents open. See my graph for clarification.

So I had a look around for the necessary keyboard short-cut, or registry setting I needed to apply to remove someone's great idea for changing the way tabs work. I couldn't find it, so in a fit of angriness, I ended up writing this dodgy macro. It's probably THE worst way to accomplish the task, but hey - I use a GOTO statement, so it can't be all bad:

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

I don't know if it works in VS2005 - I only have the freebie version.

All that's left to do then is to map TabForward to ctrl-tab and TabBackward to ctrl-shift-tab. You do this in tools->options->keyboard. I then set the old Window.NextDocumentWindow to ctrl-f6 so if I want to use the MRU order I can.

So far I haven't wanted to.

29 Comments

  1. “Sought”

    Friday, October 13, 2006 at 2:51 pm | Permalink
  2. I really think it should be “Sorked”.

    Also, now that I’m free from the shackles of the MRU tab order, I realise that this post is a bit nastier than it needed to be. Sorry MRU tab order – you do come in handy sometimes. I didn’t mean to get angry.

    Friday, October 13, 2006 at 3:55 pm | Permalink
  3. It works! Even under Visual Studio 2005!
    I was looking for a beer oclock update for ff2.0 (ok, it’s not too harder to manually fix the version, but something official is way better) and I found this wonderful macro.

    You save my sanity :-P

    ciao

    Saturday, October 28, 2006 at 1:31 am | Permalink
  4. Thank you so much!!!

    This is really the best post I’ve ever seen online, bar none :) well pr0n doesn’t count.

    please put some metatags in so it’s easier to find, maybe “fix visual studio 2005 tab order” ctrl+tab, etc.

    thanks again

    Tuesday, November 7, 2006 at 2:41 am | Permalink
  5. Works for me too in VS2005! Thank you so much!

    Now if only someone knows how to disable this amazingly impressively stupid feature of VS2005 which makes a “pop” sound every time I run this macro, as well as displaying a balloon notification…

    Sunday, February 18, 2007 at 4:18 am | Permalink
  6. I said that too fast. It actually only works in limited circumstances – namely, you must not reorder tabs. If you do, it breaks. Which almost defeats the purpose for me.

    Does anyone know how to detect such reordered tabs?

    Sunday, February 18, 2007 at 7:12 am | Permalink
  7. Hmmm… I have only just started using vs2005, and didn’t even know you could re-order the tabs – thanks!

    I’ll check it out on Monday when I’m back on my windows box.

    (Oh, and there is a way to stop the macro notification… but I forget how I did it!)

    Sunday, February 18, 2007 at 10:28 am | Permalink
  8. Tnank you X infinity

    The idiotic tab order AND even stupider tab **DIALOG** gone in one simple pair of macros =)

    This has been one of the most irritating points of VS, made worse by the fact that fools _copy_ it (!), and I can’t thank you enough. I didn’t even know this kind of thing was possible with macros.

    I came here looking for something to automatically rearrange tab orders on a windows form, but this is better =) And does anyone else notice that all of ms/vs is now just an attempt to recreate Access?

    Thursday, May 31, 2007 at 8:59 am | Permalink
  9. The balloon and popping sound can be disabled using the advice here: http://tinyurl.com/34yznu

    In summary, you need to add the following to your registry:

    [HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\8.0]
    “DontShowMacrosBalloon”=dword:00000006

    Wednesday, March 26, 2008 at 7:12 am | Permalink
  10. @Roman: You don’t even have to reorder tabs for the script to break. Some combinations of tabbing around and closing files also break the script. DTE.Windows simply isn’t guaranteed to give items to you in the tab bar order.

    We need direct programmatic access to the tab bar if we’re to get the behavior we want. Either that, or VS 10 has got to allow it as an option. Tabbed web browsers are simply too widespread these days to cling to old UI paradigms.

    Monday, April 28, 2008 at 5:45 am | Permalink
  11. That’s funny, i love that it acts as a stack rather than linear list. This way if i want to toggle between two files i dont have to tab through the other 50 that are open. I find that i can keep a good number of docs in my memory with out trying so a CTRL+TAB+TAB+TAB+TAB is alot easer than finding it in a tab list that could require scrolling the viewport.

    Wednesday, September 24, 2008 at 1:15 am | Permalink
  12. This reorders the tab bar alphabetically. Won’t help with Ctrl+Tab but might make finding the right file using the mouse a bit easier…

    Public Class myReverserClass
    Implements IComparer

    ‘ Calls CaseInsensitiveComparer.Compare with the parameters reversed.
    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
    Implements IComparer.Compare
    Return New CaseInsensitiveComparer().Compare(y, x)
    End Function ‘IComparer.Compare

    End Class ‘myReverserClass

    Sub TemporaryMacro()
    Dim keyList = New ArrayList
    Dim doc As Document
    Dim value As String
    For Each doc In DTE.Documents
    value = doc.Name & vbTab & doc.FullName

    If Not keyList.Contains(value) Then
    keyList.Add(value)
    End If
    Next
    ‘ Sort in reverse order
    Dim myComparer = New myReverserClass()

    keyList.Sort(myComparer)

    ‘ Move each window to the new tab group (moving the last one will close the original tab group).
    Dim fName As String, fPath As String
    Dim TestArray() As String
    For Each value In keyList
    TestArray = Split(value, vbTab)
    If TestArray.Length > 1 And TestArray(1) “” Then
    DTE.Documents.Item(TestArray(1)).ActiveWindow.Activate()
    OpenInOtherWindow()
    End If
    Next
    End Sub
    Private Sub OpenInOtherWindow()
    Try
    DTE.ExecuteCommand(“Window.MovetoNextTabGroup”)
    Catch
    Try
    DTE.ExecuteCommand(“Window.MovetoPreviousTabGroup”)
    Catch
    DTE.ExecuteCommand(“Window.TileVertically”)
    End Try
    End Try
    End Sub

    Saturday, January 10, 2009 at 9:47 am | Permalink
  13. Ummm – there are ide-supplied commands to achieve this.

    Set the mappings below:
    ctrl-tab: Window.NextDocumentWindow
    ctrl-shift-tab: Window.PreviousDocumentWindow

    It’s the first thing I do when working with VS on a new PC. No macros, and no tray icon bubbles.

    Neil.

    Tuesday, August 18, 2009 at 1:10 am | Permalink
  14. Neil,

    Window.NextDocumentWindow also cycles based on MRU.

    Tuesday, September 22, 2009 at 1:07 am | Permalink
  15. For what it’s worth I like both MRU and “Order of Tab” flipping. I’ve left ctrl+tab alone because I frequently switch back and forth between two files, and used ctrl+PgUp/PgDn (learned from firefox) for your lovely macro. Now I am truly a happy man.

    Btw does anyone know why the Window.NextTab and Window.PreviousTab don’t seem to do anything? Are they not talking about document tabs or something?

    Saturday, October 24, 2009 at 7:53 am | Permalink
  16. @Tabber: Commercial Tabs Studio add-in for Visual Studio gives programmatic access to the tab bar and offers lots of tab management and customization features:
    http://www.tabsstudio.com

    Friday, November 27, 2009 at 12:50 pm | Permalink
  17. From the corners of South America my grattitude to you! This is amazing :)
    I’ve been looking for this for quite a while…
    Too bad to had to resort to f@#$! macros! Why didn’t Ms left a “OrderedNextTab” “OrderedPreviousTab”…

    Thanks again!

    Saturday, November 28, 2009 at 4:57 am | Permalink
  18. Thaaaaaaank you!

    Wednesday, February 10, 2010 at 6:58 am | Permalink
  19. Doesn’t seem to work on Visual Studio 2008 :(

    Wednesday, March 17, 2010 at 10:25 pm | Permalink
  20. I would give you sixty trillion mightybucks if you can get this working reliably in Visual Studio 2010. Arrrggghhh! Visual Studio 2010 is such an awesome IDE but it’s Ctrl+Tab behaviour makes me weep like a little bitch.

    Thursday, May 20, 2010 at 1:19 am | Permalink
  21. Works fine for me in Visual Studio 2010 binding the macro to the Global Ctrl+Tab and Ctrl+Shift+Tab keys. I did end up hiding the bubble by setting Visual Studio 2010 to “Hide icon and notifications” from the system tray in Windows 7, but otherwise it does exactly what I wanted.

    Also I posted a bug on Connect regarding non-MRU lookup, and they said that they will try and add it to the next release of Visual Studio. More info here: http://goo.gl/K9rz

    Thursday, July 22, 2010 at 2:42 am | Permalink
  22. David: in VS2010 you can use the “window.nextTab” and “window.previousTab” functions (Tools -> Options… , Environment -> Keyboard).

    Saturday, October 23, 2010 at 5:29 am | Permalink
  23. I couldn’t disagree more. MRU is awesome. We all work in different ways, I’m usually working with 2-3 files at any given time and being able to get back and forth between them easily and without having to constantly scan the open files bar is priceless. Long live MRU!!!

    Saturday, November 12, 2011 at 3:09 am | Permalink
  24. No joy. In my current version of VS2005, the macros just do the usual jumping-around-the-tabs-randomly thing. Same as the builtin next/previous. Perhaps some recent MS update broke it?

    Wednesday, January 11, 2012 at 8:43 am | Permalink
  25. Finally in vs 2012 this is implemented out of the box.
    ctrl+alt+pgdown/pgup (Window.NextTab/Window.PreviousTab commands)

    Also works in vs 2010, but you need the productitivity power tools extension.

    The macro is great, and works in vs 2012 (in vs 2012 you need to write a custom extension, no longer macro support) but has the following unavoidable(?) problems:
    – opening new tabs to the left (the stupid vs 2005 way) breaks it
    – reordering tabs breaks it
    – sometimes the same window appears many times in the dte.windows collection, this has side effects (which should be the active window?)

    Thanks for the macro, valuable for learning extension programing even with vs 2012!

    Tuesday, December 11, 2012 at 3:52 am | Permalink
  26. Forget the above, Window.NextTab only cycle the visible tabs, so total useless:(

    Tuesday, December 11, 2012 at 7:34 am | Permalink
  27. I’ve found an intresting alternative in vs 2012, which finally really works.
    Usually we need the left to right navigation only when doing similar tasks on every open files (for just navigating between tons of open document, it is better to use ctrl+, /navigateto/, or alt+w,alt+w /no mru order, instead alphabetical/):
    – turn on “show pinnded tabs in a separate row” in options
    – pin all open document, the pinned documents appears in multiple line tabs (which feature is badly missing for not pinned documents)
    – now ctrl+alt+pgdown works and cycle left to right all open document

    A macro to toggle pin all open document (needs extensibility project), I found no way to determine whether a document is pinned or not, so this is still not ideal but…

    private void TogglePinTabAll( )
    {
    try
    {
    Window activeWindow = dte.ActiveWindow;
    for( int i = 1; i <= dte.Windows.Count; i++ )
    {
    if( dte.Windows.Item( i ).Kind == "Document" )
    {
    dte.Windows.Item( i ).Activate( );
    dte.ExecuteCommand( "Window.PinTab" );
    }
    }

    activeWindow.Activate( );
    }
    catch { }
    }

    Tuesday, December 11, 2012 at 7:11 pm | Permalink
  28. Working on Visual Studio 2008, thank you.

    Thursday, February 14, 2013 at 1:54 am | Permalink
  29. Macro has been dropped out in recent VS versions. An extensive extension for VS 2008/2010/2012/2013 may deserve a try, Visual Smarter:
    http://visualsmarter.blogspot.com/
    It has already more than 200 widgets and coders.

    Thursday, August 6, 2015 at 5:51 pm | Permalink

2 Trackbacks/Pingbacks

  1. […] this and this blogger going by the name of Mr Speaker has the necessary macro code. Navigate to his blog post and make use of his macro. After you apply his macro to your VS you can bind your favorite keyboard […]

  2. […] http://www.mrspeaker.net/2006/10/12/tab-un-stupidifier/ […]

Captcha! Please type 'radical' here: *
How did you find this thingo? *