11 ms·
`pbcopy` and `pbpaste` are one of my most-loved in the list. Dealing with some minified json, switching to iTerm, doing `pbpaste | json_pp | pbcopy` and having
by klausa 3y ago
`pbcopy` and `pbpaste` are one of my most-loved in the list.
Dealing with some minified json, switching to iTerm, doing `pbpaste | json_pp | pbcopy` and having a clean output is _so_ nice.
- eddieroger 3y agoI don't remember when I learned about these, but they've been game changers, and everyone I've shared them with feels the same way. I use your use case often as well, though through `jq` because I'm more familiar with it, and sometimes wish to do transforms.
- nerdponx 3y agoYou can use `python -m json.tool` for just JSON formatting, which is convenient now that Python is available by default in most Linux distros. Jq is really excellent though.
- kps 3y agoOn Linux I have these wrap xsel or xclip, and likewise open to xdg-open. Now, for your Mac example — if that's a specific pipeline you often use, you can write a Service menu entry to do it in place, without switching to a terminal.
- andelink 3y ago+1 to the service menu actions. They are so handy, but often forgotten/overlooked. I think maybe a discoverability issue.
- pmarreck 3y agoI have linux/macos-agnostic bash functions in my dotfiles that unify this to “clip” and “paste” (since “copy” is too close semantically to “cp”)
- kps 3y agopaste(1) is a POSIX standard utility, though (going back to System III), pairing with cut(1). https://pubs.opengroup.org/onlinepubs/9699919799/utilities/paste.html https://pubs.opengroup.org/onlinepubs/9699919799/utilities/p...
- vram22 3y agoThe Unix join command is also useful: https://en.m.wikipedia.org/wiki/Join_(Unix) https://en.m.wikipedia.org/wiki/Join_(Unix)
- svieira 3y agoAnd I have one that unifies _both_ to `clip` so you can put the same command in both sides of the pipe, e. g. to turn a line-delimited blob on your clipboard to a space-separated one: clip | tr '\n' ' ' | clip https://github.com/svieira/dotfiles/blob/a3654d6a194e36899787aa7850f4a9a23617c64f/functions#L136-L141 https://github.com/svieira/dotfiles/blob/a3654d6a194e3689978... # Use clipboard in shell pipelines # clip | xargs echo # uses pbpaste # ps -A | grep search | clip # uses pbcopy clip() { [ -t 0 ] && pbpaste || pbcopy }
- pmarreck 3y agoHoly crap. Of course! You win! Amazing! Simple is genius
- pmarreck 3y agoSo to check if there's anything sitting on stdin without reading it I've been using `if read -r -t0; then` # returns true if there is data but times out instantly so it doesn't consume any Is `[ -t 0 ]` more idiomatic? Apparently it fails on this case: function < file `read -r -t0` is Bash-only though and not POSIX, but it will work regardless of what type of data is on stdin
- naniwaduni 3y ago[ -t 0 ] instead checks whether stdin (fd 0) is a tty.
- inanutshellus 3y agoI love this flow! Such a powerful and clean way to solve text issues. # This will remove Windows double-spaced empty lines from your copy/paste buffer alias winlines="sed '/^$/{$!{N;s/\n//;};}'" # pbw = [P]aste [B]uffer to fix [W]indows line endings alias pbw="pbpaste | winlines | pbcopy" Also - if you want `pbpaste` and `pbcopy` on Linux... # imitate MacOS's paste buffer copy/paste: alias pbcopy='xsel --clipboard --input' alias pbpaste='xsel --clipboard --output'
- computerfriend 3y agoHere's the xclip way (almost the same actually). alias pbcopy="xclip -selection clipboard" alias pbpaste="xclip -selection clipboard -o"
- lordgrenville 3y agoI use this, and another Mac affordance I copy in Linux is alias open="xdg-open"
- lozf 3y agoThere's `wl-copy` and `wl-paste` for Wayland users too, via https://github.com/bugaevc/wl-clipboard https://github.com/bugaevc/wl-clipboard
- netr0ute 3y agoAnd `cb` which works cross-platform, via https://github.com/Slackadays/clipboard https://github.com/Slackadays/clipboard
- iuafhiuah 3y agoI find it so annoying that these only work with plain text and RTF. On X11 there is `xclip`[0] and on Wayland there is `wl-clipboard`[1] both of which support binary file formats either through parsing the header or explicitly setting the MIME type. This means you can do things like copy an image from the terminal and paste it into a graphical program like a browser or chat client and vice-versa. Also can be very useful in shell scripts for desktop automation. The workaround on MacOS is to use AppleScript via `osascript` to `set the clipboard to...`. [0] https://github.com/astrand/xclip [1] https://github.com/bugaevc/wl-clipboard
- ksala_ 3y agoSince you mention both pbcopy and iTerm - I love https://github.com/skaji/remote-pbcopy-iterm2 https://github.com/skaji/remote-pbcopy-iterm2. I do most of the work on a remove Linux server, treating my MacBook as mostly a dumb terminal, and being able to transparently copy from the remove to my local clipboard is so nice.
- whartung 3y agoI’ll have to try that. More than once I’ve been logged in to a remote host and got “pbcopy not found” “What!?… oh, right.“
- tstack 3y agoThe tmux integration in iterm is also very nice for remote work if you haven’t tried it out.
- ksala_ 3y agoI have tried it, but for whatever reason I just don't like it. I prefer just running tmux in iTerm with no integration. On the topic, you can also integrate tmux with the native clipboard - I have set copy-pipe to the remote pbcopy, so any selection done in tmux get copied to my local clipboard. I also just found out that tmux also support it natively (https://github.com/tmux/tmux/wiki/Clipboard#the-set-clipboard-option https://github.com/tmux/tmux/wiki/Clipboard#the-set-clipboar...).
- omginternets 3y agoIf I had a nickel for each `cat foo.json | jq | pbcopy`, I'd be a rich man :)
- maleldil 3y agoThat's a useless use of cat. You can use `jq . foo.json | pbcopy` or `jq < foo.json | pbcopy`.
- jdbartee 3y agoSpeaking for myself, the first form is more natural- even if it’s a useless cat, because I’m always cat-ing files to see their structure. Then progressively tacking on different transforms. And then finally putting it in whatever I want as output. It’s so ingrained, I’m more likely than not to just write it out that way even when I know exactly what I’m doing from the onset.
- paulddraper 3y agoYou could consider < foo.json jq | pbcopy
- jonnycomputer 3y agoYes, this iterative procedure is often why "useless" cats get put into it. It's a very effective way of processing regular text information. e.g. I need to grab some info from textfile.txt to use as arguments to a function. cat textfile.txt looks like its comma delimited. cat textfile.txt | cut -d, -f 2-5 ah, its the third and fourth column i need cat textfile.txt | cut -d, -f 3-4 | grep '123456' perfect cat textfile.txt | cut -d, -f 3-4 | grep 123456 | tr , ' ' myfunc $(cat textfile.txt | cut -d, -f 3-4 | grep 123456 | tr , ' ')
- gumby 3y ago> cat textfile.txt > looks like its comma delimited. Interesting; why wouldn't you use `head`? Who knows how big textfile.txt is?
- zimpenfish 3y agoExtra handy when combined with `piknik`[1] for distributed/cross-Apple account clipboard shenanigans. [1] https://github.com/jedisct1/piknik https://github.com/jedisct1/piknik
- agmm 3y agoI like to use `pbcopy` when exporting public keys to external services like GitHub. `cat ~/.ssh/mykey.pub | pbcopy`
- _rend 3y agoYou can even simplify this further by feeding `pbcopy` the key directly using file redirection instead of a pipe: `pbcopy < ~/.ssh/mykey.pub` (I use this all the time myself!)
- wincy 3y agoI love this tool too! except one time I quickly typed `cat ~/.ssh/mykey | pbcopy` And sent it straight away to my coworker on Slack. I then spent the rest of the day making a new private key and adding my new pubkey to all of the 1000+ servers I had root access to. I mean we had tools to help but it still wasn’t fun. With great power/convenience comes the potential to do dumb things at lightning speeds!
- oxygen_crisis 3y agoI might start naming my private key files ~/.ssh/keyname.PRIVATE after hearing that story...
- plorkyeran 3y agoThat's not a bad idea. I've never actually made the same mistake, but I have caught it at the last moment and having tab complete not pick the private one first would help.
- wincy 3y agoIt would have avoided it! I was using tab and forgot to select .pub as you correctly surmised. I was a junior dev at the time and all the seniors got a good laugh out of it, and I use it as a cautionary tale about trying to be TOO overeager and efficient.
- gunapologist99 3y ago
- itslennysfault 3y agoWhere are you pasting the pretty json to view it? I do this a lot as well, but just paste the minified json directly into VS Code and then OPT+SHIFT+F to format it.
- nicky0 3y ago`jq` acts as a pretty json viewer (among other things)
- nine_k 3y agoThose are so useful that I wrote trivial shell functions that do the same under Linux.
- timf 3y agoSince I'm bouncing between OSX and Linux a lot, I have a shell script with the same name on each that boils down to: if [ `uname` == "Darwin" ]; then pbcopy else xsel --clipboard fi
- jonnycomputer 3y agoYes. I use it a great deal, but I haven't gotten used to using the linux equivalents. I guess that would be either xsel or xclip. Maybe I should create a "pbcopy" that runs one of those. I like to minimize the cognitive load when I switch between mac and linux command line environments.
- lloeki 3y agoAnother frequent use I have, applying random diffs with git: git diff | pbcopy pbpaste | git apply
- js2 3y agoYou can also use `git format-patch` and `git am` if you want to apply the same commit to multiple repos, a use-case I sometimes I have.
- verst 3y agoI should use `git format-patch` instead of creating a mock draft PR (which I end up deleting) and modifying the URL to add `.patch` and then downloading the patch file haha. `git format-patch` would probably be faster :)
- cancerhacker 3y agoalias pbg='pbpaste | fgrep --color -i "`pbpaste -pboard find`"' select all in a terminal window with pages of log data and cmd-c copy; find the one phrase you want to find in that data and cmd-e to put it in the find pasteboard; cmd-n new window, type pbg to isolate the log lines.
- philsnow 3y agoTIL about named pasteboards https://developer.apple.com/documentation/appkit/nspasteboard/name/2870215-find https://developer.apple.com/documentation/appkit/nspasteboar... I recognize that your pbg alias works for pretty much any text you could copy, but I wanted to mention, in case you're looking at log files with plain old less, there's the & limiter, which limits the current view to only lines matching a regular expression (or, if you type ^R during a & prompt, for a text match). If you type ^N or ! during a & prompt it will limit the view to those lines that do not match the expression. These view limits stack, so you can "&WARN<enter>" to see all lines that have WARN in them, and then maybe you want to see just a certain PID so "&12345<enter>" and you'll only see lines with both WARN and 12345, but then that one module is printing out a bunch of messages you think are safe to ignore so you do "&!modulename<enter>" and it filters out log lines that match modulename. Very handy and less is everywhere.
- lkbm 3y agoOh man. I recently threw together a "j2p" script to make converting between json and python dicts simpler, and combining it with pbcopy/pbpaste will make it so much better: #!/usr/bin/env python3 import sys import json print(json.load(sys.stdin))
- atoav 3y agoOr directly from the commandline: pbcopy | python -c 'import sys; import json; print(json.load(sys.stdin))' | pbpaste
- tomviner 3y agoThe Python json library is callable as a module: pbcopy | python -m json.tool | pbpaste It has a load options: $ python -m json.tool --help usage: python -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines] [--indent INDENT | --tab | --no-indent | --compact] [infile] [outfile] A simple command line interface for json module to validate and pretty-print JSON objects. positional arguments: infile a JSON file to be validated or pretty-printed outfile write the output of infile to outfile options: -h, --help show this help message and exit --sort-keys sort the output of dictionaries alphabetically by key --no-ensure-ascii disable escaping of non-ASCII characters --json-lines parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid JSON Lines output. --indent INDENT separate items with newlines and use this number of spaces for indentation --tab separate items with newlines and use tabs for indentation --no-indent separate items with spaces rather than newlines --compact suppress all whitespace separation (most compact)
- mig39 3y agoIt's cool to use pbcopy and pbpaste with your phone! Copy some text on the phone, and you can pbpaste it onto the Mac command line. So cool.
- nojs 3y agoIt creeps me out when the clipboard is unexpectedly shared between my phone and computer. And since the feature seems to turn on randomly but not reliably when I want it to, I’d rather it just didn’t exist.
- emmjay_ 3y agoDisabling Handoff is currently the only way to disable Universal Clipboard. * Mac: Go to System Preferences > General > uncheck Allow Handoff. * iPhone: Go to Settings > General > Handoff > uncheck Handoff.
- pjot 3y agoMake sure both your phone and computer have Wi-Fi and Bluetooth turned on.
- MikeTheGreat 3y agoI'm curious why the Bluetooth is required? Requiring WiFi makes (so the phone/computer is on a network and can communicate with the other devices), but what's the benefit of Bluetooth? Does it only work when the phone and computer are near each other?
- hanche 3y agoBluetooth is used to discover peers and to initiate communication. Thus handoff works even on a wifi network that blocks broadcasts. It even works when no wifi network is present, by setting up an ad hoc network for the connection. (Disclaimer: This is all I know. The details seem rather murky, as handoff is a proprietary Apple protocol.)
- renewiltord 3y agoUse the Apple shortcuts app and you can just copy some text and hit a keyboard shortcut. The Shortcuts app lets you run arbitrary shell command.
- philsnow 3y agoThis is interesting, thank you. I've been automating various things with Hammerspoon but (I think) it's limited to what you can reach with either a11y or osascript / the NS dictionary for the app you want to manipulate, but Shortcuts seems to have some actions that aren't in the NS dictionary. For instance, in Shortcuts, I see that there's a "Pin Notes" action for Notes.app, but I don't see anything for pinning notes when I open Notes.app with "File -> Open Dictionary..." in Script Editor. (In this case it's likely that Notes.app has the a11y bits necessary to run that action from Hammerspoon, but it would probably be easier to go through Shortcuts.)
- burnished 3y agoJson pretty printing in the terminal? Bless, didn't know about that and it is perfect
- adolph 3y agopb[paste|copy] are a life improver. Here is a one-liner to edit the pasteboard contents in vim. pbpaste > tmp; vim tmp; cat tmp | pbcopy; rm tmp; I also use pbpaste to append various notes to files, but since pbpaste doesnt have a newline at the end I wind up using: echo "$(pbpaste)" >> notes.txt
- dharmab 3y agoYou can do this specific task with just vim: https://vi.stackexchange.com/a/21448 https://vi.stackexchange.com/a/21448
- flaminHotSpeedo 3y agoAlso a fun one to combine with `open` if you have a bunch of web URLs to open
- m463 3y agoI have a script always running that polls for youtube URLs using pbpaste and runs yt-dl then just highlight any youtube link and COPY later when I have time, I can use quicklook to browse directory of youtube videos.
- ErneX 3y agoThat’s a great hack thanks for the tip.
- RoyGBivCap 3y agoFYI: yt-dlp is more up to date and faster, AFIK: https://github.com/yt-dlp/yt-dlp https://github.com/yt-dlp/yt-dlp I download and save it as 'ytdl' for convenience, but I use it all the time on twitter too.
- m463 3y agosorry mistyped, that's what I use
- n8henrie 3y agoSo much of my Linux use is over ssh from a MacOS client that I've made a `pbcopy` executable that just pipes stdin over ssh to my MacBook to its pbcopy (with a dedicated ssh key that runs this as a forced command). Makes it super nice to be on an SSH session and `pbcopy` some content to my MacOS clipboard!
- danielagos 3y agoThat sounds amazing, I always wanted to do that! Do you have a guide or some script to help with it? Otherwise, I will try to do it on my own.
- n8henrie 3y agoHappy to share, but I'm away from my MacBook for the next 2-3 weeks. I'll ping you when I have access to the code again.
- desro 3y agoI'm also super interested in this! I've had many amusing moments of instinctively typing either `pbcopy` or `pbpaste` on remote boxes followed by a brief moment of confusion when my local clipboard isn't updated :)
- SushiHippie 3y agoYou might want to have a look at osc52
- n8henrie 3y agoI have! Unfortunately not supported in MacOS Terminal.app, which I'm otherwise very satisfied with (have tried iTerm2, use Alacritty on Linux, just like Terminal.app).
- athrun 3y agoSimply wrap your shell with osc52pty to get OSC52 support in Terminal.app https://github.com/roy2220/osc52pty https://github.com/roy2220/osc52pty
- escot 3y agoI use the following to edit contents of my clipboard: pbpaste | vipe | pbcopy Where vipe is a util for inserting your editor (vim) in the middle of a pipe. From: https://joeyh.name/code/moreutils/ https://joeyh.name/code/moreutils/
- rasen58 3y agoThis is great utility I didn't know about! Thanks! But do you know why it doesn't seem to work with the `pbfilter` function? If I do directly `pbpaste | vipe | pbcopy`, then it opens vim and the clipboard text is pasted there. But if I run `pbfilter | vipe`, then vim opens with a blank buffer. function pbfilter() { if [ $# -gt 0 ]; then pbpaste | "$@" | pbcopy else pbpaste | pbcopy fi } It seems that the number of args is 0 for some reason
- nneonneo 3y agoI think you have to use `pbfilter vipe`, as the argument to pbfilter is inserted into the middle of the pipe.
- gdavisson 3y agoI find that the `pbpaste | something | pbcopy` idiom is common enough that it's worth having a shell function for it: pbfilter() { if [ $# -gt 0 ]; then pbpaste | "$@" | pbcopy else pbpaste | pbcopy fi } Then you can use something like `pbfilter json_pp` or `pbfilter base64 -d` or `pbfilter sed 's/this/that/'` or whatever. This version also can also act as a plain-text-only filter. If you just use `pbfilter` with no argument, it'll remove any formatting from the text in the pasteboard, leaving just straight plain text. It does have a some limitations, though: you can't use it with an alias, or pipeline, or anything complex like that. The filter command must be a single regular command (or function) and its arguments.
- dredmorbius 3y agoI've aliased that (and its equivalents on Linux and Android/Termux) to 'xc' and 'xp' (for X11 Copy and X11 Paste, as I'd originated this on Linux). Being able to populate or read from the system clipboard (or secondary clipboard!), or to feed it, including by reading from or writing to pipes is wonderful.
- duffyjp 3y agoI have an alias that while trivally simple is quicker to type and remember. It copies whatever file you give it to the clipboard which is super handy. I use it with the "Compare with Clipboard" to diff a file in Rubymine for example. alias clip='pbcopy <'
- systems_glitch 3y agoBest way to get ssh keys into the paste buffer too.
- user00012-ab 3y agomy favorite vim command: :w !pbcopy or visual selection, and then :w !pbcopy