I'm having trouble getting query-regex-replace to do what I want. I've read the docs several times and still can't figure it out. Here are a few examples of things I can't figure out how to do with query-regex-replace:

  1. Replace a bunch of whitespace on one line followed by "*" with just "*" (get rid of the whitespace.

EDIT - I got this to work with [[:space:]]+\* RET * RET

  1. Replace two forward slashes followed by non white space with two forward slashes and white space (insert a space before the first non white space character).

EDIT - I also got this to work with //\([^[:space:]]+\) RET // \1

  1. Get rid of blank lines (optionally of course because this is a query replace).

EDIT - I still cannot get this to work. Among other things, I tried these:

)[[:space:]]+{ RET ) { RET


)\s-+{ RET ) { RET

Thanks for any help.

  • 2
    Please show an example line and the regex you tried.
    – Dan
    Commented Oct 2, 2016 at 3:37
  • I'm new to this forum format. Should I edit the original post or put the examples here? Also, I'm sure I tried at least five things for #1 above. Should I just put the most straightforward attempt? Commented Oct 2, 2016 at 17:10
  • Yes, edit the original post. Please show the regex expressions you tried.
    – Dan
    Commented Oct 2, 2016 at 17:43
  • 2
    Depending on what you're trying to do, there are a few options that may be more efficient than query-replace-regexp. Check out whitespace-cleanup for (1), comment-dwim for (2), and delete-blank-lines for (3). Unless you're batch cleaning up files, the options I suggested should be a better fit. Commented Oct 3, 2016 at 5:51
  • Love that C-x C-o to delete blank lines. Thanks for the link @Tianxiang Xiong
    – Prasanna
    Commented Oct 4, 2016 at 8:07

1 Answer 1


To replace space and/or tab sequence followed by an asterisk to just an asterisk:

[ LiteralTabHere]+\*

That LiteralTabHere should be a literal tab char. Enter it by pressing C-q C-i.

You need the backslash before asterisk. Because the asterisk is a meta char, it means 0 or more items.

To insert a space before the first non-white-space character after 2 slashes:

// \1

To remove blank lines


Each \n must be a literal return. To insert literal return, press C-q C-j.

  • Shouldn't it be C-q C-j? For me, C-q RET gives a carriage return (like \r), not a newline (\n).
    – npostavs
    Commented Oct 3, 2016 at 21:57
  • @npostavs you are right! Correcting it. Thanks.
    – Xah Lee
    Commented Oct 3, 2016 at 22:46
  • To remove blank lines one may also use M-x flush-lines and when it prompts Flush lines containing match for regexp Enter $ and hit Return key
    – Prasanna
    Commented Oct 4, 2016 at 8:04
  • @Prasanna you mean with ^$. That flush all blank lines, sometimes one wants multiple blank lines collapsed to just one.
    – Xah Lee
    Commented Oct 4, 2016 at 23:48
  • This is all great stuff. Thanks much. I am still having problems with replacement involving newlines. For instance, to replace ")" followed by blank space (including newlines) followed by "{" with ") {", I tried: ")[ \t\n]+{" ") {" It didn't work. Commented Oct 5, 2016 at 1:42

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.