3

By M-backspace (or by M-x backward-kill-word), I can remove the word before the cursor. Is there a built-in equivalent of this command for copying instead of deleting?

Obviously one can do M-x backward-kill-word then M-x yank (or M-x undo), but I am wondering if there is a better solution (aside from not to be so quick, the inconvenience is that it first changes the status of the buffer).

  • I usually do C-M-- (control meta hyphen) C-M-SPC M-w – nanny Jul 6 '15 at 20:41
5

C-u -1 M-@ M-w or define a new command yourself, something like

(defun backward-copy-word ()
  (interactive)
  (save-excursion
    (copy-region-as-kill (point) (progn (backward-word) (point)))))
  • Your second solution seems to not working. It seems to copy the word after the point and If there is no text after the point, the cursor jumps below (the first solution is essentially the one given by @JennyD.) – Name Jul 4 '15 at 15:00
  • Sorry, I didn't test it, the forward-word should be replaced with backward-word obviously, I've changed it, should work now. – xuchunyang Jul 4 '15 at 15:20
  • Basically the same thing but faster to type: M-- C-M-SPC M-w. You can use M-- (or C--) to specify a negative argument, which will be treated the same as C-u - 1. C-M-SPC calls mark-sexp, which will mark a word in most cases. – glucas Jul 6 '15 at 14:07
3

C-u - 1 M-x mark-word will give -1 as an argument to mark-word, thus marking the previous word. After that, M-w will copy it to the kill ring without actually deleting it, making it available to paste elsewhere.

  • Ok, this selects the word before the point, but still one has to copy it. – Name Jul 4 '15 at 14:33
  • @Name I've added that, testing now that the solution makes the copied word available both in the X buffer and in the emacs kill ring. – Jenny D Jul 4 '15 at 15:05
  • 1
    C-u - 1 can be simplified to just C-- (ctrl minus). – glucas Jul 5 '15 at 3:52
  • To add to @glucas, C-- runs negative-argument which is also bound to C-M-- and M-- for ease of combining with other keystrokes. – nanny Jul 6 '15 at 20:42
1

Emacs doesn't have a native way to copy words, but you can always write a function and and bind a key to that using examples from https://emacswiki.org/emacs/CopyWithoutSelection

(defun get-point (symbol &optional arg)
      "get the point"
      (funcall symbol arg)
      (point)
    )

(defun copy-backward-word ()
 "copy word before point"
  (interactive "")
  (save-excursion
   (let ((end (point))
     (beg (get-point 'backward-word 1)))
     (copy-region-as-kill beg end)))
  )

(global-set-key (kbd "C-c !") 'copy-backward-word) ;; Adapt this.

Marking a word though is easy:

M- -1 M-x mark-word or assuming default bindings M- -1 M-@

  • Sorry, typo. "Meta - 1" then "M-x mark word". – rocky Jul 4 '15 at 14:31
  • Ok, this selects the word before the point, but still one has to copy it. – Name Jul 4 '15 at 14:32
1

Assuming you use expand-region, when point is after a word you can just:

  1. Press its binding (I use M-SPC).

  2. Press M-w.

This also works if point is before or within the word.

You can also repeat step 1 to mark semantically bigger chunks of text.

  • thank you, interesting package, it has also other useful functionalities. – Name Jul 5 '15 at 22:05
  • Yes, expand-region is awesome. – Greg Hendershott Jul 6 '15 at 1:52

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.