vrwim
vrwim

Reputation: 14270

How do I replace a regular expression with a newline in emacs

I would like to put a newline after every tag in an HTML file in Emacs. Now I have got

M-x query-replace-regexp \(<[^>]*>\) --> \1\n

This returns

invalid use of "\"

I've already tried C-x C-j, but that returns <html>C-x C-j<head>C-x C-j etc...

If anybody knows the solution, that would be helpful :)

Upvotes: 4

Views: 1123

Answers (3)

Jason Clark
Jason Clark

Reputation: 1391

The above answers are correct. An additional caveat: when trying match a whole line including newline, e.g. ^.*SOMETHING.*$, replace the $ with C-q C-j, do not append C-q C-j. Unlike some other Regexp dialects, $ plus newline (C-q C-j) does not match in emacs Regexp.

Upvotes: 0

pfalstad
pfalstad

Reputation: 216

instead of \n, use C-q C-j

http://jeremy.zawodny.com/blog/archives/008872.html

Upvotes: 5

chrm
chrm

Reputation: 1288

You can use C-q to insert control characters.

C-q C-j to insert a newline, C-q C-i to insert a tabulator etc.

So instead of \n just type C-q C-j.

Upvotes: 8

Related Questions