Live data from Hacker News

macOS command-line tools you might not know about

saurabhs.org

411–420 of 454 posts

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

#411
post #184
post #155

Earlier quoted context omitted.

Oh 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))

Or directly from the commandline: pbcopy | python -c 'import sys; import json; print(json.load(sys.stdin))' | pbpaste

The 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)

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

#412

Earlier quoted context omitted.

I have linux/macos-agnostic bash functions in my dotfiles that unify this to “clip” and “paste” (since “copy” is too close semantically to “cp”)

And 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/a3654d6a194e3689978... # Use clipboard in shell pipelines # clip | xargs echo # uses pbpaste # ps -A | grep search | clip # uses pbcopy clip() { [ -t 0 ] && pbpaste || p…

So 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 `read -r -t0` is Bash-only though and not POSIX, but it will work regardless of what type of data is on stdin

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

#414

I use: ``` #!/usr/bin/env bash set -u title=${2:-Shell} msg=$1 osascript -e "display notification \"$1\" with title \"$title\"" ``` As `~/bin/,notify` and put it at the end of long-running commands: ``` run_this_program && ,notify "Long program is done!" ```

an upvote wasn't enough! this is cool, thanks for sharing.

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

#415

`open` is one I use all the time. I love that simple command. alias tab='open . -a iterm' alias phpstorm='open -a "PhpStorm"' alias smerge='open -a "Sublime Merge"' etc. I use those more than I do the Open/Recents dialogs in the respective apps.

Sublime Text and Sublime Merge actually ship with these CLI utilities by default, which have some additional features: $ fd 's(merge|ubl)$' /Applications/Sublime* /Applications/Sublime Merge.app/Contents/SharedSupport/bin/smerge /Applications/Sublime Text.app/Contents/SharedSupport/bin/subl

I knew of (and use) `subl` but was unaware they provided their own `smerge`. I'll delete my own alias in favor of that. Thanks!

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

#416
post #357

`open` is one I use all the time. I love that simple command. alias tab='open . -a iterm' alias phpstorm='open -a "PhpStorm"' alias smerge='open -a "Sublime Merge"' etc. I use those more than I do the Open/Recents dialogs in the respective apps.

> alias tab='open . -a iterm' But if you just open a new tab, you'll be in the same $CWD in the new tab? Am I missing some trick here?

I forget that's an option. I really dislike that and prefer for new tabs to open in my home directory. So having my `tab` alias that I use a couple times a week and opening the other 100 tabs in my home directory fits my workflow.

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

#417

Earlier quoted context omitted.

You might want to have a look at osc52

I 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).

Simply wrap your shell with osc52pty to get OSC52 support in Terminal.app

https://github.com/roy2220/osc52pty

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

#418

Earlier quoted context omitted.

Yes it’s 1Password. I use the very old, iOS only non subscription version 7.something. But I think sometimes it works (copy from iOS paste on MacOS).

Wonder how many of us are out there!? Long live this app and workflow, iCloud sync ftw.

Yes, I use this app multiple times a day, each time with the added satisfaction of knowing I own the app and not a subscription. Even if my credit card expires I will still have access to my passwords.

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

#419
post #364

Earlier quoted context omitted.

I really recommend folks use "less" over cat, especially keyboard oriented folks. Different terminal emulators don't always have the scroll behavior I want, not do they always allow me to search the file I'm looking at. "less" does all those things, in nearly every environment no matter the terminal emulator, and has other wonderful options to boot (chop long lines so they don't wrap can be nice for logs, line number…

I hate that when I use `less`, then quit, the output goes away.

You can run "less -X" for that, but it may have other problems depending on how you use less (e.g. scrolling up, etc.)

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

#420

Not a command-line tool but the network link conditioner is also really great. Never seen such a tool on another OS You can simulate a really bad network. Latency, bandwidth, packet loss etc. Great for testing but also if people insist on cameras being on. Just screw up the connection so bad that everyone gets annoyed with your blocky image and robot voice and suggest you turn off video and then you make it 'magicall…

You can do that with standard Linux tooling available on every distribution, see https://man7.org/linux/man-pages/man8/tc.8.html. What you're specifically looking for is `qdisc netem`, it can inject packet loss, reordered packets, duplicate packets, delay and more.
Post reply on HN