Powerful emacs hacks: paste images to markdown from clipboard
Why do I love emacs? Let me show you.
Problem
- I want to add images to my blog.
- I write them in markdown in emacs
- I often take screenshots on mac which saves it to clipboard (or want to paste images from my clipboard manager)
- I want to paste those screenshots into a specific directory based on the file I am editing
- If I am editing
my-new-blog-post.md
I want the images to be saved from clipboard ->../media/my-new-blog-post-1.png
,../media/my-new-blog-post-2.png
and so on.
✨ The Emacs Magic
I need to spend no time messing around with plugins, or extensions, or fancy editors, or shortcuts, or automations.
I go straight to my trusted emacs lisp code, and hack something up with the help of llm’s:
(defun genox/screenshot-from-clipboard ()
"Save clipboard image to a uniquely named file in ../media/
relative to current file, and insert a markdown image link."
(interactive)
(let* ((basename (file-name-base (buffer-file-name)))
(media-dir (expand-file-name "../media/" (file-name-directory (buffer-file-name))))
(counter 1)
filename)
;; Ensure media directory exists
(unless (file-exists-p media-dir)
(make-directory media-dir t))
;; Find a unique filename
(setq filename (expand-file-name (format "%s-%d.png" basename counter) media-dir))
(while (file-exists-p filename)
(setq counter (1+ counter))
(setq filename (expand-file-name (format "%s-%d.png" basename counter) media-dir)))
;; Save clipboard image using pngpaste
(if (eq system-type 'darwin)
(call-process "pngpaste" nil nil nil filename)
(error "Clipboard image capture only supported on macOS with pngpaste"))
;; Insert markdown image link
;; Insert markdown image link with path starting from /media/
(when (file-exists-p filename)
(insert (format "" (file-name-nondirectory filename))))
(markdown-display-inline-images)
(newline)))
Save the above function in my init.el
emacs configuration file. Evaluate it. Switch to my markdown buffer. Hit M-x genox/screenshot-from-clipboard
and it saves the image and pastes this:

which then auto expands to an inline image using markdown-display-inline-images
. Magic within minutes!! No messing around with Obsidian plugins, or creating custom extensions. Code -> eval -> test -> [blog]? -> move on.
See my previous blog post on how I get emacs to understand where to find /media/my-new-blog-post-1.png
relative to my file.