5

When I use replace-string for the first time, it asks me for the string to replace and string to replace with. When running it again, it uses the previous values as default:

Replace string (default a -> b):

I want to replace an empty string now. I can't just press Enter, though, as that would confirm the default. Is there a way how to replace the empty string after having previously replaced something else?

When asked for the first time without default, just pressing Enter works.

Note: I know there are other ways how to get the thing done. My question is how to enter an empty string when there's a default.

4
  • 1
    See this: emacs.stackexchange.com/questions/13297/… In other words, you might be out of luck. For your particular use case though, you might be able to rig something up where you set (setq query-replace-defaults nil) the right way at the right time, but I haven't come up with anything yet, hence no answer. Setting that variable to nil will get rid of the default, but only temporarily.
    – elethan
    Jan 26, 2016 at 15:47
  • @elethan: Well, there could be a solution for future if there was a key combo to make read-from-minibuffer return nil. Authors of those prompt functions could then clear the defaults if that happened.
    – choroba
    Jan 26, 2016 at 16:14
  • Just wondering -- what's the use case for replacing the empty string? It seems to insert the replacement text between each character.
    – zck
    Jan 26, 2016 at 17:05
  • @zck: Exactly. The input contains digits, I need to separate them by spaces.
    – choroba
    Jan 26, 2016 at 17:06

5 Answers 5

4

Search regexp \(\) is the exact same as hitting RET when trying to specify an empty string for searching.


As a quick test, using both \(\) and RET resulted in 833 search hits for this random text block generated using a random text generator.

abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<>

|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<>

|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<>

|; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<> |; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ !

"§ $%& /() =? * '<> |; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<> |; ²³~ @`´ ©«» ¤¼× {} abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI JKL MNO PQRS TUV WXYZ ! "§ $%& /() =? * '<> |; ²³~ @`´ ©«» ¤¼× {}abc def ghi jkl mno pqrs tuv wxyz ABC DEF GHI

But based on your comment:

The input contains digits, I need to separate them by spaces.

using \([[:digit:]]\) as zck states in his answer would be a more efficient search regexp.

1

This isn't an answer to the exact question you asked, but it does solve your problem.

You can use replace-regexp, with the search regexp as \([[:digit:]]\) and \1 as replacement (Note that there's a space after \1).

The regexp captures a single digit (the character class [:digit:]). The replacement is the group matched in the regexp followed by a space.

4
  • That is really not what he asked. Did you see choroba's profile? I don't think he needs any help with regular expressions. I've got the impresion he described his usecase as an example where one wants to replace the empty string.
    – Tobias
    Jan 26, 2016 at 17:27
  • @Tobias It's based on what choraba said in the comments to the question: "The input contains digits, I need to separate them by spaces." Jan 27, 2016 at 2:56
  • 2
    @choroba On a side note, even if this answer solves the problem for your use case, it does not answer the stated question. It would be good to edit the question if this answer solves your problem. Jan 27, 2016 at 3:14
  • @KaushalModi I am fully aware of choraba's comment. As I understand it it just serves as an example for the actual problem from the question -- the missing possibility to input an empty string. Judging from choraba's profile he does not need any help on regular expressions. But maybe I am wrong.
    – Tobias
    Jan 27, 2016 at 6:13
0

I'm not sure this feature existed back in 2016, but nowadays you can press Up when prompted for the replace string, it shows the string a → b where you can delete the a to replace the empty string.

0

It is not the replace string that makes troubles. It is always the same search string one cannot change!

However, the correct remedy is not the string shown in my previous post, the insert should read instead:

(custom-set-variables' (query-replace-from-to-separator nil) )

Sorry for the confusion ...

1
0

After inserting the line

'(query-replace-from-to-separator nil)

into the section (custom-set-variables ... ) of my .gnu-emacs-custom file, the unwanted default search-string behavior disappeared.

1
  • Please edit your answer to what you want it to be. Don't submit two answers that are the same, one with corrections. Thx.
    – Drew
    Oct 26, 2022 at 16:53

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.