how does one do a query-replace-regexp to add a prefix string but not on EVERY line but rather just lines with text (or start with specific text?)

i know how to add a prefix with query-replace-regexp but that adds the prefix to every line (even empty ones).

  • Have you tried to use ^ to match the beginning of the line?
    – user12563
    Commented Nov 8, 2016 at 16:18
  • Is this query-replace, query-replaceregexp, or replace-regexp`?
    – icarus
    Commented Nov 8, 2016 at 16:26
  • hi, its query-replace-regexp. i can replace using ^ but that covers ALL lines and i dont want to add a prefix in non text (empty) lines
    – zeltak
    Commented Nov 8, 2016 at 16:32

2 Answers 2


Another way to approach this is to use occur to select the lines you want to modify, and then apply your modification in the occur buffer:

  1. M-x occur, enter the regexp that matches the lines you want to edit
  2. Switch to the occur window
  3. Type e to enter occur-edit-mode
  4. M-C-% to call query-replace-regexp, do your replacements
  5. C-c C-c to leave occur-edit-mode
  6. Move back to your original window, where the modifications you made in step 4 will be made.

This does the same thing that @icarus suggests, but uses two different regexps: one to select the line, and a second to modify the selected lines. This approach requires a few more steps, but might be easier if @icarus solution requires a very complicated regexp.

You could also do variants of this using helm-occur or other packages, but what I describe here works in regular Emacs without any additional packages.

  • great answer. I was going for simple, as the OP said he wanted to modify the prefix, so he needed to specify the regular expression anyhow, no matter how complicated.
    – icarus
    Commented Nov 8, 2016 at 22:38
  • Yes, I think your answer is the correct one for this instance. My answer is going to be more useful when the regexps get hairy. You can also use multiple-cursors in the occur buffer, which opens up different possibilities.
    – Tyler
    Commented Nov 8, 2016 at 23:46
  • hmmm so i tried this but i cant get the changes made in step 4 to reflect in the original buffer. for example a word "page" which i want to replace with "start". i search with occur, it opens a buffer, i change in the occur buffer (after pressing e) the words page to start, press C-C but nothing happens in the original buffer. any clue? what am i missing?
    – zeltak
    Commented Nov 10, 2016 at 6:42
  • It should happen automatically, if you've entered occur-edit-mode (by pressing e). What version of Emacs are you using? This works fine for me on Emacs
    – Tyler
    Commented Nov 10, 2016 at 17:00

If you want to add a prefix of hello to all lines which start with a letter, you can use C-M-% ^\([a-zA-Z]\) hello \1

The idea is to use \(...\) to capture stuff and \1 to represent the first captured group. Here I am using [a-zA-Z] to represent a single letter using the normal unix conventions. It would be better if I said [[:alpha:]] instead.

If you want to add the prefix to non-empty lines then the critical observation you need is that a line is non-empty if it has a character on it! So you want to replace ^\(.\) with prefix \1.

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.