How to convert new line tags to actual new lines

New line tags are sequences of characters used in certain context to signal that the text that follows them is to be displayed on the following line (obviously). Computers are generally pretty good at converting them, but our poor brains have a hard time parsing them and giving us a mental representation of how the final text will look like on the screen. Whenever I need to work with them, I find it easier to just convert them to actual new lines and then doing the opposite process when I need to put the text back where it was meant to belong. Here’s how to do that.

Converting HTML line breaks

HTML uses the <br/> tag to signal line breaks. The web browser interprets it as a new line character and replaces it accordingly. This is what Shakespeare’s Sonnet 18 looks like with <br/> tags in pace of new lines.

Shall I compare thee to a summer's day?<br/>Thou art more lovely and more temperate.<br/>Rough winds do shake the darling buds of May,<br/>And summer's lease hath all too short a date.<br/>Sometime too hot the eye of heaven shines,<br/>And often is his gold complexion dimmed;<br/>And every fair from fair sometime declines,<br/>By chance, or nature's changing course, untrimmed;<br/>But thy eternal summer shall not fade,<br/>Nor lose possession of that fair thou ow'st,<br/>Nor shall death brag thou wand'rest in his shade,<br/>When in eternal lines to Time thou grow'st.<br/> So long as men can breathe, or eyes can see,<br/> So long lives this, and this gives life to thee.

I don’t know about you, but I don’t find this very readable at all. We can, however, easily convert it using an open source application called Notepad++. I mentioned this program a few times already, and with good reason: it’s hands-down the best text editor for Windows, period. Compared to regular Notepad, Notepad++ has a lot of features that help coders. A very powerful Search and Replace functionality is one of them.

To convert HTML line breaks, we will need to open the Search and Replace pop-up by pressing Ctrl + H.

We want Notepad++ to use the Extended search mode. In Find what we’ll write <br/> and in Replace with we’ll write \n, as shown in the screenshot above. Then we only need to click Replace All to have the program convert new lines for us.

Converting new line escape sequences

Most programming languages use \n as a special escape sequence to signal a new line. Shakespeare’s Sonnet 18 with \n escape sequences looks like this:

Shall I compare thee to a summer's day?\nThou art more lovely and more temperate.\nRough winds do shake the darling buds of May,\nAnd summer's lease hath all too short a date.\nSometime too hot the eye of heaven shines,\nAnd often is his gold complexion dimmed;\nAnd every fair from fair sometime declines,\nBy chance, or nature's changing course, untrimmed;\nBut thy eternal summer shall not fade,\nNor lose possession of that fair thou ow'st,\nNor shall death brag thou wand'rest in his shade,\nWhen in eternal lines to Time thou grow'st.\n So long as men can breathe, or eyes can see,\n So long lives this, and this gives life to thee.

The process to convert these escape sequences is similar to that described above, but with a key difference. We cannot replace \n with \n because in actuality there are no \n character, that is, there are no actual line breaks. The Extended search mode treats \n as an actual line feed, so we will need to escape the escape sequence to tell the program that it should look for backslash character followed by a letter n.

The screenshot below shows how to do it:

As before, clicking Replace All will perform the replacement operation.

And here’s a comparison between before and after:

Going the other way around

If you want to convert line breaks into HTML tags or new line escape sequences, you’ll need to know how your lines actually end. Earlier in this article I wrote that most programming languages use \n as an escape sequence for a line break character. In actuality, that’s an oversimplification. Back in the early days of computing, video displays didn’t exist and the output of programmes was printed
on paper using a teletypewriter. These teletypes required two commands to start a new line: one, called line feed, moved the paper up one line and one, named carriage return, moved the carriage to the original position. This convention stuck and text files generally use feature both characters. To know if your document does, you will need to enable Show End of Line under View > Show Symbol.

Our file contains both escape sequences, as shown below.

So, if we want to convert escape sequences to text, we will need to insert both characters in the Search and Replace window. Notice how we’re escaping the backslash character in theĀ Replace with field.

This screenshot shows how to convert new lines to <br/> HTML tags.

About Andrea Luciano Damico 126 Articles
Andrea Luciano Damico is a freelance translator from Italy. Among his interests are linguistics, technology, video games, and generally being a chill guy. He runs Let's Translate.it and Tech4Freelancers.net.