2

Given that I have a file such as

Lorem


Ipsum

Dolor

I want to insert strings (here, the % character) to those empty lines to obtain

Lorem
%
%
Ipsum
%
Dolor

How can I do this? I tried to follow a way similar to the methods defined here without success.

2 Answers 2

3

Try this:

(defun filibuster (str)
  (interactive "sString: ")
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward "^$" nil t)
      (replace-match str))))
4
  • May I ask why you chose the name filibuster :)
    – osolmaz
    Feb 24, 2015 at 19:14
  • 1
    It had to have fill in in. Close enough:)
    – abo-abo
    Feb 24, 2015 at 19:15
  • hi abo, may i know why the whole logic is wrapped in a save-excursion? is it to preserve the current cursor position & resume it, after finishing the replacements...
    – Madhavan
    Feb 25, 2015 at 17:06
  • @MadhavanKumar, yes.
    – abo-abo
    Feb 25, 2015 at 17:13
2

@abo-abo gave the Lisp answer. You don't specify whether you want to do this from Lisp or interactively. Interactively, you can do what you ask in either of these ways:

  • M-x replace-regexp RET ^$ RET % RET

  • C-M-% RET ^$ RET % RET !

Your Answer

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

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