Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete(or kill) the current word in emacs?

Tags:

emacs

For example, my cursor (point) is at an arbitrary letter in the word "cursor". I want to delete (kill) that word such that it is copied to kill-ring.

like image 578
Jianglong Chen Avatar asked Oct 30 '15 17:10

Jianglong Chen


People also ask

How do you kill a word in Emacs?

The Emacs way to remove the word one is inside of to press M-backspace followed by M-d . That will kill the word at point and save it to kill ring (as one unit). If the cursor is at the beginning or after the end of the word, only one of the two is sufficient.

How do I delete selected text in Emacs?

Delete Selection mode lets you treat an Emacs region much like a typical text selection outside of Emacs: You can replace the active region just by typing text, and you can delete the selected text just by hitting the Backspace key ( 'DEL' ).

How do I kill in Emacs?

The default command for killing the rest of the current line is 'kill-line' ( 'C-k' ). The default command for killing the rest of a sentence is 'kill-sentence' ( 'M-k' ).

How do I kill a line in Emacs?

The simplest kill command is C-k ( kill-line ). If used at the end of a line, it kills the line-ending newline character, merging the next line into the current one (thus, a blank line is entirely removed).


2 Answers

You could use this as a framework for killing various kinds of things at point:

(defun my-kill-thing-at-point (thing)
  "Kill the `thing-at-point' for the specified kind of THING."
  (let ((bounds (bounds-of-thing-at-point thing)))
    (if bounds
        (kill-region (car bounds) (cdr bounds))
      (error "No %s at point" thing))))

(defun my-kill-word-at-point ()
  "Kill the word at point."
  (interactive)
  (my-kill-thing-at-point 'word))

(global-set-key (kbd "s-k w") 'my-kill-word-at-point)
like image 79
phils Avatar answered Sep 17 '22 20:09

phils


The Emacs way to remove the word one is inside of to press M-backspace followed by M-d. That will kill the word at point and save it to kill ring (as one unit).

If the cursor is at the beginning or after the end of the word, only one of the two is sufficient. An Emacs user will typically move between words using commands such as forward-word (M-f) and backward-word (M-b), so they will be at the word boundary to begin with and thus rarely need to kill the word from the inside.

like image 31
user4815162342 Avatar answered Sep 17 '22 20:09

user4815162342