ChatGPT解决这个技术问题 Extra ChatGPT

Open two instances of a file in a single Visual Studio session

I have a file, xyz.cpp. I want to open two instances of this file in Visual studio (BTW, I am using Visual Studio 2005). Why would I want to do so? I want to compare two sections of the same file side by side. I know workarounds such as:

Make a copy of the file. But the problem is that it's not elegant, and I don't want to make copies every time I am faced with this. I can split the window into two. The problem with split it that I can split it horizontally only. The result of a horizontal split is that the right half of my screen is white space.

If I were able to split it vertically or open two instances of the same file, it would increase the number of lines of code I can compare.

If you want to just simply compare parts of the file, you could use Window | Split which will split the file horizontally from the middle.
@vexe Perfect. Exactly what I needed. Splits windows, both editing the same file.
Any reason you haven't accepted the highest-voted answer?
@cp.engr, from his profile: "Last seen Dec 6 '09 at 12:41". It's a shame that high rep users cannot mark answers as accepted.

P
Peter Mortensen

Here's how to do it...

Select the tab you want two copies of Select menu Window → New Window from the menu. Right click the new tab and select New Vertical Tab Group

If New Window is not listed in the *Window menu note that the command does exist, even as of Visual Studio 2017. Add it to the Window menu using menu Tools → Customize → Commands. At that point decide where to put the New Window command and select Add Command.

UPDATED on "30 July 2018"

In Visual Studio Code version 1.25.1 and later

Way 1

You can simple left click on your file in the side-panel (explorer) and press Ctrl + Enter.

Way 2

Simply right click on your file in the Visual Studio Code side-panel (explorer) and select the first option open to the side.


If you have some screen space available like a second monitor, you could also drag the new tab out of Visual Studio and see it, for instance, in the second monitor.
You can only do this for certain file types ... for example, you can't have two windows open on the same .xaml file. Which is a bummer, cause that's what I want to do!
@Shlomi.A. Try Luke's Answer
For XAML in WPF the New Window option is just not there.
This method works, but on my VS 2010 the shortcut [Ctrl] [Shift] [N] was bound to File > New > Project
C
Community

For Visual Basic, HTML and JScript and RDL Expression, the Window > New Window option mentioned in PaulB's answer is disabled. However an option can be changed in the Registry to enable the menu item.

All other languages do not restrict to a single code window so you can use PaulB's answer without editing the registry.

Enabling New Window in Windows Registry.[1] [2]

Go to the following registry key. This example is for Basic (Visual Basic), but the key is also there for HTML, JScript and RDL Expression. 64-bit OS: HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Languages\Language Services\Basic 32-bit OS: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\10.0\Languages\Language Services\Basic Find the value Single Code Window Only and do one of the following: Set it to 0 Rename the value Delete the value (use caution!)

This will enable the "New Window" menu item, but it may still not be visible in the menu.

Adding Menu Item

To actually see the New Window menu item I had to add it back into the menu:

Tools > Customize... > Commands > Add Command...

Select 'Menu Bar' the select the 'Window' menu in the dropdown

Add Command... > Window > New Window > OK

Restoring Registry Value Copy-paste this to notepad, save as a .reg file and import the file into your registry to restore the initial setting.

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\Languages\Language Services\Basic]
"Single Code Window Only"=dword:00000001

No need to edit the registry for C# files as the 'New Window' menu item should be already enabled for C# files (in VS 2010). You only need to do this for VB, HTML or JScript. According to this MS Connect issue report any issues with this are fixed for VB, you just need to remove or rename the registry entry to enable the feature.
This works in Visual Basic 2010 Express as well, I'm happy to report. The registry key is slightly different: [HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\VBExpress\10.0\Languages\Language Services\Basic].
Doesn't work for me. Tried restarting VS2010, will give it another chance after rebooting computer... (who knows how are registry entries read/stored/updated)
I just tested this (I guess my registry key got overwritten by updates or something). I didn't require a reboot. I changed the key with VS2010 running, 'New Window' still greyed out. Then closed VS2010 and opened VS2010 and 'New Window' is available.
In VS2010, flipping the registry bit for JScript does allow a second tab to be opened for a JavaScript file (yay), but nothing except whiteness is rendered in each tab (boo).
P
Peter Mortensen

Go to menu → Windows → New Window:

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


And the nobel prize for best solution in the entire multiverse goes to you @Kishore
simple, visual, helpful!
P
Peter Mortensen

You can use the WindowsNew Window option to duplicate the current window. See more at: Why I like Visual Studio 2010? Undock Windows


P
Peter Mortensen

Open the file (if you are using multiple tab groups, make sure your file is selected).

Menu Window → Split (alternately, there's this tiny nub just above the editor's vertical scroll bar - grab it and drag down)

This gives you two (horizontal) views of the same file. Beware that any edit-actions will reflect on both views.

Once you are done, grab the splitter and drag it up all the way (or menu Window → Remove Split).


(Sri: sorry, 'horizontal' as in the views' rows are aligned ... or one view above the other). AFAIK, vertical (side-by-side) views are not possible.
Appears not to have read OP, where he mentions he is aware of this very thing.
T
Taimoor Ahmad

How to open two instances of the same file side by side in Visual Studio 2019:

Open the file. Click Window -> New Window. A new window should be open with the same file. Click on Window -> New Vertical Document Group.

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


Based on this, can also perform this using keyboard shortcuts: Alt+W,N,Alt+W,V
P
Peter Mortensen

With the your file opened, go to command window (menu View → Other Windows → Command window, or just Ctrl + Alt + A)

Type:

Window.NewWindow

And then

Window.NewVerticalTabGroup

worked for me (Visual Studio 2017).

Or using menus:

Menu Window → New Window

Menu Window → New vertical tap group


You did the Lords work with this my son. Thank you.
P
Peter Mortensen

Luke's answer didn't work for me. The 'New Window' command was already listed in the customize settings, but not showing up in the .js tabs context menu, despite deleting the registry setting.

So I used:

Tools

Customize...

Keyboard...

Scroll down to select Window.NewWindow

And I pressed and assigned the shortcut keys, Ctrl + Shift + W.

That worked for me.

==== EDIT ====

Well, 'worked' was too strong. My keyboard shortcut does indeed open another tab on the same JavaScript file, but rather unhelpfully it does not render the contents; it is just an empty white window! You may have better luck.


Thanks. This worked for me for VS2012/VB.net. Opened Tools - options - (under environment)keyboard - select visual basic 6 from dropdown - select Window.NewWindow from command list - pressed ctrl + n, ctrl + w
A
Anton Gogolev

Window menu, New Horizontal/Vertical Tab Group there will do, I think.


AFAI see, You can't open the same file in different tab groups
P
Peter Mortensen

When working with Visual Studio 2013 and VB.NET I found that you can quite easily customize the menu and add the "New Window" command - there is no need to mess with the registry!

God only knows why Microsoft chose not to include the command for some languages...?


Worked easily for me! Menu Tools > Customize. Then in Commands tab, select Window in the first dropdown, then click Add Command button and voilà! (Visual Studio 2019)
P
Peter Mortensen

For newer versions (such as Visual Studio 2017)

Select the window you want to duplicate.

Go to the window tab and click on split at the top of the list.

When you are done, click it again to toggle it off.


A window split in two halves is not the same as two windows.
J
Jason S

For file types, where the same file can't be opened in a vertical tab group (for example .vb files) you can

Open 2 different instances of Visual Studio

Open the same file in each instance

Resize the IDE windows & place them side by side to achieve your layout.

If you save to disk in one instance though, you'll have to reload the file when you switch to the other. Also if you make edits in both instances, you'll have to resolve on the second save. Visual Studio prompts you in both cases with various options. You'll simplify your life a bit if you edit in only the one instance.


@Odrade - Can you point me to the correct answer that works for all types of files...
@Jason Ok, but it's still incorrect to state that you can't open the same file multiple times to to side-by-side comparison. This is clearly possible, at least for .cs files.
@Odrade You are right. I've edited to try and correct that. Letting you know in case you want to remove comments based on previous versions.
@Odrade Got around to trying Luke's answer It is far better than opening two instances of VS for VB users and has my upvote.
This seems the only working workaround for html files, where the registry edit just gives a blank second window
P
Peter Mortensen

I don't have a copy of Visual Studio 2005, but this process works on Visual Studio 2008:

Open xyz.cpp along with some other file. Right click on tab header and select new vertical tab group. Left click on that other file in the first tab group. Open xyz.cpp through solution explorer again.

You should now have two instances of file in separate vertical tab groups.


On my VS2K5 and VS2K8 it just shows the original instances in the second tab for me. This is if I double click in Solution Explorer, Drag'n'drop from File Explorer, or via File | Open menu.
@Simeon - That happens if there isn't another file open (2nd part of step 1.)
It knows the original file is opened in the other tab group. Maybe this relates again to the files it permits to duplicate.
M
Mani Malekizadeh

https://i.stack.imgur.com/EDELN.jpg


this answer is for VS Code, not Visual Studio
Not what was asked, but a good idea for an extension, really. And yes, I won't downvote this answer. Lol.
b
berserck

I came up with a hack that might produce the result intended in the original answer.

If you have the file you want in two windows in a source control, you can right-click on the file and select compare, you can compare the

If you do compare you will have a new window Called diff, showing you the contents of you file.

This is of course not ideal as the diff window will have the diff colors polluting the text. Note: you can compare the file you want to open to and empty file, and then you will have the window in a very ugly green background.

This is not perfect, it is a hack, but it was the only way I found to really have the same file in two windows.


E
EMT

As the question is too old, do not know answering this is like beating a dead horse to run or not. But if someone is using VSCode 1.62.1 or later, to see the same script in another tab just simply right click on the script name on the toolbar and select "split right" (or split up/down/left).


The question is about Visual Studio, not Visual Studio Code.
@David yes I agree with you. I missed it, I appologize.
There's no need to apologize! :^)