Smart Paste in Rider

Maarten Balliauw

Today, I’d like to introduce you to Smart Paste, a "little big feature" in Rider (as well as in ReSharper). It ensures the right escaping in string literals that are copy/pasted. For example, consider pasting The "Name" field is required into var error = "". We would paste the string, then move to both " characters and escape them with \", making sure there are no syntax errors. Rider helps us out here with a quick-fix that can escape illegal characters for us!

Using Smart Paste to escape a string

Smart paste works in C#, VB.NET, JavaScript, XML, XAML and HTML files, and suggests escaping values upon paste.

Imagine how much time smart paste can save us in some cases! E.g. when copy/pasting a large snippet of XML or JSON that has quotes all over the place… Let’s try pasting some JSON data into a (verbatim) string:

Smart paste JSON verbatim string

Rider also detects other characters that may need escaping, for example newline characters. If we paste into a regular string, smart paste will escape quotes and also add \n characters:

Smart paste JSON string

Want to paste a file path? Rider has us covered there as well – it will make sure we are escaping the \ character:

Escape file path with smart paste

Smart paste really comes in handy when writing XML documentation. Rider will escape any characters that need special treatment in an XML context, for example generics, lambda syntax, logical operators, …

Smart paste code in XML doc comments

One more. Combine with the To LINQ to XML / To LINQ to JSON quick-fixes, we can paste XML or JSON string inside a string literal and then easily convert it into C# code. XML will be transformed to an XML object (using XElement, XAttribute, …). And when Newtonsoft.Json is referenced, we can transform our string into a JSON.NET object:

Convert string to JObject using Rider

Download the latest Rider EAP build and try it out! We’d love to hear your thoughts and feedback!

Comments below can no longer be edited.

9 Responses to Smart Paste in Rider

  1. Mike-EEE says:

    March 15, 2017

    Looks a lot faster and more responsive than R#, too. 😉

    • Maarten Balliauw says:

      March 15, 2017

      Same R# engine underneath, must be on the UI side 🙂

      • Mike-EEE says:

        March 15, 2017

        Haha host, you mean. B)

    • trydis says:

      March 16, 2017

      I don’t see a big difference tbh:

  2. Dew Drop - March 16, 2017 (#2441) - Morning Dew says:

    March 16, 2017

    […] Smart Paste in Rider (Maarten Balliauw) […]

  3. Frontend-Editing in TYPO3 & Tipps für benutzerfreundliches Webdesign says:

    March 20, 2017

    […] Wer sich in Rider hier oder da mit Copy & Paste behilft, um die eine oder andere Eingabe abzukürzen, gerät ganz schnell mal in die Situation, nicht erlaubte Zeichen mit zu kopieren. Vergisst man dann diese Zeichen zu bearbeiten, entsteht ein Syntax Error. Viel Spaß beim Debuggen! Allerdings muss das nicht sein. Mit Smart Paste bietet Rider für Dateien in den Sprachen und Formaten C#, VB.NET, JavaScript, XML, XAML und HTML eine Lösung an: Smart Paste bietet automatisch Escaping-Values für solche Fälle an. Wie das funktioniert und sogar genutzt werden kann, um XML- oder JSON-Strings in C#-Code zu konvertieren, zeigt Maarten Balliauw. […]

  4. Thomas Kleist says:

    September 16, 2017

    Is there any feature like xml to c# like in visual studio in would be very nice

  5. Jose says:

    May 1, 2018

    It is not working.

    I paste as in the examples but nothing popping out.



Subscribe to .NET Tools updates