I use clipboard-kill-region to copy contents from Emacs to other programs. But sometimes, it copies the same content twice into the clipboard. It looks like,


when I copy the filename


Is there a way I can empty the clipboard contents before calling clipboard-kill-region?

My complete function is shown here; it is a utility function to copy the absolute path of currently opened file in Emacs buffer:

(defun sk-copy-file-name ()
  "Put the current file name on the clipboard"
  (let ((filename (if (equal major-mode 'dired-mode)
    (when filename
        (insert filename)
        (clipboard-kill-region (point-min) (point-max)))
      (message filename))))

1 Answer 1


Use kill-new instead of clipboard-kill-region for this. clipboard-kill-region uses kill-region which will append the text to the last kill if the last call was kill-region.

(defun sk-copy-file-name ()
  "Put the current file name on the clipboard"
  (kill-new (or (buffer-file-name) default-directory)))
  • hi Jordan, it works with emacs gui but not with emacs terminal - emacs --no-window-system
    – Saravana
    Commented Jul 31, 2015 at 13:59
  • I believe you'll need to manually interface with the systems clipboard program, you can see how to do it on OSX here, emacs.stackexchange.com/questions/10900/… Commented Jul 31, 2015 at 14:35

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.