I want to copy the name of the symbol at point, without using the mouse.

(global-set-key (kbd "C-s C-c") '<copy_word>)

Possible word marking as i-search does on the following example usage (https://emacs.stackexchange.com/a/55321/18414)

For example: [_id_hello_world] and if cursor is in between hello and world I want to copy complete _id_hello_world.

  • Have you tried multiple-cursors? – InHarmsWay Feb 6 at 12:27
  • I haven't but I just want to copy a word under a single cursor – alper Feb 6 at 13:46
  • expand-region provides a really nice interface for quickly marking the word at point (and then the sentence, the paragraph, etc). Not exactly what you asked, but that's how I do these operations: github.com/magnars/expand-region.el – Tyler Feb 6 at 15:48
  • Please clarify whether you mean copy (so it can be pasted/yanked) or select (highlighted as the region), or both. – Drew Feb 6 at 16:07
  • Where does "copy", in the context of an editor and the original Q, mean select? – RichieHH Feb 6 at 23:15

What you want is something like (kill-new (thing-at-point 'symbol)). When run, it first extracts the symbol at point and then adds it to the kill-ring, i.e. copies it. One caveat you have to keep in mind is that you need an interactive function/lambda in order to be able to invoke it with a keybinding. So actually you can have a binding of the form:

(global-set-key (kbd "C-s C-c")
   (lambda ()
      (kill-new (thing-at-point 'symbol))))
| improve this answer | |
  • I am having following error: error: Key sequence C-s C-c starts with non-prefix key C-s but works with keybinding starting with C-x @Wojciech Gac – alper Feb 7 at 7:55
  • Right. I failed to notice that. C-s won't do as prefix, as it's already being used for search. – Wojciech Gac Feb 7 at 8:57

While not strictly answering your question regarding one key copy symbol/word at point I like to use expand-region since frequently I want to copy a lot at point without needing to move to the start, mark, move to end and copy. It might be of use.

For example, here are bindings to expand and contract the active region (and of course I then need to M-w to copy but it's still reasonably efficient:

   :config (defun er/select-call-f (arg) 
             (setq current-prefix-arg arg) 
             (call-interactively 'er/expand-region) 
   (defun selectFunctionCall() 
     (er/select-call-f 3)) 
   :bind ("<C-return>" . selectFunctionCall) 
   ("M-a" . er/expand-region) 
   ("M-s" . er/contract-region))
| improve this answer | |
  • 2
    Thanks for mentioning expand-region. Be aware, that it's a bad advice to shadow keybindings M-a and M-s, they do useful things (i.e M-a in org-mode tables and M-s o is for occur). Read more about keybindings at emacs manual – jue Feb 7 at 9:14
  • fair comment re keys. – RichieHH Feb 12 at 22:17

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.