121

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118

8 Answers8

160

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.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
lc.
  • 113,939
  • 20
  • 158
  • 187
  • 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... – Scott Ivey Apr 03 '09 at 15:56
  • 2
    The `>` is important. Could not think of a natural way to make it more explicit in the answer. Also, see [Immediate Window](https://msdn.microsoft.com/en-us/library/f177hahy.aspx) on MSDN for more useful tips. – Palec Apr 03 '17 at 09:27
52
>cls 

seems to do it for me.

Martin Brown
  • 24,692
  • 14
  • 77
  • 122
22
  1. Place the mouse cursor in the Immediate Window.
  2. Right click on the mouse and select "Clear All".
rossco78
  • 231
  • 2
  • 8
14

found it...

">Edit.ClearAll"

or

">cls"

Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
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
Tisho
  • 8,320
  • 6
  • 44
  • 52
  • 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. – Mojtaba Rezaeian Feb 12 '16 at 22:29
  • 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: [http://stackoverflow.com/a/16873888/2721611](http://stackoverflow.com/a/16873888/2721611) – Mojtaba Rezaeian Feb 13 '16 at 00:31
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.

Mojtaba Rezaeian
  • 8,268
  • 8
  • 31
  • 54
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
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
Wade Hatler
  • 1,785
  • 20
  • 18
  • 1
    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. – Luc VdV Sep 19 '19 at 12:25
  • 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)". – Luc VdV Sep 19 '19 at 12:35
  • 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. – Wade Hatler Sep 20 '19 at 14:05
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

Renji
  • 123
  • 8