I use the terminal a lot and aliases are a great way to both personalise my command line experience, but also to make some tasks a little easier (and sometimes, smarter π).
[](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)
This isn’t rocket science, but I’ve got special aliases for my copy and paste commands. Since I use a Mac, the command line paste command is pbpaste
, whichβ¦well doesn’t immediately sprint to mind. Equally, copy is pbcopy
and quite often I want to copy to my clipboard but also see what was copied.
There’s just one addition that I like to add to the copy command: I find it useful to also copy the contents of files occasionally. So copy
for me is actually a function:
$ # make copy a function that checks whether there's an
# argument being passed, and if so, cat the file and pipe
# through copy. otherwise, pipe stdin into copy, then
# finally paste to stdout
copy() {
if [ -t 0 ]; then
cat $@ | pbcopy
else
pbcopy < /dev/stdin
fi
pbpaste
}
# and now alias paste to pbpaste, because gosh darnit!
alias paste=pbpaste
Now I can pipe commands to copy or pass it a filename:
$ ps | copy
# copies and shows output from `ps`
$ copy blog-post.md
# copies and shows contents of `blog-post.md` via `cat`
Caution: there is an [existing paste
command](https://en.m.wikipedia.org/wiki/Paste_\(Unix\)) which this will overwrite. If you want to invoke the original command use command paste
.
I hope that’s useful. If you want to learn more, check out my [terminal.training](https://terminal.training/?utm_source=blog\&utm_medium=link\&utm_campaign=blog-post) course.
Published 25-Apr 2018 under #code. [Edit this post](https://github.com/remy/remysharp.com/blob/main/public/blog/how-i-copy-paste-in-the-terminal.md)