7

Many times I find myself in need of pasting a path from wherever to emacs' minibuffer. To clear the minibuffer fast I navigate to the beginning and do C-k (kill line).

This effectively overrides whatever path I had in the system clipboard with the temporary path I just killed in the minibuffer. Navigating the kill ring with M-y won't bring the path I had in the system clipboard.

Is there a way to delete the current line without killing it( i.e. removing it and adding it to the kill ring)?

So far I'm marking the line and pressing delete having delete-selection-mote active. I would like a one key solution similar to C-k.

octi
  • 1,470
  • 1
  • 17
  • 28
  • possible duplicate of [Emacs: how to delete text without kill ring?](http://stackoverflow.com/questions/637351/emacs-how-to-delete-text-without-kill-ring) – ataylor Dec 07 '11 at 21:09
  • Nah, he's asking about a specific subset of text deletion, i.e. lines, similar to C-k (kill rest of line). The other question is more generic. – Chris E Dec 07 '11 at 21:10
  • 1
    For this specific example, if the path is absolute (e.g. starts with `/` or `~`, or the likes of `C:/` for NTEmacs), then you may not need to delete what was already there, as Emacs can recognise the start of the new path and ignore the preceding text. This is certainly the case for `find-file` and, I presume, for anything which calls `read-file-name` – phils Dec 07 '11 at 21:37
  • Path can be absolute or relative. Point is to remove whatever is in the minibuffer altogether. Moreover, I wanted a general way to remove a line without appending it to the kill ring – octi Dec 07 '11 at 21:40
  • 2
    Have you considered simply moving to where you want to insert the text, pasting it in, then killing the remaining text? – Marcin Dec 07 '11 at 22:15

6 Answers6

9

As of Emacs 23.2, you can set save-interprogram-paste-before-kill to a non-nil value (hat tip Tyler) to copy the clipboard selection onto the kill ring, so that it is available via C-y M-y:

(setq save-interprogram-paste-before-kill t)

If you're on an older Emacs, the following advice has the same functionality:

(defadvice kill-new (before kill-new-push-xselection-on-kill-ring activate)
  "Before putting new kill onto the kill-ring, add the clipboard/external selection to the kill ring"
  (let ((have-paste (and interprogram-paste-function
                         (funcall interprogram-paste-function))))
    (when have-paste (push have-paste kill-ring))))

And, you could do something like this (horrible keybinding, customize to suit) to delete the line from the point forward:

(define-key minibuffer-local-map (kbd "C-S-d") 'delete-line)
(defun delete-line (&optional arg)
  (interactive "P")
  ;; taken from kill-line
  (delete-region (point)
                 ;; It is better to move point to the other end of the kill
                 ;; before killing.  That way, in a read-only buffer, point
                 ;; moves across the text that is copied to the kill ring.
                 ;; The choice has no effect on undo now that undo records
                 ;; the value of point from before the command was run.
                 (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))))
Community
  • 1
  • 1
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229
4

As of Emacs 23.2, this problem can be addressed with save-interprogram-paste-before-kill. If you set this variable to t then stuff in the clipboard gets added to the kill-ring, and isn't discarded by your next kill.

The documentation:

Save clipboard strings into kill ring before replacing them. When one selects something in another program to paste it into Emacs, but kills something in Emacs before actually pasting it, this selection is gone unless this variable is non-nil, in which case the other program's selection is saved in the `kill-ring' before the Emacs kill and one can still paste it using C-y M-y.

Tyler
  • 9,872
  • 2
  • 33
  • 57
1

From Xahlee's page, it shows several commands that are annoying.

(defun my-delete-word (arg)
  "Delete characters forward until encountering the end of a word.
With argument, do this that many times.
This command does not push erased text to kill-ring."
  (interactive "p")
  (delete-region (point) (progn (forward-word arg) (point))))

(defun my-backward-delete-word (arg)
  "Delete characters backward until encountering the beginning of a word.
With argument, do this that many times.
This command does not push erased text to kill-ring."
  (interactive "p")
  (my-delete-word (- arg)))

(defun my-delete-line ()
  "Delete text from current position to end of line char."
  (interactive)
  (delete-region
   (point)
   (save-excursion (move-end-of-line 1) (point)))
  (delete-char 1)
)

(defun my-delete-line-backward ()
  "Delete text between the beginning of the line to the cursor position."
  (interactive)
  (let (x1 x2)
    (setq x1 (point))
    (move-beginning-of-line 1)
    (setq x2 (point))
    (delete-region x1 x2)))

; Here's the code to bind them with emacs's default shortcut keys:

(global-set-key (kbd "M-d") 'my-delete-word)
(global-set-key (kbd "<M-backspace>") 'my-backward-delete-word)
(global-set-key (kbd "C-k") 'my-delete-line)
(global-set-key (kbd "C-S-k") 'my-delete-line-backward)
CodyChan
  • 1,776
  • 22
  • 35
0

Found an answer to this.

Posted it first here: https://unix.stackexchange.com/questions/26360/emacs-deleting-a-line-without-sending-it-to-the-kill-ring/136581#136581

;; 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)
Community
  • 1
  • 1
Ole
  • 487
  • 7
  • 20
0

There isn't.

from the GNU Emacs Manual:

We have already described the basic deletion commands C-d (delete-char) and (delete-backward-char). See Erasing.

The other delete commands are those that delete only whitespace characters: spaces, tabs and newlines. M-\ (delete-horizontal-space) deletes all the spaces and tab characters before and after point. With a prefix argument, this only deletes spaces and tab characters before point. M- (just-one-space) does likewise but leaves a single space after point, regardless of the number of spaces that existed previously (even if there were none before). With a numeric argument n, it leaves n spaces after point.

Chris E
  • 973
  • 13
  • 26
  • Anyone who has a macro which moves to the end of the line, activates the mark, moves to the beginning of the line then clears the selection?:P – octi Dec 07 '11 at 21:12
0

What about something like:

(defun del-line (p1)
  (interactive "d")
  (move-end-of-line 1)
  (when (eq p1 (point)) ; special case when p1 is already at the end of the line
     (forward-line))
  (delete-region p1 (point)))

The behavior should be similar to C-k but without affecting the system clipboard or the kill-ring.

ETA: I read Trey's solution more carefully, and it looks like this is just a simple case of his solution. It worked in my (very!) limited tests, but probably fails for some special cases where the more complicated kill-line code works correctly.

Asher L.
  • 1,297
  • 11
  • 10