Search code examples
visual-studioshorthand

Custom shorthand Visual Studio 2017


Is there a way to create custom shorthand's in Visual Studio 2017. Just like ctor or prop?

I would like to create a shorthand for unit-tests, to just type in for instance test and then having a predefined function to edit like:

public void Method_Condition_Expected()
{
   // Arrange

   // Act

   // Assert
}

Solution

  • As @Peter B has suggested for Visual Studio itself you can do it with Snippet Designer (Tools->Code Snippets Manager).

    I found out right now, that my installed Tool ReSharper (2016.3) has an option to create so-called Live-Template's.

    With that, I can create a Template like this:

    public void $Method$_$Condition$_$Expected$()
    {
        // Arrange
    
        // Act
    
        // Assert
    }
    

    The nice part is now, that i can tab through Method,Condition and Expected and overwrite the values.

    How to use: Mark a Code-Area such like the code Above in Visual Studio then click on Resharper->Tools->Create Live Template from Selection->Create and Edit.

    Now wrap code-areas for tabing through with $ for instance like $Method$ and give a shortcut name such like test.

    I hope this helps somebody else as well.

    For a closer Comparision between Visual Studio Snippet Manager and Resharper Live Templates see Difference between Live Template and Visual Studio Snippet