Craftsmanship, Old Website, Testing

ReSharper Live Templates

March 21, 2017

I will admit it. I am lazy. Which isn’t necessarily a bad thing as a developer, so I am not losing much sleep over it. When I first began doing code katas several years ago, I made it a point to get more efficient and find shortcuts within the tooling. This meant hotkeys and code snippets within Visual Studio, useful extensions, and the various shortcuts that JetBrains’ ReSharper gives you to help improve productivity. At the time, I wrote an article on ReSharper Live Templates but with the recent updates to ReSharper and Visual Studio, I wanted to revisit and give it a refresh.

What Are Live Templates?

You are probably familiar with code snippets in Visual Studio. ReSharper’s Live Templates are similar, but much more powerful and intelligent. They can be incredibly helpful when you find yourself applying similar patterns in your code repeatedly. In my case, the repeated ceremony of creating unit tests drove me to use Live Templates. Instead of writing the same few lines of code, I wanted to generate that simply tab through the necessary changes, similar to what you are used to doing with foreach loops.

How To Create Them

Step 1: Navigate to the ReSharper Menu -> Tools -> Templates Explorer to open the explorer. This is where you can create and edit your templates. Click the new template button…

Resharper Live Template Explorer

Step 2: A new window/tab will open up for you to enter your code template in with various options. You are entering the blueprint of your code snippet and identifying the pieces that will be replaced, and the order. In my case, I will have a unit test stub that allows me to define the method, scenario, and behavior. You should make the names something descriptive for readability, but in this case they are only going to be placeholders.

Step 3: Next wrap each editable portion of the template in ‘$’ symbols. This will identify them as parameters. These are the fields that you will be prompted to replace as you tab through. You can adjust the order you enter them in, as well as apply macros to make them smarter. For now we will keep it simple:

Creating our ReSharper Live Template

Step 4: Add “$END$” to the spot where you want the cursor to end up at. Make any other changes and save. Check out ReSharper’s documentation for more information on the different settings.


Now return to your code and type the first few letters of your shortcut and your template should show up in the intellisense window. Tab through or select it and it will insert your code template and highlight any fields you wanted to replace.

ReSharper Live Template in action

Pretty simple, but incredibly useful when you are doing the same thing over and over. ReSharper is an amazing tool for .NET development and one that is worth investing some time to learn the different features. It is the little things like this that save time and let you focus on the real work. This was a pretty simple and straightforward example, but I have found myself using templates quite a bit in my day to day work.

You Might Also Like

Leave a Reply