3

Running spacemacs 0.200.

I am sorry if it is too basic. But I am just not able to get this.

How do I convert two consecutive newlines with some text, say, 'there were two newlines here'?

I could see the notation for special character classes like [[:alnum:]], etc. But no clue about newlines.

5
  • Your question is not clear to me. Do you want to replace all occurrences of <br><br> with some text?
    – StarBug
    Jun 10, 2019 at 14:06
  • @StarBug No. I want to replace two consecutive newlines with the text '<br><br>'. Anyway, I have edited the question to make it more clear.
    – deshmukh
    Jun 10, 2019 at 15:28
  • 1
    Interactively, you can use replace-regexp for this. Just type C-q C-j to indicate a newline in the REGEXP.
    – StarBug
    Jun 10, 2019 at 15:53
  • @StarBug: Please post that comment as an answer.
    – Drew
    Jun 10, 2019 at 16:23
  • Related: emacs.stackexchange.com/q/9548
    – phils
    Jun 11, 2019 at 22:44

1 Answer 1

3

Interactively, you can use replace-regexp for this. Type C-q C-j to insert a newline character in the minibuffer (so you would need to type this twice).

In Elisp strings you can use \n to indicate a newline, so you would write (re-search-forward "\n\n") to search for two consecutive newlines. You can then use a replace-match command for the replacement.

2
  • 2
    The same should work with query-replace instead of replace-regexp. Jun 10, 2019 at 17:56
  • Why doesn't C-q RET work? Sep 22, 2020 at 17:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.