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.

Dan
  • 32,980
  • 7
  • 102
  • 169
Xi Xiao
  • 533
  • 4
  • 11

3 Answers3

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]]
Erik Sjöstrand
  • 826
  • 4
  • 15
  • 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 01 '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 02 '15 at 09: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 02 '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 03 '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 05 '15 at 06:46
  • wow, I didnt expect there is a viable solution, but here it is! thank you both above! – Xi Xiao Nov 04 '15 at 08:50
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)

chen bin
  • 4,857
  • 20
  • 36
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.

Preminster
  • 296
  • 2
  • 6