KCommands KCommands

The free KCommands extension provides a keyboard accessible command line for Visual Studio 2017/2019 to quickly find and replace text similar to SlickEdit and Vim:

KCommands in Visual Studio 2019

To open the command line assign a shortcut to the KCommands.Open command in Visual Studio options:

Keyboard options

Two commands are available to find and replace text:

f /text/options
r /text/new text/options

Search options:

i - ignore case,
b - search backwards,
w - match whole word,
r - use .NET regular expressions.

Target options:

s - selection,
d - document (default),
o - open documents,
p - project,
* - solution.

UI options:

t - wait for search to complete,
0 - don't show results,
1 - list results in Find results 1 window,
2 - list results in Find results 2 window.

KCommands uses the same options as in the Visual Studio Find and Replace dialog:

VS Find and Replace dialog

Example commands:

f /bool            - find next "bool" in the current document with default options (match case)
f /bool/iw         - find next "bool" ignoring case and matching whole word
f/bool             - the space after the command name can be omitted
f /BOOL/w*         - find all usages of "BOOL" in the solution
r /BOOL/bool/w     - replace all "BOOL" with "bool" in the current document 
r ./.\             - use another character (in this case . instead of /) as a command separator to replace slash with backslash

Pressing Enter in the command line executes the command. Pressing Esc closes the command line (you can also use a custom shortcut to the KCommands.Close command). Pressing Tab switches to the VS text editor. Pressing Up and Down arrows scrolls commands history. If the command has ? as the first or the only character, executing it opens this page as documentation.

Pressing Ctrl+Enter executes the command and closes the command line. If you frequently use this key combination, try the KCommands.OpenTransient command - it opens the command line and then automatically closes after a command execution on Enter. If you press Ctrl+Enter in a transient command line - it will execute the command and keep the command line open.

KCommands.Open supports a parameter to prefill the command line. With Visual Commander you can create a command to prefill e.g. with "f /" and assign a keyboard shortcut to quickly find text:

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
        DTE.ExecuteCommand("KCommands.Open""f /");
}

Plus with Visual Commander you can add the current word or selected text to the command:

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
    EnvDTE.TextSelection ts = DTE.ActiveDocument.Selection as EnvDTE.TextSelection;
    DTE.ExecuteCommand("KCommands.Open""f /" + ts.Text);
}

KCommands.Execute also supports a command parameter. With Visual Commander you can create complete ready to use commands for frequently used queries and create batch replacements:

public void Run(EnvDTE80.DTE2 DTE, Microsoft.VisualStudio.Shell.Package package)
{
    DTE.ExecuteCommand("KCommands.Execute""r /RecipeCollection/RecipeAsDictionary/*t0");
    DTE.ExecuteCommand("KCommands.Execute""r /RecipeItem/RecipeValue/*t0");
    DTE.ExecuteCommand("KCommands.Execute""r /Customer/CustomerInvoice/*");
}

KCommands v1.3.4 - September 11, 2019. Download Now

 

Organize Visual Studio tabs with Tabs Studio add-in