I’ve been tinkering with my [dotfiles](https://github.com/remy/dotfiles) a lot recently, and wanted to show how I’m creating gists on the CLI these days.
[](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[READER DISCOUNTSave $50 on terminal.training](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[I’ve published 38 videos for new developers, designers, UX, UI, product owners and anyone who needs to conquer the command line today.](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
[$49 - only from this link](https://training.leftlogic.com/buy/terminal/cli2?coupon=BLOG\&utm_source=blog\&utm_medium=banner\&utm_campaign=remysharp-discount)
First, you’ll need to install the gist
application using brew install gist
, then you’re going to add a new function
to your shell via your .zshrc
(or .bashrc
or whatever flavour you enjoy).
All newly created gists on the CLI will make use of the following flags:
-
create all gists as secret (
-p
) -
copy the gist url to the clipboard (
-c
) -
open the browser to the gist (
-o
)
The function that we create will extend the brew installed gist
applications by supporting three modes for the CLI tool:
-
$ gist filename.json
— create a gist from thefilename.json
with the namefilename.json
-
$ cat filename.json | gist
– create a new gist fromSTDIN
-
$ gist
– paste whatever’s on the clipboard and create a new gist calledpaste.txt
This last one is fun and pretty much the reason I made this function. I tend to have something on my clipboard that I want to quickly share. I can now just type gist
into my terminal and I’ll make a gist, show me the page (so I get visual confirmation it worked) and it’s already copied to my clipboard.
gist function[](#gist-function)
Copy and paste this bash function into your profile and you’ll have the extended functionality that I described above.
$ function gist() {
# if there's nothing piped on STDIN
if [ -t 0 ]; then
# and there's no arguments...
if ((! $# )); then
# take what's on the clipboard and paste it in a new gist
command gist -Pcop -f paste.txt
else
# create a gist based on the arguments give
command gist -cop $@
fi
else
# otherwise, create a gist, with arguments, but use the
# content from STDIN
command gist -cop $@ < /dev/stdin
fi
}
Hope that’s useful. In fact, I’ve since evolved this script to pipe the output directly to the [gitio command I have](https://github.com/remy/dotfiles/blob/93f4390655db3930c5cde5b1f998dc8d0596a17a/.functions#L36-L50) so I get short URL too!
Published 1-Dec 2016 under #web. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/smarter-cli-gists.md)
Comments
Lock Thread
Login
Add Comment[M ↓ Markdown]()
[Upvotes]()[Newest]()[Oldest]()

rem
0 points
7 years ago
Ugh! You’re completely right. I’ll remove it. Thanks.
[Commento](https://commento.io)