0

I'm using Emacs to create text to copy to another program, but I need to copy line by line. I'm using a variation of kill-ring-save I provide below.

(defun my-kill-ring-save ()
    (interactive)
    (if (equal mark-active nil) (kill-ring-save (point) (line-end-position)) (kill-ring-save (point) (mark))))
(global-set-key "\M-w" 'my-kill-ring-save)

So I would do this:

  1. M-w to copy the line
  2. Alt-Tab to go to the other program
  3. Ctrl-v to paste the line
  4. Alt-Tab to go back to Emacs
  5. C-k two times (delete the current line, and again so the next line moves up)
  6. Repeat the process

The problem is that when I hit Ctrl-v in the other program sometimes it yanks not only the line I want, but other previously deleted lines. I read that the paste command should only paste the last line in the kill ring, thats what I would like it to do. This problem only happens (as far as I know) If I C-k two times fast and right after copying the line with M-w. If I kill lines with C-k, wait for 3 seconds, I'm able to M-w the current line and paste just this line. Sorry if it seems like a exageration of a small problem, but I'm actually doing this process a lot of times so any solution would be great for me. I couldn't find any related problem about this specficic issue.

EDIT: the solution below unfortunately didn't work or I couldn't use it correctly, appreciate any update on this, thanks!

1 Answer 1

1

kill-ring-save is for interactive use. To delete and store in kill-ring:

(defun my-kill-ring-save-this-line () (interactive "*") (kill-new (buffer-substring-no-properties (line-beginning-position) (line-end-position)))

or if the properties are needed

(defun my-kill-ring-save-prop-this-line () (interactive "*") (kill-new (buffer-substring (line-beginning-position) (line-end-position)))

Delete a line but not store:

(defun my-delete-line-this-line () (interactive "*") (delete-region (line-beginning-position) (line-end-position))

4
  • (defun kill-new () (buffer-substring-no-properties (line-beginning-position) (line-end-position))) (global-set-key (kbd "M-w") 'kill-new) This gives me error command-execute: Wrong type of argument : commandp, kill-new Commented Dec 19, 2017 at 20:28
  • kill-new is provided by Emacs, installing it anew as defun would overwrite i.e. destroy the original. Write the interactive spec as you did in OP, than the body as provided in answer. Commented Dec 20, 2017 at 9:04
  • Edit the answer, all there meanwhile. Commented Dec 20, 2017 at 9:12
  • 1
    Still added the warnings if buffer is read-only. Commented Dec 20, 2017 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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