Live data from Hacker News

macOS command-line tools you might not know about

saurabhs.org

121–130 of 454 posts

Re: macOS command-line tools you might not know about

#121
post #8

`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.

Another frequent use I have, applying random diffs with git:

    git diff | pbcopy
    pbpaste | git apply

Re: macOS command-line tools you might not know about

#124

Earlier quoted context omitted.

That's a useless use of cat. You can use `jq . foo.json | pbcopy` or `jq < foo.json | pbcopy`.

Speaking 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.

Yes, 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 , ' ')

Re: macOS command-line tools you might not know about

#125
post #98

Earlier quoted context omitted.

In what way do you see those alternatives as superior?

They avoid an unnecessary invocation of the cat executable. Instead, they open a file descriptor and pass that. Tiny difference but there you go.

To add, searching for “useless use of cat” will yield several results for those interested in learning more. Other examples include “useless use of echo” and “useless use of ls *”.

Re: macOS command-line tools you might not know about

#126
post #65

Here are my favorites: alias sleepoff="sudo pmset -a disablesleep 1" alias sleepon="sudo pmset -a disablesleep 0" This fully disables sleep, period. Just make sure you don't leave it unplugged too long, but on Apple Silicon it lasts for quite a long time.

this is just a worse version of caffeinate which is more likely to leave you with a dead battery.

Re: macOS command-line tools you might not know about

#127
post #121
post #8

`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.

Another frequent use I have, applying random diffs with git: git diff | pbcopy pbpaste | git apply

You 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.

Re: macOS command-line tools you might not know about

#128
post #90

Surprised pngpaste isn't mentioned (in the article nor the comments). If you take a screen shot (command + shift + 4) or partial screen shot (command + shift + control + 4) you can save it directly to an image file with: pngpaste filename.png

`pngpaste` doesn’t ship with macOS. All the tools in the article do.

Re: macOS command-line tools you might not know about

#129

While you're at it, `brew install coreutils`. The coreutils that ship with macOS lack a lot of features available on Linux. If you use bash, I recommend upgrading it, too, since Apple ships a 16 year old build (iirc due to legal issues associated with GPLv3) `brew install bash`

They're shipping zsh now.

I've found the transition to zsh surprisingly painless. But then, I do most of my command line scripting on linux and not my own machine. But still.

Re: macOS command-line tools you might not know about

#130

Nice list. Also, hidutil ( https://developer.apple.com/library/archive/technotes/tn2450... ). Example: hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000049,"HIDKeyboardModifierMappingDst":0x700000065}]}' For my PC keyboard, remaps "Ins" (normally useless under macOS) to something ("PC Execute") I can trap and remap with Keyboard Maestro.

There's a nice generator for these mappings https://hidutil-generator.netlify.app/
Post reply on HN