Skip to content

Instantly share code, notes, and snippets.

@vvvvalvalval
Last active November 7, 2018 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vvvvalvalval/2ba72b7f05b82ab94da960c7d1f46e5c to your computer and use it in GitHub Desktop.
Save vvvvalvalval/2ba72b7f05b82ab94da960c7d1f46e5c to your computer and use it in GitHub Desktop.
REPLCP - Copy last REPL output

REPLCP

Small dev utility for copying REPL output to the clipboard.

Installation

Via deps.edn, typically in an alias:

{:deps
 ;; ...
 :aliases
 {:dev
  {:extra-deps
   {;; ...
    replcp {:git/url "https://gist.github.com/vvvvalvalval/2ba72b7f05b82ab94da960c7d1f46e5c"
            :sha "f842d9cca8cfcc4df8789c628e2577f6d1778461"}}}}}

Usage

;; require the namespace
(require 'replcp)

;; evaluate an expression at the REPL
(into []
  (map (fn [n] {:a "foo" :b n :c (range n)}))
  (range 20))

;; Call replcp/cp
(replcp/cp)

;; a pretty-printed representation of the last value has just been copied to the clipboard

Tips

Don't forget to require the replcp namespace

E.g by require-ing it from a dev namespace.

To customize, fork this repo

E.g to re-implement default-pprint-str with something else than the Fipp library, or to use other Fipp options.

Create an editor shortcut for evaluating (replcp/cp)

E.g an Emacs keybinding, or a Cursive Live Template or REPL Command.

Caveats

For this to work, the REPL must be running on the same machine where your clipboard is!

{:deps
{org.clojure/clojure {:mvn/version "1.8.0"}
fipp {:mvn/version "0.6.14"}}
:paths [""]}
(ns replcp
(:require [fipp.edn])
(:import
(java.awt Toolkit)
(java.awt.datatransfer StringSelection)))
(def fipp-opts
{})
(defn default-pprint-str
[v]
(with-out-str (fipp.edn/pprint v fipp-opts)))
(defn cp
"Copies the supplied value to the Clipboard.
When no value is supplied, defaults to *1 (last value in the REPL).
An optional function for printing values to strings may be supplied."
([] (cp *1))
([v] (cp v default-pprint-str))
([v stringify]
(-> (Toolkit/getDefaultToolkit)
.getSystemClipboard
(.setContents
(StringSelection. (stringify v))
nil))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment