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.
Hmm.
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
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?
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.
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."
Crash.
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.
You can also paste (yank) a newline out of your buffer into an interactive prompt.
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.
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.
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.
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)
Perfect tip, I'd been hunting for this for awhile now. Thanks.
Thanks for the tip!
I used to yank newline character. It's quite annoying anyway.
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!
More than two years later a still useful tip. Thanks :-) Also to the other voices.
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).
Rare to find exactly what I needed with one search -- thanks bunches!