Emacs: how to delete text without kill ring?

后端 未结 15 1012
旧巷少年郎
旧巷少年郎 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:38

    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.

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

    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.

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

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

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

    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.

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

    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)))
    
    0 讨论(0)
提交回复
热议问题