Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I replace a character with a newline in Emacs?

Tags:

replace

emacs

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 two 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 another way to do it?

like image 548
Lei Avatar asked Mar 04 '09 23:03

Lei


People also ask

How do I add a new line in Emacs?

Insert the line to ". emacs" file. <C + S + Return> that means <Ctrl + Shift + Enter> for new line above. Both will indent also.

How do I replace in Emacs?

Simple Search and Replace Operations When you want to replace every instance of a given string, you can use a simple command that tells Emacs to do just that. Type ESC x replace-string RETURN, then type the search string and press RETURN. Now type the replacement string and press RETURN again.

How do I create a new line character in Vim?

Use \r instead of \n . Substituting by \n inserts a null character into the text. To get a newline, use \r . When searching for a newline, you'd still use \n , however.


2 Answers

M-x replace-string RET ; RET C-q C-j.

  • C-q for quoted-insert,

  • C-j is a newline.

like image 157
Jonathan Arkell Avatar answered Sep 19 '22 11:09

Jonathan Arkell


There are four ways I've found to put a newline into the minibuffer.

  1. C-o

  2. C-q C-j

  3. C-q 12 (12 is the octal value of newline)

  4. 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

like image 31
slipmthgoose Avatar answered Sep 17 '22 11:09

slipmthgoose