2

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
  • 77,472
  • 10
  • 114
  • 243
alper
  • 1,370
  • 1
  • 13
  • 35

2 Answers2

3

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
  • 557
  • 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
  • 77,472
  • 10
  • 114
  • 243
RichieHH
  • 873
  • 4
  • 9