ctrlf icon indicating copy to clipboard operation
ctrlf copied to clipboard

Duplicate Isearch's C-w

Open urish1 opened this issue 4 years ago • 21 comments
trafficstars

After starting Isearch, C-w (isearch-yank-word-or-char) yanks the word at point into the current query. You can call it successively, and it yanks additional words. I find this workflow very useful, and I miss it when using ctrlf.

Any chance of adding it?

Thanks for an otherwise excellent package!

urish1 avatar Dec 06 '20 08:12 urish1

Hmm, after one day of switching to ctrlf I'm realising how badly I miss this feature. I think it's actually going to be a deal-breaker :-( Would it be easy to add?

aspiers avatar Dec 16 '20 01:12 aspiers

I don't think it would be too challenging, no.

raxod502 avatar Dec 21 '20 22:12 raxod502

Me too! :-)

Tried ctrlf today and found this almost immediately. It's the one isearch feature I use a lot.

ska2342 avatar Jan 19 '21 20:01 ska2342

@urish1, @aspiers, @ska2342 Did you try the future history, i.e., M-n? To me, isearch's C-w is one of those quircky special features that you actually don't want to replicate.

astoff avatar Jan 20 '21 07:01 astoff

Happy to see this feature request is already here.

But thanks for the tip @astoff, that helps already. Now I need to train my muscle memory.

To1ne avatar Jan 20 '21 09:01 To1ne

My use-case for C-w is to search for usages of a function or variable. Very, very barebones code navigation; works for all coding languages as it doesn't rely on special features of the modes. I'm on a function call, I know its definition is in the current file, I hit C-s C-w C-s C-s C-s ... until I find what I'm searching for. Lightning fast. Been doing that for at least 15 years. But that's just me. :-)

ska2342 avatar Jan 20 '21 10:01 ska2342

@astoff commented on January 20, 2021 7:02 AM:

@urish1, @aspiers, @ska2342 Did you try the future history, i.e., M-n? To me, isearch's C-w is one of those quircky special features that you actually don't want to replicate.

I'm afraid I can't understand this at all. M-n is bound to next-history-element, and the behaviour of that function bears zero resemblance to isearch's C-w. The former navigates through history, whereas the latter yanks the rest of the word at point into the minibuffer. Am I missing something, or were you thinking of something completely different?

BTW the use case @ska2342 describes is a very common and sensible one.

aspiers avatar Jan 20 '21 11:01 aspiers

@aspiers M-n in this context seems to fill the search string with a default value which seems to be symbol/word at point or something like that. So it kind of works like C-w, except you can't repeat M-n to expand the selection.

Supporting C-w would be great, though. Although it might be strange since CTRLF hides the cursors. 🤔

luismbo avatar Jan 23 '21 21:01 luismbo

@luismbo It doesn't for me - like I said, I find it's bound to next-history-element. Clearly there is something different between our setups. I know that ivy-next-history-element has the behaviour you describe, so maybe somehow you have that bound to M-n?

aspiers avatar Jan 24 '21 00:01 aspiers

@aspiers Works for me as well. Maybe you have an old version installed?

So it kind of works like C-w, except you can't repeat M-n to expand the selection

If expanding the search subject is a desired feature, I would suggest it to work more like expand-region instead of just growing word-by-word to the right.

Also, I find the keybinding C-w in Isearch pretty incongruous, not the least because those keys mean backward-kill-word in readline. That Isearch command should have been bound to M-f instead. So, if the feature is included to ctrlf, I suggest adding a special behavior to M-b and M-f at the beginning/end of minibuffer.

astoff avatar Jan 24 '21 07:01 astoff

@astoff commented on January 24, 2021 7:54 AM:

@aspiers Works for me as well. Maybe you have an old version installed?

Nope. I've also checked the source and it does not bind M-n to anything. What is M-n bound to in the ctrlf mini-buffer for you? Type C-s C-h c M-n to find out. I suspect it's ivy-next-history-element like I mentioned previously, and that you are getting that binding from ivy somehow. I am using selectrum more than ivy, which could explain the difference.

So it kind of works like C-w, except you can't repeat M-n to expand the selection

If expanding the search subject is a desired feature, I would suggest it to work more like expand-region instead of just growing word-by-word to the right.

That would be nice to have on a separate binding, but it would also not work for a very common use case of mine (and I suspect @ska2342's too) where I want to capture a prefix of some symbol. For example if my point is at the beginning of the word use-package-defaults on the line starting its defcustom declaration, I could do C-s C-w C-w C-s C-w and then continue pressing C-s to search for consequent mentions of use-package-handler.

Also, I find the keybinding C-w in Isearch pretty incongruous, not the least because those keys mean backward-kill-word in readline.

Absolutely agree with this :-)

That Isearch command should have been bound to M-f instead.

Ah, that's a very nice idea, but only if M-f still invoked forward-word as previously when not at the end of the minibuffer. I think that's probably the same as what you're suggesting here:

So, if the feature is included to ctrlf, I suggest adding a special behavior to M-b and M-f at the beginning/end of minibuffer.

Presumably you mean that M-b would add a new prefix to the beginning of the minibuffer obtained from the word prefix before the point in the current buffer? Again, that sounds nice to me.

In summary, if ctrlf offered these two special behaviour bindings, plus a binding for equivalent behaviour to er/expand-region, then I think it would overtake the competition ;-)

aspiers avatar Jan 24 '21 11:01 aspiers

What is M-n bound to in the ctrlf mini-buffer for you?

next-history-element. "Future history" is a standard feature of completing-read, and the beauty of ctrlf is that nothing extra is needed to implement this. If you're curious how this works, search for the 2 occurrences of (thing-at-point 'symbol t) in ctrlf.el.

I want to capture a prefix of some symbol.

Fair enough. If I wanted something so fine-grained, I would just type it, but it's a matter of taste.

I think that's probably the same as what you're suggesting here

You bet!

astoff avatar Jan 24 '21 11:01 astoff

@astoff commented on January 24, 2021 11:35 AM:

What is M-n bound to in the ctrlf mini-buffer for you?

next-history-element. "Future history" is a standard feature of completing-read, and the beauty of ctrlf is that nothing extra is needed to implement this. If you're curious how this works, search for the 2 occurrences of (thing-at-point 'symbol t) in ctrlf.el.

OK, now I understand. Unfortunately this does not give the desired behaviour, because it populates the future history with the symbol which was at point when ctrlf was invoked, not when M-n was pressed.

aspiers avatar Jan 24 '21 12:01 aspiers

I also think implementing C-w functionality when there is no region selected in the minibuffer would be a wonderful addition. I too have 20yrs of muscle memory of C-s C-w. I should probably switch to M-s . for most of these, but one really nice advantage is that multiple repeated C-w's bring in more words, and then a single delete removes the words you just brought in. With this, you can pick your level of granularity. So for example you can easily choose to search for just ctrlf-translate and not an entire symbol.

Thanks for this exceptional tool. No more guessing whether Ctrl-g will actually exit an isearch.

jdtsmith avatar Apr 10 '21 19:04 jdtsmith

@jdtsmith commented on April 10, 2021 8:49 PM:

I also think implementing C-w functionality when there is no region selected in the minibuffer would be a wonderful addition. I too have 20yrs of muscle memory of C-s C-w. I should probably switch to M-s . for most of these, but one really nice advantage is that multiple repeated C-w's bring in more words, and then a single delete removes the words you just brought in.

Yes - not only this, but C-w also allows you to start your search with other terms (e.g. by typing them manually) and then augment the search with words (or fragments thereof) under the point. M-s . provides a strict subset of the functionality offered by isearch's C-w.

Thanks for this exceptional tool. No more guessing whether Ctrl-g will actually exit an isearch.

Right ;-)

aspiers avatar Apr 10 '21 19:04 aspiers

After starting Isearch, C-w (isearch-yank-word-or-char) yanks the word at point into the current query. You can call it successively, and it yanks additional words. I find this workflow very useful, and I miss it when using ctrlf.

Any chance of adding it?

Thanks for an otherwise excellent package!

So it seems the C-w feature still not been added?

basheewang avatar Feb 18 '22 08:02 basheewang

No, but pull requests happily accepted. At some point I will go through the CTRLF backlog and clean up things like this, but it has not yet reached the top of my list yet.

raxod502 avatar Feb 19 '22 02:02 raxod502

The following works for me

(defun ctrlf-yank-word-or-char ()
  (interactive)
  (let ((input (field-string (point-max))) yank)
    (when (or ctrlf--match-bounds (= (length input) 0))
      (with-current-buffer (window-buffer (minibuffer-selected-window))
        (setq yank (buffer-substring-no-properties
                    (or (and ctrlf--match-bounds
                             (cdr ctrlf--match-bounds))
                        ctrlf--current-starting-point)
                    (progn (forward-word) (point)))))
      (goto-char (field-end (point-max)))
      (insert yank))))

(define-key ctrlf-minibuffer-mode-map (kbd "C-w") #'ctrlf-yank-word-or-char)

thisirs avatar Jul 21 '22 14:07 thisirs

Works for me too, thanks - would be great to have this added to the package!

aspiers avatar Jan 29 '23 16:01 aspiers