Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: Insert word at point into replace string query

Tags:

emacs

Is there an analogue to inserting the word after point into the isearch query by hitting C-w after C-s but for the replace string (and replace regexp) queries?

I also enjoy Sacha Chua's modification of C-x inserting whole word around point into isearch:

http://sachachua.com/blog/2008/07/emacs-keyboard-shortcuts-for-navigating-code/

This too would be really useful in some cases if it could be used in replace string.

I'd be very thankful for any tips! Thank you!

like image 756
user673592 Avatar asked Nov 24 '11 12:11

user673592


2 Answers

This will do it, although it isn't as fancy as C-w in isearch because you can't keep hitting that key to extend the selection:

(defun my-minibuffer-insert-word-at-point ()
  "Get word at point in original buffer and insert it to minibuffer."
  (interactive)
  (let (word beg)
    (with-current-buffer (window-buffer (minibuffer-selected-window))
      (save-excursion
        (skip-syntax-backward "w_")
        (setq beg (point))
        (skip-syntax-forward "w_")
        (setq word (buffer-substring-no-properties beg (point)))))
    (when word
      (insert word))))

(defun my-minibuffer-setup-hook ()
  (local-set-key (kbd "C-w") 'my-minibuffer-insert-word-at-point))

(add-hook 'minibuffer-setup-hook 'my-minibuffer-setup-hook)

EDIT: Note that this is in the standard minibuffer, so you can use it use it anywhere you have a minibuffer prompt, for example in grep, occur, etc.

like image 79
scottfrazer Avatar answered Oct 21 '22 17:10

scottfrazer


Two answers:

  1. Replace+ automatically picks up text at point as the default value when you invoke replace commands.

  2. More generally, Icicles does something similar to what scottfrazer's code (above) does, but it is more general. At any time, in any minibuffer, you can hit M-. (by default) to pick up text ("things") at point and insert it in the minibuffer. You can repeat this, to either (a) pick up successive things (e.g. words) of the same kind, accumulating them like C-w does for Isearch, or (b) pick up alternative, different things at point. More explanation here.

like image 23
Drew Avatar answered Oct 21 '22 16:10

Drew