0

This code copies the current line:

    (defun copy-line ()                                                                                                  
  "Copy the whole line that point is on."                                                                            
  (interactive)                                                                                                      
  (let ((beg (line-beginning-position 1))                                                                            
        (end (line-beginning-position 2)))                                                                           
    (if (eq last-command 'quick-copy-line)                                                                           
        (kill-append (buffer-substring beg end) (< end beg))                                                         
      (kill-new (buffer-substring beg end)))))  

, but this one also uses kill, which marks the buffer as changed.

Can we "copy", so that the buffer remains unchanged?

1 Answer 1

3

Use the function copy-region-as-kill instead. Here are some highlights from the docstring:

(copy-region-as-kill BEG END &optional REGION)

  Probably introduced at or before Emacs version 19.29.

Save the region as if killed, but don’t kill it.

...

When called from Lisp, save in the kill ring the stretch of text
between BEG and END, unless the optional argument REGION is
non-nil, in which case ignore BEG and END, and save the current
region instead.

And here's a quick one-liner that does it:

(defun copy-line ()
  (interactive)
  (copy-region-as-kill (line-beginning-position 1)
                       (line-end-position 1)))

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.