c#

Replace a string with newline in emacs

I sometimes find myself wanting to replace certain strings in a text file with a new-line character. Not with “\n” or “\r\n” mind you, but an actual break to make the text more readable in the editor. Take for example a random stack trace from Visual Studio. I sometimes copy these out to another simpler text editor to examine them closer. The problem is that when pasted, they will appear as a single line with “\r\n” instead of actual line breaks:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)\r\n at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()\r\n at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at System.Windows.Forms.AxHost.CreateHandle()\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at MyApplication.frmPrint.InitializeComponent() in (...)

If your text editor breaks lines at the edge of the window, it might look more like this:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& 
clsid, Object punkOuter, Int32 context, Guid& iid)\r\n at 
System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid 
clsid)\r\n at System.Windows.Forms.AxHost.CreateInstanceCore(Guid 
clsid)\r\n atSystem.Windows.Forms.AxHost.CreateInstance at 
System.Windows.Forms.AxHost.GetOcxCreate()\r\n at 
System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)\r\n at 
System.Windows.Forms.AxHost.CreateHandle()\r\n at 
System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.Control.CreateControl(Boolean 
fIgnoreVisible)\r\n at System.Windows.Forms.AxHost.EndInit()\r\n at 
MyApplication.frmPrint.InitializeComponent() in (...)

..which may be better, but still not very readable.

Most text-editors have some form of replace-all utility built in, but it’s not always easy to replace “\r\n” with an actual line shift (try it!). In Emacs, there is a nice way to handle this:

Use M-x Replace-String to prompt for the string to replace. Enter “\r\n” in the mini-buffer. Press enter, and it will prompt for the string to replace it with. At this point, use C-q C-j to input a new-line (C-j) as quoted text (C-q), and press enter again.

The result will be a much more readable stack trace:

at System.Windows.Forms.UnsafeNativeMethods.CoCreateInstance(Guid& clsid, Object punkOuter, Int32 context, Guid& iid)
at System.Windows.Forms.AxHost.CreateWithLicense(String license, Guid clsid)
at System.Windows.Forms.AxHost.CreateInstanceCore(Guid clsid)
at System.Windows.Forms.AxHost.CreateInstance at System.Windows.Forms.AxHost.GetOcxCreate()
at System.Windows.Forms.AxHost.TransitionUpTo(Int32 state)
at System.Windows.Forms.AxHost.CreateHandle()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.AxHost.EndInit()
at MyApplication.frmPrint.InitializeComponent() in (...)

Viewing Linqpad scripts in VS2013

I use LINQPad all the time at, both for experimenting with C#, and for building and testing Linq queries before I import them in our projects.

As an example, I recently wrote a few scripts for fetching some data from a database, and reformatting it into JSON data which we will use as dummy-data for testing. This will not be a part of our finished project, but it’s still nice to have the script visible in Visual Studio, even if I only run it through LINQPad .

The following are a couple of tips I’ve found useful when working with .linq-files in or from within Visual Studio.

Continue reading