Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete extra blank lines in emacs

Tags:

regex

emacs

M-x flush-lines ^$ 

deletes all blank lines in a buffer. However I only want to delete extra blank lines, that is if there are n consecutive blank lines I want to delete n-1 and keep one.

I know that delete-blank-lines does the job for the blank lines under the point, however I want a simple solution which works for the whole buffer.

Any ideas how to do this? Especially is it possible to modify the regex ^$ from my first example to match only the extra lines?

like image 737
student Avatar asked Dec 11 '10 23:12

student


People also ask

How do I delete multiple lines in Emacs?

Move your cursor directly before the region you want to delete. Set a mark by pressing Ctrl-6 or Ctrl-^ . Move the cursor to the end of the region you want to delete and press Ctrl-k .

How do I permanently delete blank lines?

The d command in sed can be used to delete the empty lines in a file.

How do I insert a blank line in Emacs?

When you want to insert a new line of text before an existing line, you can do it by typing the new line of text, followed by RET .


1 Answers

C-x h M-x replace-regexp RET ^ C-q C-j C-q C-j + RET C-q C-j RET

which marks the whole buffer and replaces two or more blank lines with a single blank line.

like image 76
jlf Avatar answered Oct 16 '22 02:10

jlf