How can you sort tabs in Visual Studio 2008 by file type? - visual-studio

I have been trying (without much luck) to write a macro for Visual Studio 2008 that will sort opened .cpp and .h files into two tab groups, placing headers on the left tab group and .cpp files on the right. I would love to have this feature because of the amount of time I spend moving my tabs around so that I can see both parts of the class that I am working on. I know that there is a non-free add-on for visual studio that allows tab managing, but it conflicts with an add-on that I need to use for work, making a macro my best option so far.
I am sure that this could be applied to other layouts and sorting needs as well if I can get it working. My thinking was to make the sorting happen automatically for me each time I open a document window, so I created a visual basic macro in the environment events section of the Visual Studio Macro IDE. Below is the code that I have so far:
Public Sub keepHeaderAndCxxInDifferentTabs() Handles WindowEvents.WindowCreated
Dim openedFile As String
openedFile = ActiveWindow.Document.FullName
If openedFile.Contains(".h") Then
' if the file being opened is a header file make sure it appears on the left
If DTE.ActiveDocument.ActiveWindow.Left > 600 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
ElseIf openedFile.Contains(".cpp") Then
' if the file being opened is a cpp file make sure it appears on the right
If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
Else
' if the file being opened is anything else make sure it appears on the right
If DTE.ActiveDocument.ActiveWindow.Left < 250 Then
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
End If
End Sub
Sadly this macro currently does not do anything. My best guess was that I could use the 'Left' property of the active window to figure out which tab group the new window appeared in and then move the window to the next tab group if it is not where I want it to be. I have tried a range of different values for the 'Left' property but so far none of my tabs move.
Does anyone know what I did wrong or have any suggestions? Thank you in advance for your time.

I found a way to do what I wanted using the function below. As long as I have my two vertical tabs setup when I run the macro it puts all headers in the left tab group and all other files in the right tab group. This can be further extended so that when I open any files using any other macros I write it sorts them as well by making a call to it after the macro has run. Sadly this does not work automatically, I am having problems getting it to actually perform the sorting whenever a specific event is triggered (using the environment events section).
'=====================================================================
' Sorts all opened documents putting headers into the left tab group
' and everything else into the right tab group
'=====================================================================
Public Sub SortFilesInTabs()
For i = 1 To DTE.Windows.Count Step 1
If DTE.Windows.Item(i).Document IsNot Nothing Then
If DTE.Windows.Item(i).Document.FullName.Contains(".h") Then
' if the file is a header file make sure it appears on the left
If DTE.Windows.Item(i).Document.ActiveWindow.Left > 600 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to left")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoPreviousTabGroup")
End If
ElseIf DTE.Windows.Item(i).Document.FullName.Contains(".cpp") Then
' if the file is a cpp file make sure it appears on the right
If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
ElseIf DTE.Windows.Item(i).Document.FullName.Length > 0 Then
' if the file is any other valid document then make sure it appears on the right
If DTE.Windows.Item(i).Document.ActiveWindow.Left < 250 Then
WriteOutput("moved file " & DTE.Windows.Item(i).Document.FullName & " to right")
DTE.Windows.Item(i).Document.Activate()
DTE.ExecuteCommand("Window.MovetoNextTabGroup")
End If
End If
End If
Next i
End Sub
If anyone can improve this further pl

Related

Margins Incorrect in Word Document after pasting from one document to another using VB6

I have inherited a VB6 app and I could do with some help with part of it.
The code opens a word document and copies its contents. Once this is complete it will open another document and paste the contents from the first document into the second. The opening, copying and pasting works ok, the issue comes with the formatting of the pasted text and the section break it follows. Instead of appearing straight after the section break it is being put on another page, the section break does however still say it is continuous. I've done some digging and tried what it says in the following
Stop Margin Adjustment when pasting - Microsoft Community
Problems with margins when I copy and paste a document into template - Microsoft Community
Section break causes unexpected page break in word
Troubleshoot page breaks and section breaks - Word - Office.com
None of these have helped. A cut down version of the code is as follows:
GetWord97Object objWordApp
objWordApp.Visible = True
objWordApp.documents.Open strCopyFromDoc
DeleteHeadersAndFooters objWordApp.documents(strCopyFromDoc)
objWordApp.documents(strCompyFromDoc).content.Copy
objWordApp.documents.Open strCopyToDoc
objWordApp.documents(strCopyToDoc).characters(objWordApp.ActiveDocument.characters.Count).Select
Set objRng = objWordApp.ActiveDocument.content ' Range used so as not to overwrite original text
objRng.Collapse Direction:=0
If IsWordAppVersionLessThan2002(CInt(objWordApp.Version)) Then
objRng.Paste
Else
objRng.PasteAndFormat wdPasteDefault
End If
I've tried the paste and format but that hasn't helped.
The version of Word I am using is 2002 SP3 but I need it to work with 2002 and up. The VB6 is at SP6.
Thanks in advance for your help.
I've managed to get rid of the problem. It looks like it was something to do with the document, rather than the code. I've given copying the header and footer from one document to another and that seems to have worked this time. Previous attempts at copying didn't seem to make any difference. Not an ideal solution but at least it is sorted.
I've found the solution and It's more simple that I thought. Just save the document before you paste your content. That makes Word to keep the original margins definitions. In my code I did that.
Private Sub CommandButton4_Click()
Dim Item As String
Dim i As Integer
For i = 0 To ProcList.ListCount - 1
Dim docNew As Document
Dim docproc As Document
Set docNew = Word.ActiveDocument
docNew.Content.Copy
Set docproc = Documents.Add
With docproc
.SaveAs FileName:=ProcList.List(i)
Selection.ClearParagraphAllFormatting
Selection.Paste
End With
Next i"

Single Space Indent

Sometimes when i'm working in visual studio, I end up with a block of code that I need to indent a single space in order to make it align completely.
A normal sized indent would be done by selecting the text and pressing the tab key, however if I just want to move it along one space, selecting the text and pressing the space bar overwrites the code.
I know I could do this this by changing the tab spacing option so the indent size is 1, indenting the text and then changing it back but this seems a bit longwinded...
I've had no luck searching, so I've written a macro to do the above, but I thought i'd ask on here before i resigned to using it just in case the function/shortcut already existed...
Edit: Macro moved to answers
Here is said macro for anyone interested:
Sub SingleSpaceIndent()
Dim textEditor As Properties
textEditor = DTE.Properties("TextEditor", "AllLanguages")
textEditor.Item("IndentSize").Value = 1
DTE.ActiveDocument.Selection.Indent()
textEditor.Item("IndentSize").Value = 4
End Sub

Turn Off Whole Line Copy in Visual Studio

There is a setting in Visual Studio 2010 to turn off copy and cut commands when the cursor is on a blank line and there is no selection. However, when the cursor is not on a blank line and you press ctrl+C, it always copies the entire line to the clipboard. I find this very irritating because I always highlight something first, copy it, then place the cursor where I want to paste it and press ctrl+V. However, sometimes I miss the v and hit the c, which replaces the text on the clipboard with the text of the current line and I have to start all over...
Does anyone know how to turn off copying when there is no selection, regardless of whether the cursor is on a blank line or not?
There is the option in the settings:
Go to Tools - Options -> Text Editor -> ALl Languages -> Apply Cut or Copy commands to blank lines when there is no selection
Also if you accidentally copied something into clipboard you can use following shortcut:
Ctrl+Shift+V – cycle through the clipboard ring.
EDITED:
It seems there is no option to turn of it because by default Ctrl-C is assigned to Edit.Copy command, which copies the current line if nothing is selected. However you can assign following macro to Ctrl-C and it should fix the issue:
Sub CopyOnlyIfSelection()
Dim s As String = DTE.ActiveDocument.Selection.Text
Dim n As Integer = Len(s)
If n > 0 Then
DTE.ActiveDocument.Selection.Copy()
End If
End Sub
I know this is old question, but as Macros are no longer natively supported in newer versions of Visual Studio, I thought I'd shared my new extension (cause I couldn't find any existing extensions): https://marketplace.visualstudio.com/items?itemName=KiwiProductions.CopyOnlySelection

Visual Studio 2010 macro recording not picking up startofline-firstcolumn presses

In Visual Studio 2008 I would regularly record a macro to (for example) take a list of class member declarations and turn it into a list of property definitions.
Using Visual Studio 2010, the macro recorder appears to ignore a 2nd press of the Home key (the 1st should take you to the start of the text on the line, the 2nd should take you to the character position 1 on the line).
Putting the cursor at the end of a tabbed line, starting to record and pressing "Home" twice results in the following (it doesn't matter whether the tabs are actual tabs or spaces)...
Sub TemporaryMacro()
DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
End Sub
When what it should really put is...
Sub TemporaryMacro()
DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstText)
DTE.ActiveDocument.Selection.StartOfLine(VsStartOfLineOptions.VsStartOfLineOptionsFirstColumn)
End Sub
I know I could just go in and edit it, but as I normally record something and then immediately run it multiple times, that is not appealing. And I'm not aware of any keypress that will take you to the first column.
Can anybody confirm if this is a fault with the macro recorder in VS2010, or am I doing something really stupid?
This does not work for me, either, meaning it does the same thing for me as it does for you, and the macro does not play back correctly.
As a workaround, I use the key combo [end, right, up] to go to the first character position when recording a macro. This translates into:
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.CharRight()
DTE.ActiveDocument.Selection.LineUp()
The only time this doesn't work is when you operate on the last line of the file. If you anticipate that happening you could use [up, end, right] instead:
DTE.ActiveDocument.Selection.LineUp()
DTE.ActiveDocument.Selection.EndOfLine()
DTE.ActiveDocument.Selection.CharRight()
Of course, this version would not operate correctly on the first line of the file.

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.
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.
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.
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)
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.
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.
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
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.
Ctl + Alt + PgUp or PgDn shortcuts worked to toggle next/prev tab out of the box for me...
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.
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!
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.
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.
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.
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.
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.
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.

Resources