I am trying to replace a character - say ;
- with a new line using replace-string
and/or replace-regexp
in Emacs.
I have tried the following commands:
M-x replace-string
RET ;
RET \n
This will replace ;
with 2 characters: \n
.
M-x replace-regex
RET ;
RET \n
This results in the following error (shown in the minibuffer):
Invalid use of `\' in replacement text.
What's wrong with using replace-string
for this task? Is there any other way to do it?
Thanks.
There are four ways I've found to put a newline into the minibuffer.
C-o
C-q C-j
C-q 12
(12 is the octal value of newline)
C-x o to the main window, kill a newline with C-k, then C-x o back to the minibuffer, yank it with C-y
Switch to text-mode
M-x text-mode
Highlight block to indent
Indent
C+M \
Switch back to whatever mode..
Don't forget that you can always cut and paste into the minibuffer.
So you can just copy a newline character (or any string) from your buffer, then yank it when prompted for the replacement text.
More explicitly:
To replace the semi colon character (;) with a newline, follow these exact steps.
Bit more to it than the original explanation says.
inline just:
C-M-S-%
(if binding keys still default) than
replace-string
^J
Source: Stackoverflow.com