visual-studio-codenewline

how do I display \n as new line in text file?


How do I display \n as new line in text file or markdown file? I am on OSX using vscode

from what I read in this StackOverflow question I understand that \n should just display as new line in text editors? I have a terminal output that have \n that I would like to replace as new lines so that when added to github the markdown content would render correctly

the raw output I am saving to a text file is

\nsequenceDiagram\n    participant ABOUT_ME_APP as \"AboutMeApp\"\n    

after formatting it should look like

sequenceDiagram
participant Application as \"Application.java\" 
...

this is how I am manually replacing the \n

I can get to work if I replace \n with new line or even on windows laptop using notepad++ replace \n with same \n and it will display the newline correctly.

My understanding is in the raw text file new line is stored as \n or \r\n internally anyway? and displays as new line when opened in text editor, is that correct?

What am I missing? why is \n not being displayed as newline when saved to a text file?


Solution

  • My understanding is in the raw text file new line is stored as \n or \r\n internally anyway?

    Although people will say things like "this is a raw text file" or "this is an HTML file" they don't really exist. There are simply files which are a bunch of numbers. We decide how to treat their contents. Often this is done using the file extension, like .txt or .html, but that's just a convention.

    Like everything, characters are stored as numbers. Exactly which number depends on the character encoding, a way we decide which numbers represent which characters. The most common character encoding these days is UTF-8 where a newline is 10 or 0a in hex. When you tell VS Code that a file represents UTF-8 encoded text (which is typically the default) then it knows to display the number 10 as a newline, and the number 97 as a.

    If you want to save a text file with newline characters in it, use newline characters. Same goes for Markdown, Markdown doesn't do anything special with \n.


    \n is literally a \ and an n. It's how a newline character is typically represented when we want it to be visible. To turn it into a newline it has to be run through some sort of formatter to turn it into a newline character.

    For example, most programming languages will treat \n in a string as a newline. For example, in Ruby.

    print "These\nare\nturned\ninto\nnewlines\n"
    
    These
    are
    turned
    into
    newlines