I can't believe I've never needed to do this before, but I was struggling with a dumb problem a little while ago. I had a file open in Emacs and it contained a bunch of key/value pairs separated by, what else, the web's favorite: &

To make this more readable I wanted to put each pair on its own line. So I needed to replace all instances of & with newline (or the equivalent of hitting the 'enter' key).

So I did what any emacs user would do:

M-x replace-string

I typed '&' and hit return. I was then prompted for the replacement string.


If I hit enter, it'll assume I want to replace it with nothing--essentially removing the '&' characters. That's not right.

But what can I use?

Here's what I tried:

  • \n
  • \\n
  • C-q [enter]
  • about four other things I've already forgotten

Nothing worked.

Then I tried a few searches that led me no closer to a solution. So I turned to our internal mailing list for Emacs users. [You know you have a lot of software engineers when you can justify an internal mailing list for Emacs. Or VIM. We've got one for them too.]

The answer, it turns out, is: C-q C-j.

That's right, Control-J is the way to tell emacs to stick in a newline. In retrospect that makes a lot of sense.

Now if I ever forget again, I can just search my own web site and hopefully find this in the future.

Posted by jzawodn at April 13, 2007 01:24 PM

Reader Comments
# drew said:

I imagine you tried c-q c-m too. If c-q c-j makes so much sense, can you tell me why c-q enter and c-q c-m don't work?

on April 13, 2007 01:42 PM
# Paul Brown said:

I always use M-% (with ! if I want to answer "y" to all of the prompts to make it behave like M-x replace-string), and with M-%, you can yank (C-y) content into the replace buffer. I believe that the expected C-q behavior works with M-% as well, in both the replacant and replacand.

on April 13, 2007 02:29 PM
# Ray said:

I once knew this method for entering linefeed but I had forgotten. In testing for this comment, it turns out is is good that I had forgotten because it doesn't always work.

The method fails on a DOS file that edited on Unix or Macintosh OS X. Under DOS, lines are terminated by cr/nl (Ox0D0x0A). Typing C-qC-j as in your example would lead to the ampersands being replaced by the empty string. There may be other possible situations where cross OS file edits would fail. *

Easier for me to remember, and works in all cases, is go to the end of an existing line, C-k to kill the existing end-of-line character and the C-y to yank that character into the second field of the replace.

* Funny story - I once worked with a really good set of operations people at a small (100 people) company. Operations had firm procedures about not deploying code that they didn't receive from QA. Anyway late night comes, dev wants to get something trivial out the door, they'd made a minor edit to a config file, nothing could break, client needs this asap, etc. QA is not available for reasons I can't recall. VP of Engineering says, "Deploy it." Operations says, "Not unless it comes from QA or someone above both of us (CEO)."

VP, pissed, gets the CEO on the phone, "Operations is holding us up. We need this done now. It's just a config file. Nothing can break. Client needs it."

CEO to operations, "Deploy."

Operations, "OK."


That cluster goes bye-bye. Nothing works. Application servers won't come up. Weird error messages referring to files that weren't changed.

Dev sends a replacement, "exactly" the same as the file that had been working earlier in the day. Same thing. App servers won't come up. Weird errors. Cluster down beyond service level agreements. Client doesn't get _any_ hits. Hours go by before operations finds the problem cr characters.

Turns out the config file had been edited on Windows and instead of being checked into version control had been sent through the mail. Checking into version control and checking out would have converted from DOS to Unix formats. The file ran for dev because they were using servers on Windows.

By not going through procedures dev had changed not only the one line that they knew about, but every line in the file.

on April 13, 2007 02:30 PM
# Jonah said:

You can also paste (yank) a newline out of your buffer into an interactive prompt.

on April 13, 2007 03:26 PM
# Alexandre Vassalotti said:

That's curious...

M-x replace-regex notices the user, with "Note: `\n' here doesn't match a newline; to do that, type C-q C-j instead", if you enter `\n' in the search regular expression (but not in the replacement regular expression). I wonder why Emacs does not the same thing with M-x replace-string. That would be surely easy to do.

on April 13, 2007 09:17 PM
# Panayotis Vryonis said:

To VIM users: you can type "special characters like newlines, CR, ESC, etc) in the command line by pressing Ctrl-V first.

Ctrl-V will then show up like "^I", Ctrl-V like "^M", etc.

on April 13, 2007 11:33 PM
# Philip said:

Ctrl+J and Ctrl+M work for the shell as well

on April 14, 2007 12:34 AM
# David said:

Great tip. I wish I could pound one little emacs fu into my head every day. Its like learning a language though, if you don't use a keystroke often you forget it.

on April 18, 2007 08:19 AM
# Raj Shekhar said:

Here is how I do it
- Select the area that you want to change
- C-u
- M-| (this will run a shell command on the selected region0
- tr '&' '\n' (or your equivalent perl one liner)

on April 20, 2007 01:32 PM
# Steven Klassen said:

Excellent tip; thanks!

on December 28, 2007 04:36 PM
# Jim said:

Perfect tip, I'd been hunting for this for awhile now. Thanks.

on July 15, 2008 04:44 PM
# nil said:

Thanks for the tip!

I used to yank newline character. It's quite annoying anyway.

on October 8, 2008 01:45 AM
# Nick Lee said:

With almost the same problem, I did a Google search, which lead me to your website.

Thank you very much! You recipe is really useful!

on May 28, 2009 01:59 AM
# Erik said:

Just used this tip, thanks a lot!

on October 14, 2009 09:01 AM
# Peter said:

More than two years later a still useful tip. Thanks :-) Also to the other voices.

on October 23, 2009 12:55 AM
# Peter Hofer said:

Follow up: I've just seen that Emacs (at least 21.4) finds the newline also with \s- , so ^.*word.*\s-+ will find all lines containing the text "word" (and any white space the following line starts with).

on October 23, 2009 01:09 AM
# noel said:

Rare to find exactly what I needed with one search -- thanks bunches!

on October 28, 2009 11:31 AM
# igor said:

Thanks. I needed it and googled your post in 1 min

on March 2, 2010 01:53 PM
Disclaimer: The opinions expressed here are mine and mine alone. My current, past, or previous employers are not responsible for what I write here, the comments left by others, or the photos I may share. If you have questions, please contact me. Also, I am not a journalist or reporter. Don't "pitch" me.


Privacy: I do not share or publish the email addresses or IP addresses of anyone posting a comment here without consent. However, I do reserve the right to remove comments that are spammy, off-topic, or otherwise unsuitable based on my comment policy. In a few cases, I may leave spammy comments but remove any URLs they contain.