6

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 '11 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. – Amelio Vazquez-Reina Dec 8 '11 at 23:40
4
(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. – Marcus Junius Brutus May 30 '14 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 '14 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 '14 at 12:26
3

cinsk answer did not work for me on emacs 24.

But this did:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.