Visual Studio immediate window command for Clear All
Asked Answered
B

8

122

Is there a command to clear the immediate window in Visual Studio?

I hate having to grab the mouse for a right click menu there - would rather just type "cls" or something.

Bodhisattva answered 3/4, 2009 at 15:39 Comment(1)
didn't realise there was a right-click menu!Skitter
E
161

To clear the immediate window, you can use >cls, which is a predefined command alias to >Edit.ClearAll.

The MSDN article lists all predefined aliases and you can define your own, too. (For VS 2010 and earlier, custom aliases are described in a separate article, though.) Scanning through, there's a whole slew of them, some of which might even have their roots in MS-DOS DEBUG.EXE (specifically >d, >g, >p, >q, and >t come to mind).


Also worth noting as it's only two keys to press: Context menu > Clear All invokes the same command and it can be navigated using keyboard. Therefore in the immediate window, you can press Context Menu, L.

If you don't have a context-menu key on your keyboard (you know, the one between Right Alt and Right Ctrl), you can use Shift+F10 instead.

Envy answered 3/4, 2009 at 15:45 Comment(2)
i normally use the context key when I'm in the office - but i've been working on a laptop for the past few months from a clients office, and I don't have the context key on it. It finally bothered me enough to look for a solution...Bodhisattva
The > is important. Could not think of a natural way to make it more explicit in the answer. Also, see Immediate Window on MSDN for more useful tips.Thibeault
I
52
>cls 

seems to do it for me.

Ingmar answered 3/4, 2009 at 15:53 Comment(0)
C
23
  1. Place the mouse cursor in the Immediate Window.
  2. Right click on the mouse and select "Clear All".
Cuddle answered 17/9, 2015 at 14:18 Comment(0)
B
14

found it...

">Edit.ClearAll"

or

">cls"

Bodhisattva answered 3/4, 2009 at 15:42 Comment(0)
M
3

Here is how to do it at run time:

  1. Reference the EnvDTE dlls in your application.

  2. Create and then use this function as necessary.

Public Sub ClearImmediateWindow()
  Try
    Dim vsWindowKindImmediateWindow As String _ 
          = "{ECB7191A-597B-41F5-9843-03A4CF275DDE}"
    Try
      Dim obj As Object = System.Runtime.InteropServices.Marshal._ 
                          GetActiveObject("VisualStudio.DTE.10.0")
      If obj IsNot Nothing Then
        Dim DTE2 As EnvDTE80.DTE2 = CType(obj, EnvDTE80.DTE2)
        For Each wndw As EnvDTE.Window In DTE2.Windows
          If wndw.ObjectKind = vsWindowKindImmediateWindow Then
            wndw.Activate()
            DTE2.ExecuteCommand("Edit.ClearAll")
            Exit For
          End If
        Next
      End If
    Catch comEx As COMException
      ' Not running from within the VS IDE?
    Catch ex As Exception
      Throw ex
    End Try
  Catch ex As Exception
    ' Handle this as you desire.
  End Try
End Sub
  End Sub
Manus answered 13/4, 2012 at 3:19 Comment(2)
Not working for me. Error is: "A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll" while getting object from Marshal.Lacerta
My visual studio version is 2012 so I changed VisualStudio.DTE.10.0 to VisualStudio.DTE.11.0 and it worked. Also here I found a shorter syntax of same answer: https://mcmap.net/q/182539/-is-it-possible-to-programmatically-clear-the-output-window-in-visual-studioLacerta
L
1

For visual studio 2012 I use:

Public Sub ClearImmediateWindow()
    Dim dte As EnvDTE80.DTE2 = Marshal.GetActiveObject("VisualStudio.DTE.11.0")
    dte.Windows.Item("Immediate Window").Activate() 'Activate Immediate Window  
    dte.ExecuteCommand("Edit.SelectAll")
    dte.ExecuteCommand("Edit.ClearAll")
    Marshal.ReleaseComObject(dte)
End Sub

to automatically clear immediate window from codes(requires to add DTE references to project). If it not works try VisualStudio.DTE.8.0, VisualStudio.DTE.9.0, ... according to your visual studio version.

Lacerta answered 13/2, 2016 at 0:59 Comment(0)
S
1

I used the last answer just about verbatim and it works, although I wanted the focus back on where it was. Here's the very slightly improved C# version. I enable it with a configuration switch.

#if DEBUG
    if (GetIni("Debug", "ClearImmediateWindow", true)) {
        try {
            var dte = (EnvDTE.DTE) Marshal.GetActiveObject("VisualStudio.DTE.15.0");
            var me  = dte.ActiveWindow;
            dte.Windows.Item("Immediate Window").Activate();
            dte.ExecuteCommand("Edit.ClearAll");
            me.Activate();
        }
        catch { /* Meh! */ }
#endif
Sieber answered 22/7, 2018 at 21:7 Comment(3)
Just a gotcha; if you have more than one instance of VS open, Murphy's law dictates that it will clear the other one and leave you trying to figure out why the F^&$%**! it doesn't work. At least that's what I've been doing for the last 20 minutes.Jelene
And now it works, but I think only if VS is the active window. If another window (for example the project I'm debugging) is active when this code is executed, I get "Exception from HRESULT: 0x80010001 (RPC_E_CALL_REJECTED)".Jelene
You're right. If you're running 2 instances, you'd have to work out a rational way to tell one from the other. I imagine you would have to iterate all the main windows and check for the one with the highest ZOrder. I quit using this some time ago because I came up with a better way. I wrote this because Visual Studio steals Debug message. For all other tools I run them through DbgView (SysInternals). I wrote a back end process that captures them before Visual Studio steals them, and forwards them to DbgView, so I got back to what I wanted in the first place.Sieber
S
0

Those who are wondering how to clear immediate window on Visual Studio Mac, just right click inside the window and select the option Clear

Sigismondo answered 7/1, 2021 at 22:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.