Questions tagged [newline]
Newline refers to a character sequence representing a line break in text (also known as End of Line, EOL, or Line Break). Different operating systems have different sequences for newlines.
4,800
questions
3080votes
20answers
2.8mviews
Echo newline in Bash prints literal \n
In Bash, tried this:
echo -e "Hello,\nWorld!"
But it doesn't print a newline, only \n. How can I make it print the newline?
I'm using Ubuntu 11.04 (Natty Narwhal).
2240votes
11answers
933kviews
How to replace a character by a newline in Vim
I'm trying to replace each , in the current file by a new line:
:%s/,/\n/g
But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.
What should I ...
2129votes
9answers
1.1mviews
How do I break a string in YAML over multiple lines?
In YAML, I have a string that's very long. I want to keep this within the 80-column (or so) view of my editor, so I'd like to break the string. What's the syntax for this?
In other words, I have this:...
1914votes
27answers
2.2mviews
How can I remove a trailing newline?
What is the Python equivalent of Perl's chomp function, which removes the last character of a string if it is a newline?
1781votes
19answers
308kviews
Why should text files end with a newline?
I assume everyone here is familiar with the adage that all text files should end with a newline. I've known of this "rule" for years but I've always wondered — why?
1069votes
23answers
832kviews
git replacing LF with CRLF
Running git on a Windows XP machine, using bash. I exported my project from SVN, and then cloned a bare repository.
I then pasted the export into the bare repositories directory, and did a:
git add -...
855votes
26answers
2.0mviews
Writing a list to a file with Python
Is this the cleanest way to write a list to a file, since writelines() doesn't insert newline characters?
file.writelines(["%s\n" % item for item in list])
It seems like there would be a standard ...
730votes
23answers
1.1mviews
How can I echo a newline in a batch file?
How can you you insert a newline from your batch file output?
I want to do something like:
echo hello\nworld
Which would output:
hello
world
726votes
11answers
551kviews
Difference between \n and \r?
What’s the difference between \n (newline) and \r (carriage return)?
In particular, are there any practical differences between \n and \r? Are there places where one should be used instead of the ...
652votes
9answers
233kviews
What's the strategy for handling CRLF (carriage return, line feed) with Git?
I tried committing files with CRLF-ending lines, but it failed.
I spent a whole work day on my Windows computer trying different strategies and was almost drawn to stop trying to use Git and instead ...
605votes
13answers
595kviews
What are carriage return, linefeed, and form feed?
What is the meaning of the following control characters:
Carriage return
Line feed
Form feed
599votes
11answers
402kviews
git-diff to ignore ^M
In a project where some of the files contains ^M as newline separators. Diffing these files are apparently impossible, since git-diff sees it as the entire file is just a single line.
How does one ...
571votes
9answers
444kviews
How do I get a platform-dependent new line character?
How do I get a platform-dependent newline in Java? I can’t use "\n" everywhere.
525votes
35answers
669kviews
How can I insert a line break into a <Text> component in React Native?
I want to insert a new line (like \r\n, <br />) in a Text component in React Native.
If I have:
<text>
<br />
Hi~<br />
this is a test message.<br />
</text>
Then ...
479votes
15answers
1.3mviews
What is the JavaScript string newline character?
Is \n the universal newline character sequence in JavaScript for all platforms? If not, how do I determine the character for the current environment?
I'm not asking about the HTML newline element (<...
465votes
13answers
667kviews
Replace a newline in TSQL
I would like to replace (or remove) a newline character in a TSQL-string.
Any Ideas?
The obvious
REPLACE(@string, CHAR(13), '')
just won't do it...
442votes
15answers
587kviews
./configure : /bin/sh^M : bad interpreter [duplicate]
I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error
[root@localhost lpng142]# ./configure
bash: ./configure: /bin/sh^M: bad interpreter: No such file ...
437votes
21answers
633kviews
Split Java String by New Line
I'm trying to split text in a JTextArea using a regex to split the String by \n However, this does not work and I also tried by \r\n|\r|n and many other combination of regexes.
Code:
public void ...
397votes
22answers
536kviews
How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script
How can I programmatically (i.e., not using vi) convert DOS/Windows newlines to Unix?
The dos2unix and unix2dos commands are not available on certain systems. How can I emulate these with commands ...
396votes
11answers
418kviews
How to find out line-endings in a text file?
I'm trying to use something in bash to show me the line endings in a file printed rather than interpreted. The file is a dump from SSIS/SQL Server being read in by a Linux machine for processing.
Are ...
393votes
14answers
332kviews
Pass a PHP string to a JavaScript variable (and escape newlines) [duplicate]
What is the easiest way to encode a PHP string for output to a JavaScript variable?
I have a PHP string which includes quotes and newlines. I need the contents of this string to be put into a ...
373votes
10answers
681kviews
How do I handle newlines in JSON?
I've generated some JSON and I'm trying to pull it into an object in JavaScript. I keep getting errors. Here's what I have:
var data = '{"count" : 1, "stack" : "sometext\n\n"}';
var dataObj = eval('('...
355votes
17answers
954kviews
Writing string to a file on a new line every time
I want to append a newline to my string every time I call file.write(). What's the easiest way to do this in Python?
345votes
17answers
424kviews
'\r': command not found - .bashrc / .bash_profile [duplicate]
I have windows, using Cygwin, trying to set JAVA_HOME permanently through my .bashrc file.
.bashrc:
export PATH="$JAVA_HOME/bin:$PATH"
export JAVA_HOME=$JAVA_HOME:"/cygdrive/c/Program Files (x86)/...
315votes
7answers
196kviews
Render a string in HTML and preserve spaces and linebreaks
I have an MVC3 app that has a details page. As part of that I have a description (retrieved from a db) that has spaces and new lines. When it is rendered the new lines and spaces are ignored by the ...
310votes
14answers
454kviews
Removing trailing newline character from fgets() input
I am trying to get some data from the user and send it to another function in gcc. The code is something like this.
printf("Enter your Name: ");
if (!(fgets(Name, sizeof Name, stdin) != NULL)) {
...
310votes
13answers
120kviews
How to stop VIM from adding a newline at end of file?
So I work in a PHP shop, and we all use different editors, and we all have to work on Windows. I use vim, and everyone in the shop keeps complaining that whenever I edit a file there is a newline at ...
304votes
6answers
243kviews
CSV in Python adding an extra carriage return, on Windows
import csv
with open('test.csv', 'w') as outfile:
writer = csv.writer(outfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
writer.writerow(['hi', 'dude'])
writer.writerow(['hi2', 'dude2'])
...
287votes
17answers
623kviews
How to remove line breaks from a file in Java?
How can I replace all line breaks from a string in Java in such a way that will work on Windows and Linux (ie no OS specific problems of carriage return/line feed/new line etc.)?
I've tried (note ...
281votes
21answers
291kviews
How to add line break for UILabel?
Let see that I have a string look like this:
NSString *longStr = @"AAAAA\nBBBBB\nCCCCC";
How do I make it so that the UILabel display the message like this
AAAAA
BBBBB
CCCCC
I don't ...
280votes
8answers
74kviews
Trying to fix line-endings with git filter-branch, but having no luck
I have been bitten by the Windows/Linux line-ending issue with git. It seems, via GitHub, MSysGit, and other sources, that the best solution is to have your local repos set to use linux-style line ...
268votes
9answers
270kviews
Bash: Strip trailing linebreak from output
When I execute commands in Bash (or to be specific, wc -l < log.txt), the output contains a linebreak after it. How do I get rid of it?
268votes
4answers
315kviews
What is the difference between a "line feed" and a "carriage return"?
If there are two keywords then they must have their own meanings. So I want to know what makes them different and what their code is.
265votes
31answers
396kviews
How do I add a newline to a TextView in Android?
When I define a TextView in xml, how do I add a new line to it? \n seems not to work.
<TextView
android:id="@+id/txtTitlevalue"
android:text="Line1 -\nLine2"
android:...
263votes
20answers
289kviews
Windows batch: echo without new line
What is the Windows batch equivalent of the Linux shell command echo -n which suppresses the newline at the end of the output?
The idea is to write on the same line inside a loop.
249votes
8answers
122kviews
How line ending conversions work with git core.autocrlf between different operating systems
I've read a lot of different questions and answers on Stack Overflow as well as git documentation on how the core.autocrlf setting works.
This is my understanding from what I've read:
Unix and Mac OSX ...
214votes
8answers
350kviews
See line breaks and carriage returns in editor
Is there a text editor on Linux that allows me to see line breaks and carriage returns? Does Vim support this feature?
211votes
16answers
311kviews
Remove blank lines with grep
I tried grep -v '^$' in Linux and that didn't work. This file came from a Windows file system.
208votes
27answers
268kviews
Add line break within tooltips
How can line breaks be added within a HTML tooltip?
I tried using <br/> and \n within the tooltip as follows:
<a href="#" title="Some long text <br/> Second line text \n Third line ...
204votes
21answers
201kviews
String Resource new line /n not possible?
It doesn't seem like it's possible to add a new line /n to an XML resource string. Is there another way of doing this?
199votes
6answers
209kviews
awk without printing newline
I want the variable sum/NR to be printed side-by-side in each iteration. How do we avoid awk from printing newline in each iteration ? In my code a newline is printed by default in each iteration
for ...
195votes
6answers
136kviews
How to use newline '\n' in f-string to format output in Python 3.6?
I would like to know how to format this case in a Pythonic way with f-strings:
names = ['Adam', 'Bob', 'Cyril']
text = f"Winners are:\n{'\n'.join(names)}"
print(text)
The problem is that '\' cannot ...
190votes
6answers
142kviews
Eclipse and Windows newlines
I had to move my Eclipse workspace from Linux to Windows when my desktop crashed. A week later I copy it back to Linux, code happily, commit to CVS. And alas, windows newlines have polluted many files,...
188votes
15answers
118kviews
What are these ^M's that keep showing up in my files in emacs?
So I think it may have to do with textmate, but we work in a small team and are having some issues with full-file conflicts of nearly identical files in git because each line of one branch has a ^M ...
184votes
4answers
185kviews
How to do a newline in output
How do I make \n actually work in my output? At the moment it just writes it all in 1 long block. Thanks for any help
Dir.chdir 'C:/Users/name/Music'
music = Dir['C:/Users/name/Music/*.{mp3, MP3}']
...
170votes
19answers
380kviews
Generating CSV file for Excel, how to have a newline inside a value
I need to generate a file for Excel, some of the values in this file contain multiple lines.
there's also non-English text in there, so the file has to be Unicode.
The file I'm generating now looks ...
153votes
18answers
1.0mviews
Java String new line
I have a string like
"I am a boy".
I would like to print it this way
"I
am
a
boy".
Can anybody help me?
148votes
4answers
140kviews
Stop Visual Studio from mixing line endings in files
When opening a text based file in Visual Studio 2010 it will then write my edits with CRLF instead of the line ending format of the original file. How can I stop VS from doing this? Any half decent ...
147votes
9answers
107kviews
How do you search for files containing DOS line endings (CRLF) with grep on Linux?
I want to search for files containing DOS line endings with grep on Linux. Something like this:
grep -IUr --color '\r\n' .
The above seems to match for literal rn which is not what is desired.
The ...
146votes
24answers
201kviews
Importing CSV with line breaks in Excel 2007
I'm working on a feature to export search results to a CSV file to be opened in Excel. One of the fields is a free-text field, which may contain line breaks, commas, quotations, etc. In order to ...