← Back to context

Comment by latexr

1 year ago

> trash a.txt b.png moves `a.txt` and `b.png` to the trash. Supports macOS and Linux.

The way you’re doing it trashes files sequentially, meaning you hear the trashing sound once per file and ⌘Z in the Finder will only restore the last one. You can improve that (I did it for years) but consider just using the `trash` commands which ships with macOS. Doesn’t use the Finder, so no sound and no ⌘Z, but it’s fast, official, and still allows “Put Back”.

> jsonformat takes JSON at stdin and pretty-prints it to stdout.

Why prioritise node instead of jq? The latter is considerably less code and even comes preinstalled with macOS, now.

> uuid prints a v4 UUID. I use this about once a month.

Any reason to not simply use `uuidgen`, which ships with macOS and likely your Linux distro?

https://www.man7.org/linux/man-pages/man1/uuidgen.1.html

I am not the author, but my bet is that he didn't know of its existence.

The best part about sharing your config or knowledge is that someone will always light up your blind spots.

  • > The best part about sharing your config or knowledge is that someone will always light up your blind spots.

    Yes! I will take this as a chance to thank every people who shared their knowledge on the Internet. You guys are so freaking awesome! You are always appreciated.

    A big chunk of my whole life's learning came from all the forums that I used to scour through, hours after hour! Because these awesome people always sharing their knowledge, and someone adding more. That's what made Internet, Internet. And all is now almost brink of loss, because of greedy corporates.

    This habit also helped me with doom-scrolling. I sometimes, do doomscroll, but I can catch it quickly and snap out of it. Because, my whole life, I always jumped in to the rabbit holes, and actually read those big blog posts, where you had those `A-ha` moments, "Oohh, I can use that", "Ahh, that's clever!".

    When, browsing, do not give me that, by brain actually triggers, "What are you doing?"

    Later, I got lazy, which I am still paying for. But I am going to get out of it.

    Never stop jumping into those rabbit holes!! Well, obviously, not always it's a good rabbit hole, but you'll probably come out wiser.

  • Or more abstractly: post anything to the internet and people will always detail how you’re wrong. Sometimes that can be useful.

  • also possible (even though I've seen the author's response to not knowing) is that the scripts were written before native was included. at that point, the muscle memory is just there. I know I have a few scripts like that myself

Python also pretty-prints out of the box:

    $ echo '{ "hello": "world" }' | python3 -m json.tool
    {
        "hello": "world"
    }

Other examples where native features are better than these self-made scripts...

> vim [...] I select a region and then run :'<,'>!markdownquote

Just select the first column with ctrl-v, then "i> " then escape. That's 4 keys after the selection, instead of 20.

> u+ 2025 returns ñ, LATIN SMALL LETTER N WITH TILDE

`unicode` is widely available, has a good default search, and many options. BTW, I wonder why "2025" matched "ñ".

     unicode ñ
    U+00F1 LATIN SMALL LETTER N WITH TILDE
    UTF-8: c3 b1 UTF-16BE: 00f1 Decimal: &#241; Octal: \0361

> catbin foo is basically cat "$(which foo)"

Since the author is using zsh, `cat =foo` is shorter and more powerful. It's also much less error-prone with long commands, since zsh can smartly complete after =.

I use it often, e.g. `file =firefox` or `vim =myscript.sh`.

  • > `unicode` is widely available

    It's not installed by default on macOS or Ubuntu, for me.

    • You are right but

        $ unicode
        Command 'unicode' not found, but can be installed with:
        sudo apt install unicode
      

      and it did. So it really was available. That's Debian 11.

> Why prioritise node instead of jq?

In powershell I just do

    > echo '{"foo": "bar"} | ConvertFrom-Json | ConvertTo-Json
    {
        "foo": "bar"
    }

But as a function

`trash` is good to know, thanks! I'd been doing: "tell app \"Finder\" to move {%s} to trash" where %s is a comma separated list of "the POSIX file <path-to-file>".

  • Oooh, I just suggested in another comment that using applescript would be possible. I didn't think it would be this easy though.

For trash on macOS, I recommend https://github.com/ali-rantakari/trash

Does all the right things and works great.

There’s a similar tool that works well on Linux/BSDs that I’ve used for years, but I don’t have my FreeBSD desktop handy to check.

> Why prioritise node instead of jq? The latter is considerably less code and even comes preinstalled with macOS, now.

That was my thought. I use jq to pretty print json.

What I have found useful is j2p and p2j to convert to/from python dict format to json format (and pretty print the output). I also have j2p_clip and p2j_clip, which read from and then write to the system clipboard so I don't have to manually pipe in and out.

> Any reason to not simply use `uuidgen`, which ships with macOS and likely your Linux distro?

I also made a uuid, which just runs uuidgen, but then trims the \n. (And maybe copied to clipboard? It was at my old job, and I don't seem to have saved it to my personal computer.)

Instead of trash, reimplementing rm (to only really delete after some time or depending on resource usage or to shred of you are paranoid if the goal is to really delete something) or using zfs makes much more sense.

I believe it would be possible to execute an applescript to tell the finder to delete the files in one go. It would theoretically be possible to construct/run the applescript directly in a shell script. It would be easier (but still not trivial) to write an applescript file to take the file list as an argument to then delete when calling from the shell.

  • It’s not theoretical, and it is trivial. Like I said, I did exactly that for years. Specifically, I had a function in my `.zshrc` to expand all inputs to their full paths, verify and exclude invalid arguments, trash the rest in one swoop, then show me an error with the invalid arguments, if any.