I would like to use C-k to delete a line without sending it to the kill-ring.

I have the following on my .emacs file

(delete-selection-mode 1)

but this only seems to work for C-d (delete-char)

I have also read the solutions described in this thread: Emacs: how to delete text without kill ring?, but I didn't see anything that addresses this precise problem.

  • Why does it matter if it goes to the kill ring?
    – psusi
    Dec 8, 2011 at 23:30
  • 1
    @psusi: Quite often I copy some text and then kill several lines one by one before yanking. When yanking, I don't remember how many lines I killed, and cycling through the kill ring to find the text I want to yank can be distracting. Either way, I'm also interested in learning how this can be done. Dec 8, 2011 at 23:40

3 Answers 3

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

Maybe this is not the best solution, but it seems works. You may need to bind `delete-line' to some global key, such as

(global-set-key [(control shift ?k)] 'delete-line)
  • what exactly is (control shift ?k) ? I copied the line verbatim in my .emacs file and tried using the CTRL+SHIFT+k keystroke and it didn't seem to work. May 30, 2014 at 20:10
  • Depending on the window-system (esp. terminal), control+shift+k may not work. To test for specific key-binding is available, use C-H k, then type the keybinding whether it works.
    – cinsk
    Jun 9, 2014 at 1:05
  • this kills only to the end-of-line. How about killing the whole line, but keeping the current cursor column?
    – towi
    Nov 17, 2014 at 12:26

cinsk answer did not work for me on emacs 24.

But this did:

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

The approach I follow is to just rewrite kill-line to use delete-region instead of kill-region. The functions kill-region and delete-region are almost the same. The big different is that the former saves what's deleted in the kill ring. The latter doesn't. Rewriting the function with this replacement retains the exact behavior of kill-line without any side effects.

(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

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")
     (if arg
         (forward-visible-line (prefix-numeric-value arg))
       (if (eobp)
           (signal 'end-of-buffer nil))
       (let ((end
                (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))))

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .