Select Page


Find and Replace is a decent feature for any text editor that supports it. Besides allowing you to search for a given string of text in a file, it even allows you to insert some other text in place of it. The replace function not only replaces a single instance of the found string, but it can also be applied across the entire file in every found instance of the searched text. How cool is that, huh?

In the code editor called Sublime Text, there is the ability to search for Regular expressions besides plain strings of text. There are a bunch of cool possibilities you can pull off using this feature, but what we’re interested in for now is the ability to replace anything you search with an actual newline escape sequence, and vice versa. But why would you want to do that in the first place?

Why would I want to replace stuff with newlines?

A good question. 

Answer? “Depends”.

For example, if you’ve written out a list of some words like this:

And would like them to be in lines like this:

You can do that using the replace with newline method. But don’t get me wrong, there are actually a lot of useful scenarios where you may wish to use this technique. A more common use case is to replace a typed out ‘\n’ with a newline in code files, as this is where most of usability is visible.

Replace anything with a newline

You can choose to replace just about anything with a newline in Sublime Text, and it’s really easy to use as long as you can remember the steps! What are you waiting for? Fire up Sublime Text and press Command + Shift + F (in Mac) or Ctrl + Shift + H in (Windows and Linux) to enable the Find and Replace bar on the bottom of the editor window:

Make sure to enable Regular Expressions by clicking on the .* button on the left (it is enabled if it has a gray background). Now what you’d want to do, inside the text-input box beside Find, is to search for the characters (or any form of text) that you’d like to replace with a newline. In the example above, I’d like to replace the ‘, ‘ (Comma that has a space after it), with a newline. So this is what I’d search:

Right off the bat if you click on the Find button, you’d be taken to a new tab with the results. However, this is not what we want, so don’t click on Find for now. After you make sure that you’ve typed in the right character, type out “\n” inside the text-input box beside Replace:

Click on Replace on the bottom right:

Under the confirmation window where it shows you the number of instances found, click on Replace:

And wala! All the instances of our found text has been replaced with a newline!

Replacing instances of \n with a newline

As I had already mentioned, a more general use case for this method is to replace every instance of \n found in a file with an actual newline. If you try using your inner-genius instincts to try the same process as previous and search for \n beside Find and try to replace it like this:

You’d get back this:

So don’t try it out that way. What you need to do, is type in an extra backslash (\) before the \n you insert in the text-input box beside Find (only), like this:

Notice how the ‘n’ turns from a purple color to white. This depicts that it is no longer interpreted as an escape sequence, but rather, as a regular string you searched for.

Now if you try clicking on Replace, and the confirm that you want to replace it:

You’d get the \n(s) to be turned into actual newlines, just as expected:

Don’t believe it actually worked or if I did it myself? We’ll here’s a gif for that too:

Replace newlines with a comma

Just in case you asked for, it is also possible to replace an actual newline with something else. What this will do is join the lines together, but divide each of them using whatever you replaced the newlines with. It doesn’t just have to be a comma, but it can simply be anything else. For example, if you want to replace each newline with a tab, you can simply use ‘\t’ and make it happen!

If we go back to our ‘apple, banana’ example, and wish to turn each words with separate lines into a single line with the words divided by commas, this is what I’ll do:

Simply interchange the contents of the text-input boxes from this:

to this:

As always, click on the Replace button on the bottom right, confirm, and you get back this:

I wouldn’t mind if you thank me later, though! For now, how about you try replacing the newlines with a tab instead?