Elisp code: copy full path of current file into OS clipboard and yank ring

  |   Source

Copy below code into ~/.emacs. The hot keys is "C-x v f":

;; you need install xsel under Linux
;; xclip has some problem when copying under Linux

(defun copy-yank-str (msg)
  (kill-new msg)
  (with-temp-buffer
    (insert msg)
    (shell-command-on-region (point-min) (point-max)
                             (cond
                              ((eq system-type 'cygwin) "putclip")
                              ((eq system-type 'darwin) "pbcopy")
                              (t "xsel -ib")
                              ))))


(defun copy-full-path-of-current-buffer ()
  "copy full path into the yank ring and OS clipboard"
  (interactive)
  (when buffer-file-name
    (kill-new (file-truename buffer-file-name))
    (copy-yank-str (file-truename buffer-file-name))
    (message "full path of current buffer => clipboard & yank ring")
    ))


(global-set-key (kbd "C-x v f") 'copy-full-path-of-current-buffer)

Comments powered by Disqus