Emacs: how to delete text without kill ring?

Emacs

Emacs Problem Overview


I'd like to just delete some text so I can yank some other text instead of it. How can I do that? C-w cuts the selected text to kill ring and I end up without the text I wanted to yank.

Also, is it possible to yank text directly instead of some text without even pressing buttons to kill it?

Emacs Solutions


Solution 1 - Emacs

I type M-x delete-region quite often, but you can bind it it to a key.

With Delete Selection Mode in newer versions of Emacs you don't have to type a command just start typing:

> By default, text insertion occurs normally even if the mark is active—for example, typing a inserts the character ‘a’, then deactivates the mark. Delete Selection mode, a minor mode, modifies this behavior: if you enable that mode, then inserting text while the mark is active causes the text in the region to be deleted first. Also, commands that normally delete just one character, such as C-d or DEL, will delete the entire region instead. To toggle Delete Selection mode on or off, type M-x delete-selection-mode.

Solution 2 - Emacs

You can use M-y after C-y to insert previous item from the kill ring, or use browse-kill-ring package.

As for the second question, see DeleteSelectionMode.

Solution 3 - Emacs

I have had the same issue. The closest thing I've got so far is to just make a small function that's essentially:

(defun ruthlessly-kill-line ()
  "Deletes a line, but does not put it in the kill-ring. (kinda)"
  (interactive)
  (move-beginning-of-line 1)
  (kill-line 1)
  (setq kill-ring (cdr kill-ring)))

Solution 4 - Emacs

Taken from the EmacsWiki:

> The equivalent of ‘kill-line’ (‘C-k’) but without kill-ring side effects: > > (delete-region (point) (line-end-position))

Solution 5 - Emacs

M-x eval-expression (setq kill-ring (cdr kill-ring)) - removes last item from kill-ring

Solution 6 - Emacs

For your second question, alternatively to DeleteSelectionMode you can enable CUA Mode which additionally gives you a nice rectangle selection mode enabled by C-Return. CUA mode is part of emacs since 22.1.

Solution 7 - Emacs

Most kill functions use kill-region to do the actual work of killing text. I use a lisp macro to create delete functions from kill functions.

(defmacro jpk/delete-instead-of-kill (&rest body)
  "Replaces `kill-region' with `delete-region' in BODY."
  `(cl-letf (((symbol-function 'kill-region)
              (lambda (beg end &optional yank-handler)
                (delete-region beg end))))
     ,@body))

(defun jpk/delete-word (arg)
  "Like `kill-word', but does not save to the `kill-ring'."
  (interactive "*p")
  (jpk/delete-instead-of-kill (kill-word arg)))

Solution 8 - Emacs

Found an answer to this.

Posted it first here.

;; Ctrl-K with no kill
(defun delete-line-no-kill ()
  (interactive)
  (delete-region
   (point)
   (save-excursion (move-end-of-line 1) (point)))
 (delete-char 1)
)
(global-set-key (kbd "C-k") 'delete-line-no-kill)

Solution 9 - Emacs

Technically, the other answers are wrong on the first part.

Here is my implementation and motivation:

(defun delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument ARG, do this that many times."
  (interactive "p")
  (delete-region (point) (progn (backward-word arg) (point))))

I adapted the code here from "kill-word" in simple.el. I switched kill-region with delete-region and forward-word for backward-word. This way it TRUELY does not affect the kill-ring, unlike the other situations where, outside of emacs, I noticed that the kill-ring was influenced.

Solution 10 - Emacs

(defun copy-to-register-z (p1 p2)
  "Copy text selection to register named “z”."
  (interactive "r")
  (copy-to-register ?z p1 p2))
(defun replace-register-content-z (p1 p2)
  "Replace register named “z”'s content."
  (interactive "r")
  (delete-region p1 p2)
  (insert-register ?z))
(global-set-key (kbd "C-c c") 'copy-to-register-z)
(global-set-key (kbd "C-c v") 'replace-register-content-z)

Solution 11 - Emacs

Add a delete equivalent of kill-region and kill-line keys C-w and C-k as follows. Bound to keys C-v and C-z.

;; A keybinding to delete-region gives a good "Windows CUT Ctrl-x equivalent".
;; What keybinding to use is awkward to choose.
;; using C-v "Windows PASTE Ctrl-v" is quite a subversive option.
;;  C-v = scroll up in emacs which I have no use for.
(global-set-key (kbd "C-v") 'delete-region)

;; To have also a "Windows CUT" alternative to C-k (kill-line) this can be done: (defun delete-line () "delete line, take it out of kill ring. bind this func to C-z" (interactive) (setq last-command 'delete-line) (kill-line) (setq kill-ring (cdr kill-ring)) (setq kill-ring-yank-pointer kill-ring) (setq last-command 'delete-line) ) (global-set-key (kbd "C-z") 'delete-line) ;; without setting of last-command 2+ C-zs mess up kill-ring

Solution 12 - Emacs

My answer to first question: mark the text in emacs (using either mouse or set mark C-SPC), and press "<- Backspace" button on keyboard instead of C-w. This way you can still paste over text in system clipboard, without worrying that the clipboard got overridden by text killed by C-w

Background for this answer: sometimes when I got text outside emacs that I want to use to replace a region in emacs, I often made a mistake by first copying that text into system clipboard (i.e. on Windows Ctrl + C) , then doing a C-w in emacs to "delete" the region of text I want to replace, with the hope that a later M-w could recover my text in clipboard from kill-ring. Unfortunately the clipboard would be simply overridden by text killed by C-w, and original message in clipboard would never show in kill-ring.

For 2nd question, yes you can always mark the text in emacs first and then directly C-y

Solution 13 - Emacs

As a complement to all answers. If you write elisp code to delete, you can call functions that kill as long as you use a local kill-ring object like this:

(defun delete-something()
  "Delete something without storing in kill ring."
  (let (kill-ring)
     (kill-something)))

Use any function that kill something in place of the kill-something. The function will then delete and nothing will be remembered in the real kill-ring.

Solution 14 - Emacs

Here's a version of kill-region that doesn't force values into the kill-ring.

(defun kill-or-delete-region (beg end prefix)
  "Delete the region, storing it in the kill-ring.
If a prefix argument is given, don't change the kill-ring."
  (interactive "r\nP")
  (if prefix
      (delete-region beg end)
    (kill-region beg end)))

(global-set-key (kbd "C-w") 'kill-or-delete-region)

This enables you to do C-w as before, but C-u C-w now deletes text without changing the kill-ring.

Solution 15 - Emacs

> how to delete text without kill ring?

the delete-region function will delete the selected region without adding it to the kill ring

> Also, is it possible to yank text directly instead of some text > without even pressing buttons to kill it?

I think you are asking how to replace the text in a selected region with something that you have already put on the kill ring.

Setting (delete-selection-mode 1) will allow you to yank/paste over a selected region. It also allows you to just type and replace a selected region.

On version 25.1.1 on OS X I also had to add (setq select-enable-primary nil) to prevent it from copying the selected region to the kill ring

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmannickenView Question on Stackoverflow
Solution 1 - EmacsashawleyView Answer on Stackoverflow
Solution 2 - EmacsEugene MorozovView Answer on Stackoverflow
Solution 3 - EmacsHaakon LøtveitView Answer on Stackoverflow
Solution 4 - EmacsJohn J. CamilleriView Answer on Stackoverflow
Solution 5 - EmacsEvgenyView Answer on Stackoverflow
Solution 6 - EmacsdanielpoeView Answer on Stackoverflow
Solution 7 - EmacsjpkottaView Answer on Stackoverflow
Solution 8 - EmacsOleView Answer on Stackoverflow
Solution 9 - EmacsPascalVKootenView Answer on Stackoverflow
Solution 10 - EmacsZBeginnerView Answer on Stackoverflow
Solution 11 - EmacsJames ColemanView Answer on Stackoverflow
Solution 12 - EmacsKevin ZhuView Answer on Stackoverflow
Solution 13 - EmacsPRouleauView Answer on Stackoverflow
Solution 14 - EmacsWilfred HughesView Answer on Stackoverflow
Solution 15 - EmacskevincaseyView Answer on Stackoverflow