12

I have the following problem : when i use 'C-x r t' (string-rectangle), i am prompted to enter some string, to replace the selected rectangle. Lets say i type ";;" (to comment out the rectangle). This works just fine, but the next time i call the same function, and i want to replace with nothing (empty string), i can not do that, because there is "default" string ";;" that i previously used, and if i just hit enter the command will use that ";;" string instead of empty one. How do i set it to empty string ?

Similar problem i had with some search/replace functions. Basicaly, when it remembers the last string i used, and sets it to default, how do i "enter" empty string instead ?

EDIT :

Kill-rectangle, mentioned by YoungFrog solves the specific problem, but i had another thing in mind : basicaly, in what ever function emacs remembers the last value you entered, and uses it as "default" when you just hit enter, you should be able to clear that "default" to whatever was real default before you first used the function. Here is another example :

In dired mode, if you 'M-x rgrep' it asks you for 3 inputs :

1) What to search for : whatever you enter here, will be used as default next time you use rgrep. As someone pointed out, it makes no sense to search for empty string, so clearing this "default" value is indeed never needed i guess.

2) Next it will ask in what files to search for. It's real default is "all" files. If i enter *.txt, and search, next time when i use rgrep, *.txt will be used as "default". Now i wish to clear this "default" *.txt, and return to its original default, "all", just as i wanted to clear ";;" "default" in string-rectangle function to it's real default, empty string.

Of course, this specific problem for all files could probably be solved with typing * ? But this general problem potentialy remains with other commands that remember last used value.

3) It will next ask for Base directory, no problem here.

So, the way i see it, whenever emacs tries to help me by remembering my last used value in some command (which i find very helpful most of the time) there should be a way to clear that used value as if i never used that command before.

P.S. I am still new to emacs, and perhaps the functionality i require is not needed, as there is always some workaround ?

  • 1
    I belive that's difficult, because reading and the exchange of the default value for the empty string does not happen in the same function in most cases. In this specific case delete-rectangle is an option. – politza Jun 20 '15 at 10:03
  • If you intend to insert spaces in the selected rectangle, you can use clear rectangle. – Kaushal Modi Jun 20 '15 at 12:18
  • If you simply want to block comment/uncomment, why not use comment-dwim? Also entering "empty" string for search wouldn't make sense, but you are able to enter an empty string for the replace part. I am curious to see more examples where entering an empty string might be needed by a user. – Kaushal Modi Jun 20 '15 at 12:23
  • 3
    Just looked at your description quickly, but it sounds like there might be a problem with the design of command string-rectangle. Whenever an empty string makes sense as a possible input there should be a way to enter it, as distinguished from accepting a default value. Look closely at the doc for the command (and at its source code, if you are comfortable with that), to see if there is in fact a design flaw here. If you think there is, then please report it: M-x report-emacs-bug. – Drew Jun 20 '15 at 14:41
  • For 2), the you can literally enter all, which is slightly different from *: it matches .* too. – npostavs Sep 19 '15 at 5:05
3

Don't use C-x r t (string-rectangle), use C-x r k (kill-rectangle) instead.

  • 1
    I only answered the specific example given because IMO the general case (the question in the title) admits no (general) answer. Still, I want to mention that C-j (instead of RET) might help in some cases, e.g. ido-mode. – YoungFrog Jun 20 '15 at 13:16
3

In the general case, what you want cannot be achieved. This is because several utilities and modes in Emacs store their read-completion history in different variables, not in one location. You can manually set those history variables to nil (for your C-x r t example, (setq string-rectangle-history nil)), but it doesn't scale. It's also not easy to discover the names of the history variables without reading the code.

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.