1

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
alper
  • 1,238
  • 11
  • 30
  • Have you tried [multiple-cursors](https://github.com/magnars/multiple-cursors.el)? – InHarmsWay Feb 06 '20 at 12:27
  • I haven't but I just want to copy a word under a single cursor – alper Feb 06 '20 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: https://github.com/magnars/expand-region.el – Tyler Feb 06 '20 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 06 '20 at 16:07
  • Where does "copy", in the context of an editor and the original Q, mean select? – RichieHH Feb 06 '20 at 23:15
  • I meant copy but while copying it can also highlight the region but not mendatory. @Drew – alper Feb 07 '20 at 07:43

2 Answers2

2

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 ()
      (interactive)
      (kill-new (thing-at-point 'symbol))))
Wojciech Gac
  • 527
  • 2
  • 13
  • 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 07 '20 at 07: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 07 '20 at 08:57
1

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:

 (use-package 
   expand-region 
   :config (defun er/select-call-f (arg) 
             (setq current-prefix-arg arg) 
             (call-interactively 'er/expand-region) 
             (exchange-point-and-mark)) 
   (defun selectFunctionCall() 
     (interactive) 
     (er/select-call-f 3)) 
   :bind ("<C-return>" . selectFunctionCall) 
   ("M-a" . er/expand-region) 
   ("M-s" . er/contract-region))
Drew
  • 75,699
  • 9
  • 109
  • 225
RichieHH
  • 848
  • 4
  • 9
  • 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](https://www.gnu.org/software/emacs/manual/html_node/elisp/Key-Binding-Conventions.html) – jue Feb 07 '20 at 09:14
  • fair comment re keys. – RichieHH Feb 12 '20 at 22:17