Question

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.

Was it helpful?

Solution

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. 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.

OTHER TIPS

>cls 

seems to do it for me.

  1. Place the mouse cursor in the Immediate Window.
  2. Right click on the mouse and select "Clear All".

found it...

">Edit.ClearAll"

or

">cls"

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

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top