1

when typing in some projectile filter string for instance (eg SPC p f) in spacemacs, I often would like to reuse the word under the cursor.

I know about C-y to paste. Which is the one to insert the word under the cursor? I stumbled on C-w but it's not that. And why does C-w does by the way??

(btw if there is something more spacemacs-y I'm interested)

  • 2
    @Drew The question title is completely unclear to me. But I understand what he's asking from the body: he wants to insert the word at the point when using projectile-find-file. – nanny Oct 30 '15 at 18:32
  • 1
    I think it's called the minibuffer. Unfortunately I'm not too familiar with the correct emacs terminology (vim convert using spacemacs). – Emmanuel Touzery Oct 30 '15 at 18:39
  • the reason for the title is because I had found about C-w and didn't understand its behaviour. I thought it stopped at some separator on left and right before hitting the spaces. I now think that it goes from cursor to space. I want the whole word under the cursor, from the space (or bracked) before to the space (or dot, or bracket) after. – Emmanuel Touzery Oct 30 '15 at 18:44
  • The question is clearer after the edit to remove mention of C-s etc. Thx. – Drew Oct 30 '15 at 19:59
4

If you're using Helm (which I believe is the default in Spacemacs), you can insert the word at the point with M-n or C-w.

From the Helm Wiki:

To yank the symbol at point from helm-current-buffer (i.e. buffer where a helm command originated):

M-n

Alternatively, customize helm-yank-symbol-first to enable

C-w

to always yank the whole symbol on first invocation.

  • Ok, M-n is what I want. C-w inserts from the cursor to the end of the word. M-n inserts the whole word. HOWEVER, M-n is a pain to reach on the keyboard... Is there any way I could remap M-n to another key or change the C-w behaviour to be more like M-n? – Emmanuel Touzery Oct 30 '15 at 18:42
  • @EmmanuelTouzery While in Helm, if I do C-h c M-n, I can see that the key is mapped to the function next-history-element. So you can do (define-key helm-map (kbd "C-w") 'next-history-element) to assign C-w to the same function as M-n. – nanny Oct 30 '15 at 19:00
  • thanks, I tried it but it didn't work. I get symbol's value as variable is void: helm-map. Otherwise I checked and what I am in is called help projectile. – Emmanuel Touzery Oct 30 '15 at 19:15
  • 2
    Most likely you're getting the "helm-map is void" error because you're making the assignment before launching Helm and thereby autoloading the rest of the Helm package. You should probably do (with-eval-after-load 'helm (define-key ...)) instead, which delays the binding until everything has been properly loaded. – Aaron Harris Oct 30 '15 at 19:40
  • That helm wiki section is outdated: helm-yank-symbol-first doesn't exist any more. – xuchunyang Oct 31 '15 at 4:25
3

Below is some commands that operate text between current buffer and minibuffer in helm:

  • C-w (helm-yank-text-at-point) Append the next word at point into minibuffer, like isearch's C-w
  • M-n (next-history-element) Move through the “future history” list (see manual Minibuffer History, the first element of it is the symbol at point in the beginning)
  • C-c C-y (helm-yank-selection) Set minibuffer contents to helm selection
  • C-c C-k (helm-kill-selection-and-quit) Save helm selection to kill-ring and quit helm
  • C-c C-i (helm-copy-to-buffer) Insert helm selection into the current buffer

If you want C-w to insert from the beginning of word, not the cursor's position, you can try the following hack:

(defun helm-yank-text-at-point--move-to-beginning (orig-func &rest args)
  "Initialize `helm-yank-point' to the beginning of word at point."
  (unless helm-yank-point
    (setq helm-yank-point
          (with-helm-current-buffer
            (save-excursion
              (let ((fwd-fn (or helm-yank-text-at-point-function #'forward-word)))
                (funcall fwd-fn -1))
              (point)))))
  (apply orig-func args))

(advice-add 'helm-yank-text-at-point :around
            #'helm-yank-text-at-point--move-to-beginning)
  • otherwise for me C-w doesn't insert the word, but from the cursor the end of the word. That's why I resorted to that shortcut remap. I would prefer if I could say to C-w, give me the full word, not only from cursor to end of word. – Emmanuel Touzery Oct 31 '15 at 7:25
  • well now I noticed that isearch's C-w does the same as the helm's default of inserting cursor-to-end-of-word. Can that be changed too? – Emmanuel Touzery Oct 31 '15 at 7:39
  • found it here, to get C-w to insert the whole word! emacswiki.org/emacs/SearchAtPoint#toc6 – Emmanuel Touzery Oct 31 '15 at 7:42
  • @EmmanuelTouzery The added answer should do what you need on helm's C-w, but it looks complex since it changes some helm internal variable, besides it may no work in the future. – xuchunyang Oct 31 '15 at 8:20
  • Ok thank you. Then I'll stick with the M-n alias for now and keep this in mind (also one day to review when I want to improve my emacs lisp skills). – Emmanuel Touzery Oct 31 '15 at 8:37

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.