5

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.

1

3 Answers 3

6

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]]
6
  • 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
    Commented Oct 1, 2015 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. Commented Oct 2, 2015 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
    Commented Oct 2, 2015 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
    Commented Oct 3, 2015 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. Commented Oct 5, 2015 at 6:46
0

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
# --
[[${1:`(simpleclip-get-contents)`}][$0]]

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

0

This version works with fontified text and does not move the kill ring:

# -*- mode: snippet; require-final-newline: nil -*-
# name: yank
# key: yank
# --
[[${1:`(substring-no-properties (current-kill 0 t))`}][$0]]

Explanation:

(substring-no-properties (current-kill 0 t))
  • substring-no-properties lets you get just the raw string, e.g. just "copied text" instead of #("copied text" 0 11 (fontified t face font-lock-comment-face))
  • current_kill has an optional argument DO-NOT-MOVE:

    If optional arg DO-NOT-MOVE is non-nil, then don’t actually move the yanking point; just return the Nth kill forward.

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.