I am creating a yasnippet to create org-mode link with content like: [[$1:value-read-from-clipboard][$0]]

I wish the $1 read value from clipboard (the use-case is: I copy the URL from chrome browser and use this yasnippet to automatically attach the URL string into a link in org file).

I am using evil-mode, so the clipboard value is saved in "* register.

Question is: 1.how to read clipboard value in yasnippet, perhaps by elisp code? 2.I am open to any better solution to achieve this use-case.


You can use backticks to run elisp code in snippets. Here's a full example snippet which works for me:

# -*- mode: snippet; require-final-newline: nil -*-
# name: yank
# key: yank
# --
[[${1:`(current-kill 0)`}][$0]]
  • have you had chance to test this? I received error like [[[yas] elisp error: Wrong type argument: listp, thttps://mail.google.com/mail/u/0/#inbox][]] – Xi Xiao Oct 1 '15 at 17:45
  • I tested it now and noticed that I forgot curly braces. I did not get an error though. I've updated the answer and also included a yasnippet header in order to get a full working example. – Erik Sjöstrand Oct 2 '15 at 9:33
  • Sorry, but your latest code I received this [[[yas] elisp error: Wrong type argument: listp, thttps://gitter.im/syl20bnr/spacemacs][]] the value in clipboard is https://gitter.im/syl20bnr/spacemacs so the yasnippet code expands correctly but there is kinda wired error in front... – Xi Xiao Oct 2 '15 at 12:29
  • I think you should use (current-kill 0) instead of (clipboard-yank); the backquotes tell yasnippet to insert whatever the expression returns, but clipboard-yank inserts the yanked string into the buffer and returns nil. – npostavs Oct 3 '15 at 18:38
  • Yes @npostavs you are correct. I wonder why clipboard-yank worked in my Emacs though... Anyway I have edited my answer with your solution. – Erik Sjöstrand Oct 5 '15 at 6:46

Install https://github.com/rolandwalker/simpleclip (check its README on how to install cli tools)

Then modify you snippet:

# -*- mode: snippet; require-final-newline: nil -*-
# name: yank
# key: yank
# --

This solution works always (Cygwin/Windows/Mac/Linux)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.