Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Copy Emacs File Names To The Mac Clipboard

** TL; DR

This copies the name of the current Emacs buffer to the Mac pasteboard.

Code

(defun copy-buffer-name-to-osx-clipboard ()
  (interactive)
  (let ((process-connection-type nil))
    (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
      (process-send-string proc (buffer-file-name))
      (process-send-eof proc))))

I load that in my init files and set a hotkey to call it with this:

Code

(global-set-key (kbd "C-M-!") 'copy-buffer-name-to-osx-clipboard)

The original version that just does a copy paste to the pasteboard of whatever is selected came from [[https://gist.github.com/the-kenny/267162][this gist]]. For completeness, it looks like this:

Code

(defun copy-from-osx ()
    (shell-command-to-string "pbpaste"))
  
  (defun paste-to-osx (text &optional push)
    (let ((process-connection-type nil))
      (let ((proc (start-process "pbcopy" "*Messages*" "pbcopy")))
        (process-send-string proc text)
        (process-send-eof proc))))
  
  (setq interprogram-cut-function 'paste-to-osx)
  (setq interprogram-paste-function 'copy-from-osx))

I use the evil vim binding so I don't need that version. I also don't totally understand how it works. I just know it does.

Reference:

https://gist.github.com/the-kenny/267162

https://emacs.stackexchange.com/questions/10900/copy-text-from-emacs-to-os-x-clipboard