Emacs: how to delete text without kill ring?

后端 未结 15 1014
旧巷少年郎
旧巷少年郎 2020-12-04 14:00

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 with

相关标签:
15条回答
  • 2020-12-04 14:46

    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

    0 讨论(0)
  • 2020-12-04 14:53

    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)))
    
    0 讨论(0)
  • 2020-12-04 14:55

    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.

    0 讨论(0)
  • 2020-12-04 14:57

    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.

    0 讨论(0)
  • 2020-12-04 14:58

    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

    0 讨论(0)
  • 2020-12-04 14:58

    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.

    0 讨论(0)
提交回复
热议问题