Emacs:删除一行而不将其发送到杀戮环

Emacs:删除一行而不将其发送到杀戮环

我想用来C-k删除一行而不将其发送到kill-ring.

.emacs我的文件中有以下内容

(delete-selection-mode 1)

但这似乎只适用于C-d( delete-char)

我还阅读了此线程中描述的解决方案:Emacs:如何在没有杀环的情况下删除文本?,但我没有看到任何可以解决这个精确问题的内容。

答案1

(defun delete-line (&optional arg)
  (interactive "P")
  (flet ((kill-region (begin end)
                      (delete-region begin end)))
    (kill-line arg)))

也许这不是最好的解决方案,但它似乎有效。您可能需要将“delete-line”绑定到某个全局键,例如

(global-set-key [(control shift ?k)] 'delete-line)

答案2

cinsk 答案在 emacs 24 上对我不起作用。

但这做到了:

;; 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)

答案3

我遵循的方法是重写kill-line以使用delete-region而不是kill-region.功能kill-regiondelete-region几乎是一样的。最大的不同是前者保存删除环中删除的内容。后者则不然。使用此替换重写函数保留了确切的行为,而kill-line没有任何副作用。

(defun my/kill-line (&optional arg)
  "Delete the rest of the current line; if no nonblanks there, delete thru newline.
With prefix argument ARG, delete that many lines from point.
Negative arguments delete lines backward.
With zero argument, delete the text before point on the current line.

When calling from a program, nil means \"no arg\",
a number counts as a prefix arg.

If `show-trailing-whitespace' is non-nil, this command will just
delete the rest of the current line, even if there are no nonblanks
there.

If option `kill-whole-line' is non-nil, then this command deletes the whole line
including its terminating newline, when used at the beginning of a line
with no argument.

If the buffer is read-only, Emacs will beep and refrain from deleting
the line."
  (interactive "P")
  (delete-region
   (point)
   (progn
     (if arg
         (forward-visible-line (prefix-numeric-value arg))
       (if (eobp)
           (signal 'end-of-buffer nil))
       (let ((end
              (save-excursion
                (end-of-visible-line) (point))))
         (if (or (save-excursion
                   ;; If trailing whitespace is visible,
                   ;; don't treat it as nothing.
                   (unless show-trailing-whitespace
                     (skip-chars-forward " \t" end))
                   (= (point) end))
                 (and kill-whole-line (bolp)))
             (forward-visible-line 1)
           (goto-char end))))
     (point))))

(global-set-key (kbd "C-k") 'my/kill-line)

相关内容