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.

  • 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 '16 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 '16 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 '16 at 17:05
  • @zck: Exactly. The input contains digits, I need to separate them by spaces. – choroba Jan 26 '16 at 17:06
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.

  • 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 '16 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." – Kaushal Modi Jan 27 '16 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. – Kaushal Modi Jan 27 '16 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 '16 at 6:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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