I don't even use Time Machine for my off-machine backups, but I use tmutil to create local snapshots so I can easily back out of changes by using the Time Machine GUI to restore files from the snapshot.
Neat, I was not aware of `networkQuality`. A good replacement for opening up Speedtest or whatnot when you just want to figure out if the network is slow or something else is up.
It tests your bandwidth while fully saturating your upload AND your download. If maxing out your upload has a huge impact on your download (like, a 70% drop), then that’s probably a sign you could tweak your network for better flow control.
That’s sort of the idea of `networkQuality`. It’s a new idea for how to measure that’s different from the standard speed test.
Through if you run it with the `-s` flag it’ll test them sequentially.
In my case, it appears that maximizing my download has a huge impact on my upload as well, which makes sense from a TCP/IP point of view. In this case, do you mean the ISP's flow control?
I'm sure others know waaay more about this, but I think it's a thing you can improve locally. Though usually I think of it as the other way around, where maxing out upload severely impacts download. My understanding is that this causes bufferbloat[0], making packets queue up for a long time on your gateway, ultimately limiting you to way less bandwidth then you should be able to get.
My one experience with this is on Ubiquiti hardware where there's a feature called "Smart Queues" you can enable. Really it's FQ_CODEL[1] under the hood. If you tell it your real maximum up/down bandwidth, minus ~5%, it'll enforce those limits in a way that prevents buffer bloat and lets you use nearly your full download bandwidth even when your upload bandwidth is maxed out. On Ubiquiti gear this has a CPU impact since it has move some traffic handling from dedicated hardware to the CPU. But it was a huge night and day difference for me. After enabling this, having a couple people on Zoom calls (highish upload) no longer tanked everyone else's download speed.
Also I think this stuff matters more when you have a large multi-user network. For normal home life, definitely not worth it. (In my case it was wifi for ~20 people).
Try running it with `-s` to see if you get speed tests that resemble what you expect. The idea is that maxing out both links at once is a better measure of network quality than sequence speed tests. Also it’s new RTT metric is key.
Xcode uses `mdfind` to provide symbols in crash reports and for Instruments, and is the reason why it seems to have a life of its own and work only when it wants to. Spotlight indexing is extremely flaky for reasons I'm not aware of.
I 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.
You 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.
On 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.
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:
I 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'
I 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...`.
Since you mention both pbcopy and iTerm - I love 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.
I 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-clipboar...).
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.
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 numbers can be VITAL, etc).
I still uselessly use cat though, it's such a nice way to build a pipeline.
generally, speaking, if you don't have an idea of how big the file is, or it would take up too much real-estate on your terminal window, sure. 100%. It was just an example.
lot's of times we sort of know what we are working with, but don't remember the particulars especially
As a scientist who cares about reproducibility, the big difference between the "useless cat" and providing the input file name on the command line is that, in the latter case, the program can capture that file name and reproduce it. That is harder when using stdin.
Many of my programs and scripts start output with the line:
# cmd arg1 arg2 arg3 ...
and simply echo back lines that start with '#'. That way, I have an internal record of the program that was run and the data file that was read (as well as previous parts of the analysis chain).
And, 'R' ignores lines starting with '#', so the record is there, but does not affect later analyses.
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 *”.
Not just that, but also all the bytes have to go through an extra pipe. Presumably they're copied an extra time because of this.
When you run "cmd < file", the command reads from stdin, which pulls directly from the file. When you do "cat file | cmd", "cat" opens the file, reads from there, and writes to a pipe. Then "cmd" reads from its stdin, which is a pipe.
GNU cat will use the copy_file_range syscall when possible!
copy_file_range allows a user land program to copy data between two files without doing any user space work. Instead of reading data into a buffer and writing it back out to the destination, the kernel will somehow manage to move the data for you.
I think this will prevent any extra copies from occurring in situations where it can be used.
I teach shell scripting. Cat invocations are cheap and help learners understand and keep clear where input is coming from, and where it is going. There are no awards or benefits to reducing the number of lines, commands invoked, or finding the shortest possible way to perform a task in a script. There are plenty of detriments to reading and understanding though when we try to obfuscate this to save 1ms of execution time on a script that is going to execute near instantaneously anyways.
I 100% agree with you. My only defense of OP is that `<` is something tends to be forgotten. Like everyone else in this thread I go to `cat` first for things like this. But sometimes I forget that even `<` exists, and the callout is a nice reminder.
If the command is meant to stream through something really fast by using a large buffer size, then prepending a cat(1) will limit the incoming buffer size to ~4k.
It usually doesn't matter much, but there are some situations where it can matter a lot. For one thing, you can't use seek() on a pipe, so e.g. `cat bigfile | tail` has to read through the entire file to find the end, but `tail bigfile` will read the file backward from the end, completely skipping the irrelevant beginning and middle. With `pv bigfile | whatever`, pv (which is basically a pipeline progress indicator) can tell how big file is and tell you how for through you are as a percentage; with `cat bigfile | pv | whatever`, it has no idea (unless you add a flag to tell it). Also, `cat bigfile | head` will end up killing cat with a SIGPIPE signal after head exits; if you're using something like "Unofficial bash strict mode" [1], this will cause your script to exit prematurely.
Another sometimes-important difference is that if there are multiple input files, `somecommand file1 file2 file3` can tell what data is coming from which file; with `cat file1 file2 file3 | somecommand` they're all mashed together, and the program has no idea what's coming from where.
In general, though, I think it's mostly a matter of people's expertise level in using the shell. If you're a beginner, it makes sense to learn one very general way to do things (`cat |`), and use it everywhere. But as you gain expertise, you learn other ways of doing it, and will choose the best method for each specific situation. While `cat |` is usually an ok method to read from a file, it's almost never the best method, so expert shell users will almost never use it.
The “useless cat” meme needs to die. Everyone is aware that most commands accept a file argument, but looking up the arguments and their ordering is annoying and using cat for things like this is just fine.
granted, it is a little snarky and maybe the snark isn't appropriate in today's tech environment. but no, things like "useless use of cat" do not need to go away, because they make me better at what I do in little ways. those little ways add up over time.
> but looking up the arguments and their ordering is annoying
you seem to be arguing for complacency. taking your idea to an extreme, why learn to do _anything_ well?
All of them do. Including bash. It’s just not the same syntax (ie ‘< filename’).
But I honestly think people who try to optimise away ‘cat’ are optimising the wrong thing. If one extra fork() is that detrimental then don’t use a shell scripting language.
For a lot of people, “useless” ‘cat’ enables them to write a pipeline in the order that their brain farts out the requirements for the pipeline. So they’ve optimised for human productivity. And given the human brain is slower than a few extra fork()s, I think optimising for one’s brain makes more sense here.
Literally the next sentence after the one you quoted explains my point:
> It’s just not the same syntax (ie ‘< filename’).
Reading from a file isn’t a hard problem. Having a good UX for doing that is where most shells fall apart. And that’s basically what ‘cat’ offers here: an improved UX.
Having ‘cat’ as a shell builtin wouldn’t really solve the complaints raised by “useless use of” anyway because you’d still be piping (and in some cases, fork()ing too). You couldnt really use ‘cat’ as syntactic sugar for ‘<‘ because things start to get really weird if you want to pass flags to ‘cat’ or even redirect the output to something other than a pipe. And given ‘cat’ is POSIX (https://en.m.wikipedia.org/wiki/List_of_Unix_commands#/media...) the current behaviour of shells is, in my opinion, correct. This is why my own shell has a differently named builtin that approximately serves the purpose of ‘cat’ but for instances when you need the command built into the shell and it can’t just be passing a file handle to the next command (in my case, because i wanted to pass metadata out-of-band as well as the file contents)
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!
That'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.
It 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.
also userify allows you to set up sudo access on some of the servers and not others, so that'd take care of the other root-access issue you have. (sudo also provides auditing/logging controls that are useful in a multi-user environment)
Could be an alias, I have a limited set of aliases for each type of system. But I keep a repository of hundreds of personal shell scripts and it fit better there.
Yes. 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.
I 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 :)
alias 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.
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.
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:
$ 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)
It 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.
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?
Bluetooth 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.)
There’s a fix for it where you remove all the unlock entries in Keychain, and then re-enable it. Only thing that worked for me long term (but it did fix it 100%)
I tried this yesterday. It... helped. But what I've now found is that if I escape out of the password prompt (which will turn the screen off again) and then try to unlock it a second time, the watch unlock will be triggered. I wonder if it's something about waking from sleep. (I also wonder if it would have worked before to do the same thing, but it never occurred to me to try.)
Some of the “not working” cases may be due to the application you’re copying from setting the items as `localOnly`, ex: from a password manager. I don’t have an explanation for other failures.
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.
I’m on the same version of 1password, probably for similar reasons.
In their iOS app, there’s an entry in Settings -> Security -> Allow Universal Clipboard which lets you opt-in to passwords through the clipboard. I suspect there’s something similar on macOS.
I have a clipboard manager application called "Paste" (creative i know). Its an awesome app for a million reasons. But one thing I like is that it allows me to see and hear when my iphone copy worked.
So I have it enabled so there is a sound when something goes into the clipboard. Even on my mac, I have come to rely on that audio feedback. But it has the added benefit that when I am using my phone in front of my computer and I copy something on my phone, I immediate (and it is impressively fast... maybe a 200ms delay), I hear the chime that something was added to my clipboard on my mac. So it gives you that good feedback that a copy "worked".
You can also shift+cmd+V to see the clipboard history, which is another complimentary tool with universal clipboard because if a paste isn't working as expected you can see if the universal copy never "took" (as you mentioned it is semi-unreliable), or if it just got overridden. You can then use the navigator to paste the older item.
I've noticed that more and more apps on both macOS and iOS sniff the clipboard contents and randomly clobber it. I usually notice it in apps like Sourcetree, where I'll click something or do a certain action and suddenly I can't paste anymore. I even get a feel for it, like my mind detects the pattern that empties the clipboard so I sense when I can no longer paste, but I can't figure out concrete repeatable steps to make it happen. On iOS it's more random, and I feel like it's probably Facebook doing it, or maybe websites in Safari. I just assume that everything is spying on my clipboard contents now, hoping to log secrets/passwords and PII to sell to scammers.
I have to say, this is one of the more disappointing developments from Apple, that they certainly must know by now about these clipboard shenanigans, but have done nothing to stop them. They need to implement permissions that deny all apps the ability to get/set the clipboard by default, and have an option to ask the user whether so-and-so app can access the clipboard (outside of normal copy/paste), every time with the option to allow always. And all clipboard access attempts should probably get logged somewhere.
I would think requiring opt-in for clipboard functionality would be the more radical option that would leave most users (myself included, I would imagine) scratching their heads when they can’t copy/paste as a matter of course. Maybe you meant something more specifically related to 3rd-party sniffing/modifying clipboard contents, but I haven’t really encountered that outside of apps such as CopyQ and Paste, and they are pretty explicit and intentional about their functions.
I have found a lot of utility with cross-device copy/paste. I know it requires the somewhat mysterious phantom Bluetooth/Wi-Fi connectivity that AirPlay/Airdrop use, so if I have disabled Bluetooth on my device, for example, it will no longer work. I could see where it might not be fully reliable enough to count on, I have experienced inexplicable failures, not often but enough to understand that it might not be some folks’ default preference. As part of the “handoff” function, it can be disabled in Settings at least.
This 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.)
So 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!
I'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 :)
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).
I don't think wrapping my entire shell session in a moderately complex third party tool (that maybe just uses pbcopy under the hood[1]) counts as "simply" when compared to my existing solution which just pipes over ssh and a couple bash scripts.
This tool isn’t doing anything particularly complex.
It sets up a new pty, attaches the child process to it, listens for OSC52 control codes, and calls pbcopy when appropriate.
You can wrap your ssh session with it and you’re done.
It’s very elegant and multiple orders of magnitude less complex than something like tmux.
This 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
I 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.
I'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.
I 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.
I used to work at a 24 hr end user tech support call center. They didn’t use Macs, but we had a machine for the techs to use to understand what the customer is looking at. I wrote a script to sleep until late at night then start saying weird/creepy stuff to mess with the overnight crew.
A nice feature I discovered recently is -a: you can give it a specific audio device, which falls back to the default if not present. I use it to report when builds finish through my monitor speakers in case I'm not wearing headphones. If I'm on the go and don't have the monitor connected it plays through the speakers.
Also for things like sharing public ssh keys. Instead of "can you send me your public key" and getting something with random line breaks depending on the users text editor I just have to ask them to "cat ~/.ssh/id_rsa.pub | pbcopy".
I also alias pbcopy / pbpaste on Linux too, so useful!
The random linebreaks are ok - SSH can handle them. (that's how Userify does it I think, too. it replicates whatever the user provides.. no judgment :)
It’s weird how macs don’t come with a GUI for managing system services. Windows does.. that being said the number of times I’ve had to mess with system services on mac can probably be counted with a few fingers…
You can also prepend #!/usr/bin/osascript
to a script and then make it executable with chmod oag+x. You can then invoke it normally in bash:
./filename.sh (or whatever)
Years ago, I wrote a script to find something in a big blob of data and to alert me when it was done, I added a “say <some glib Rambo or Schwarzenegger>” type phrase upon completion. I forgot about it and went to bed and was jolted awake hours later by what was clearly an “intruder” speaking to his accomplice, in my home office. Quite the relief when I realized what happened.
That's hilarious! Similar vein: there is a Metasploit module to induce the say command on post-exploited mac machines. I haven't seen it used in practice, but I eagerly watch for the eventual twitter thread that reads:
"So, I used the msf module that invokes `say` on a client's laptop"
I use Pushover for a few alerts so years ago I wrote a little bash script called `push` that you pass a title and optionally a body. Was very nice to to `./longRunningCommand && push "Task Done" "Here is a body"`. I'd sometimes combine this with my `beep` script that just makes a noise for when I know I'll still be at my computer but want to know when something finishes.
Here is my beep script, it's almost embarrassingly basic. You can pick a different sound, I wanted one that wasn't too offensive/harsh and I've used this for 3-4+ years.
I often have multiple terminal tabs open. Sometimes I’ll run a command that ends up taking a while and switch away and forget about it.
So I added a fish command completion script that plays a beep with afplay if the task took longer than 5 seconds. It helps me get back on task for those “just long enough” tasks that I run.
It took me a while but I finally got open to open folders in a new Finder tab instead of opening a new window each time.
function opent () {
what=${1:-`pwd`}
what=$(cd "$what"; pwd)
osascript -e "tell application \"Finder\"
activate
set t to target of Finder window 1
set toolbar visible of window 1 to true
end tell
tell application \"System Events\"
keystroke \"t\" using command down
end tell
tell application \"Finder\"
set target of Finder window 1 to POSIX file \"$what\"
end tell" > /dev/null
}
## opens current dir
$ opent .
## same
$ opent
More than that, it sends a message to launchd/the app instead of forking on the spot.
Sadly the app does get the shell's environment and it can't be disabled:
Opened applications inherit environment variables just as if you had
launched the application directly through its full path. This behavior
was also present in Tiger.
Why this matters, e.g with vscode:
- ensure vscode is fully closed
- enter project directory, something sets vars in your shell (you manually or automatically via direnv)
- code .
- vscode process now has the env from the shell it was started
- open another directory from the UI
- vscode forks and inherits from its parent process, thus the other project window has the original shell's env
- go to another directory
- code .
- vscode finds out it's already running, forks and opens another window. this window has the original shell env
- fully quit vscode and reopen it, but via the app in /Applications
- vscode opens, now has a blank environment for its main process, and forks form there to restore previous windows, which now lack the environment they had
It's a) completely inconsistent and b) dangerous: imagine the original shell had a setting or secret in an env var that was shared to the second project (e.g virtualenv, deploy target, deployment key...)
The same issue can happen with other apps but also tmux (the tmux daemon is spawned from the first tmux command, and then subsequent sessions from tmux-server; doing it another way is possible but nontrivial)
I also find myself using open -n -a <application> to open a new separate instance of an application if I want to copy settings from one file to another or work on two files with separate instances of a program
Another good one is 'hidutil' which can remap any keys without additional software. It's handy for things like remapping CapsLock to anything, etc. For actual full keyboard layouts though I'd use Ukelele[0].
I remember Caffeine used to be a third-party program that would prevent your Mac from sleeping, same behavior as the `caffeinate` here. Were they acquired and incorporated into the OS?
From what I recall, caffeine app predated the caffeinate command (which was only introduced in lion). Caffeine.app also disables sleep via a completely different method than caffeinate. The latter uses a IOPMAssertion which is the recommended way to do it because it is visible in `pmset -g assertions` whereas the former doesn't use that approach (I forgot how exactly it did it, there's like 4 different methods on osx to prevent sleep).
Isn't there `xsel` or `xclip` installed instead [0]? These two commands are commonly used on the Linux boxes and they support pipes too. For example, the xclip is used by the original password-store implementation [1].
On [2] you might find the aliases to pbutil for X11-based Linuxes.
[0]: If not, why not run apt-get command to install one of them, like xclip?
Yes, this is great... I was given a Macbook with a Norweigan keyboard for testing a port of FreeBSD on it, and I quickly discovered that the keyboard layout remapping stuff available via the UI won't remap at least this one key to what I'd find on my US keyboard.
Probably want to check your policies if using a business-owned mac. Caffeinate probably violates your security policies if it’s a decent sized company.
`caffeinate` can set assertions, the same assertions that Zoom or PowerPoint or Keynote do to stop the screen going to sleep during a meeting or presentation, the same assertions that the browsers can set during streaming video, so you absolutely can bypass whatever your admins set using `caffeinate -dmisu` which sets every assertion available.
I set an alias in my shell so this is just 'airport', lets you interact with the wifi settings - I particularly like 'airport -s' for doing a scan of the local wifi networks, since it shows signal strength and channel information right there, which is helpful when troubleshooting.
It also separates 2.4GHz and 5.0GHz bands and shows security details (which matters sometimes e.g band steering or automatic same-SSID signal-strength-based AP selection doesn't work)
453 comments
[ 4.0 ms ] story [ 310 ms ] threadThat’s sort of the idea of `networkQuality`. It’s a new idea for how to measure that’s different from the standard speed test.
Through if you run it with the `-s` flag it’ll test them sequentially.
My one experience with this is on Ubiquiti hardware where there's a feature called "Smart Queues" you can enable. Really it's FQ_CODEL[1] under the hood. If you tell it your real maximum up/down bandwidth, minus ~5%, it'll enforce those limits in a way that prevents buffer bloat and lets you use nearly your full download bandwidth even when your upload bandwidth is maxed out. On Ubiquiti gear this has a CPU impact since it has move some traffic handling from dedicated hardware to the CPU. But it was a huge night and day difference for me. After enabling this, having a couple people on Zoom calls (highish upload) no longer tanked everyone else's download speed.
Also I think this stuff matters more when you have a large multi-user network. For normal home life, definitely not worth it. (In my case it was wifi for ~20 people).
[0] https://en.wikipedia.org/wiki/Bufferbloat [1] https://en.wikipedia.org/wiki/CoDel
Edit: Found https://www.macinstruct.com/tutorials/how-to-check-your-macs... which says:
"The networkquality tool uses Apple’s CDN (https://mensura.cdn-apple.com/api/v1/gm/config) as a target"
The contents of this file (for me):
Try running it with `-s` to see if you get speed tests that resemble what you expect. The idea is that maxing out both links at once is a better measure of network quality than sequence speed tests. Also it’s new RTT metric is key.
[1] https://www.speedtest.net/apps/cli
[2] https://github.com/sindresorhus/fast-cli
https://www.manpagez.com/man/1/fs_usage/
I can't count the number of times a bit of fs_usage foo has helped me dig out of a seriously messy pile of network, file and page fault issues ..
Dealing with some minified json, switching to iTerm, doing `pbpaste | json_pp | pbcopy` and having a clean output is _so_ nice.
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.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/p...
https://en.m.wikipedia.org/wiki/Join_(Unix)
Simple is genius
`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
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...`.
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-clipboar...).
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.
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 , ' ')
> looks like its comma delimited.
Interesting; why wouldn't you use `head`? Who knows how big textfile.txt is?
I still uselessly use cat though, it's such a nice way to build a pipeline.
I've been typing cat for over 25 years. Old habits die hard.
lot's of times we sort of know what we are working with, but don't remember the particulars especially
https://github.com/sharkdp/bat
Many of my programs and scripts start output with the line: # cmd arg1 arg2 arg3 ...
and simply echo back lines that start with '#'. That way, I have an internal record of the program that was run and the data file that was read (as well as previous parts of the analysis chain).
And, 'R' ignores lines starting with '#', so the record is there, but does not affect later analyses.
Instead, they open a file descriptor and pass that.
Tiny difference but there you go.
Even the Wikipedia page on cat has a section about that, titled eponymously.
https://en.m.wikipedia.org/wiki/Cat_(Unix)
When you run "cmd < file", the command reads from stdin, which pulls directly from the file. When you do "cat file | cmd", "cat" opens the file, reads from there, and writes to a pipe. Then "cmd" reads from its stdin, which is a pipe.
copy_file_range allows a user land program to copy data between two files without doing any user space work. Instead of reading data into a buffer and writing it back out to the destination, the kernel will somehow manage to move the data for you.
I think this will prevent any extra copies from occurring in situations where it can be used.
https://man.archlinux.org/man/copy_file_range.2
https://git.savannah.gnu.org/cgit/coreutils.git/tree/src/cat...
And ... ?
In short, I straight up don't care.
Maybe use dd with one of its blocksize options, then?
Not at a terminal, can't check.
Another sometimes-important difference is that if there are multiple input files, `somecommand file1 file2 file3` can tell what data is coming from which file; with `cat file1 file2 file3 | somecommand` they're all mashed together, and the program has no idea what's coming from where.
In general, though, I think it's mostly a matter of people's expertise level in using the shell. If you're a beginner, it makes sense to learn one very general way to do things (`cat |`), and use it everywhere. But as you gain expertise, you learn other ways of doing it, and will choose the best method for each specific situation. While `cat |` is usually an ok method to read from a file, it's almost never the best method, so expert shell users will almost never use it.
[1] http://redsymbol.net/articles/unofficial-bash-strict-mode/
> but looking up the arguments and their ordering is annoying
you seem to be arguing for complacency. taking your idea to an extreme, why learn to do _anything_ well?
Such a shell could remove some of the more common cases.
But I honestly think people who try to optimise away ‘cat’ are optimising the wrong thing. If one extra fork() is that detrimental then don’t use a shell scripting language.
For a lot of people, “useless” ‘cat’ enables them to write a pipeline in the order that their brain farts out the requirements for the pipeline. So they’ve optimised for human productivity. And given the human brain is slower than a few extra fork()s, I think optimising for one’s brain makes more sense here.
Are you sure? https://unix.stackexchange.com/questions/208615/is-cat-a-she... disagrees and neither https://manpages.ubuntu.com/manpages/jammy/man7/bash-builtin... nor https://zsh.sourceforge.io/Doc/Release/Shell-Builtin-Command... mention it
> It’s just not the same syntax (ie ‘< filename’).
Reading from a file isn’t a hard problem. Having a good UX for doing that is where most shells fall apart. And that’s basically what ‘cat’ offers here: an improved UX.
Having ‘cat’ as a shell builtin wouldn’t really solve the complaints raised by “useless use of” anyway because you’d still be piping (and in some cases, fork()ing too). You couldnt really use ‘cat’ as syntactic sugar for ‘<‘ because things start to get really weird if you want to pass flags to ‘cat’ or even redirect the output to something other than a pipe. And given ‘cat’ is POSIX (https://en.m.wikipedia.org/wiki/List_of_Unix_commands#/media...) the current behaviour of shells is, in my opinion, correct. This is why my own shell has a differently named builtin that approximately serves the purpose of ‘cat’ but for instances when you need the command built into the shell and it can’t just be passing a file handle to the next command (in my case, because i wanted to pass metadata out-of-band as well as the file contents)
[1] https://github.com/jedisct1/piknik
`cat ~/.ssh/mykey.pub | pbcopy`
`pbcopy < ~/.ssh/mykey.pub`
(I use this all the time myself!)
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!
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.
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.
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.
* Mac: Go to System Preferences > General > uncheck Allow Handoff.
* iPhone: Go to Settings > General > Handoff > uncheck Handoff.
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?
Turning off Bluetooth or wifi may be one of the more common reasons it doesn’t work. Some people never do that, but others do.
The only drawback is that yes it only works most of the time. And when it doesn’t I get infuriated.
Glitches happen without any change to settings or network on my side - it works now, and 5 min later doesn’t.
I use most of Apple's "built in" applications like Mail, Notes, Photos, etc. with Firefox (instead of Safari) probably the only exception to that.
https://georgegarside.com/blog/macos/fix-apple-watch-auto-un...
https://developer.apple.com/documentation/uikit/uipasteboard...
In their iOS app, there’s an entry in Settings -> Security -> Allow Universal Clipboard which lets you opt-in to passwords through the clipboard. I suspect there’s something similar on macOS.
So I have it enabled so there is a sound when something goes into the clipboard. Even on my mac, I have come to rely on that audio feedback. But it has the added benefit that when I am using my phone in front of my computer and I copy something on my phone, I immediate (and it is impressively fast... maybe a 200ms delay), I hear the chime that something was added to my clipboard on my mac. So it gives you that good feedback that a copy "worked".
You can also shift+cmd+V to see the clipboard history, which is another complimentary tool with universal clipboard because if a paste isn't working as expected you can see if the universal copy never "took" (as you mentioned it is semi-unreliable), or if it just got overridden. You can then use the navigator to paste the older item.
I've noticed that more and more apps on both macOS and iOS sniff the clipboard contents and randomly clobber it. I usually notice it in apps like Sourcetree, where I'll click something or do a certain action and suddenly I can't paste anymore. I even get a feel for it, like my mind detects the pattern that empties the clipboard so I sense when I can no longer paste, but I can't figure out concrete repeatable steps to make it happen. On iOS it's more random, and I feel like it's probably Facebook doing it, or maybe websites in Safari. I just assume that everything is spying on my clipboard contents now, hoping to log secrets/passwords and PII to sell to scammers.
I have to say, this is one of the more disappointing developments from Apple, that they certainly must know by now about these clipboard shenanigans, but have done nothing to stop them. They need to implement permissions that deny all apps the ability to get/set the clipboard by default, and have an option to ask the user whether so-and-so app can access the clipboard (outside of normal copy/paste), every time with the option to allow always. And all clipboard access attempts should probably get logged somewhere.
I have found a lot of utility with cross-device copy/paste. I know it requires the somewhat mysterious phantom Bluetooth/Wi-Fi connectivity that AirPlay/Airdrop use, so if I have disabled Bluetooth on my device, for example, it will no longer work. I could see where it might not be fully reliable enough to count on, I have experienced inexplicable failures, not often but enough to understand that it might not be some folks’ default preference. As part of the “handoff” function, it can be disabled in Settings at least.
There was a prompt asking if I wanted to enable it, when I set up my phone/Mac. Same setup screen that asks if you want to enable location, Siri, etc.
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.)
then just highlight any youtube link and COPY
later when I have time, I can use quicklook to browse directory of youtube videos.
I download and save it as 'ytdl' for convenience, but I use it all the time on twitter too.
https://github.com/roy2220/osc52pty
But thank you for the share, it is interesting!
[1]: https://github.com/roy2220/osc52pty/blob/master/oscexecutor....
You can wrap your ssh session with it and you’re done.
It’s very elegant and multiple orders of magnitude less complex than something like tmux.
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.
It seems that the number of args is 0 for some reasonThis 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.
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.
alias clip='pbcopy <'
:w !pbcopy
or visual selection, and then :w !pbcopy
qlmanage -t -s 1000x1000 -o ~/Pictures/foo.png ~/Pictures/foo.svg
To turn an square SVG into a PNG without installing anything extra or using an online image tool
I administer that machine for her (to the extent that such a thing is needed), and so I knew (a) her login and (b) that SSH was open.
Me combining remote access with "say" made for a very memorable morning for that kid.
networkQuality is something I really wish I knew about sooner.
I also alias pbcopy / pbpaste on Linux too, so useful!
"So, I used the msf module that invokes `say` on a client's laptop"
#!/bin/bash
afplay -v 3 /System/Library/Sounds/Glass.aiff
So I added a fish command completion script that plays a beep with afplay if the task took longer than 5 seconds. It helps me get back on task for those “just long enough” tasks that I run.
It’s indispensable.
Sadly the app does get the shell's environment and it can't be disabled:
Why this matters, e.g with vscode: It's a) completely inconsistent and b) dangerous: imagine the original shell had a setting or secret in an env var that was shared to the second project (e.g virtualenv, deploy target, deployment key...)The same issue can happen with other apps but also tmux (the tmux daemon is spawned from the first tmux command, and then subsequent sessions from tmux-server; doing it another way is possible but nontrivial)
https://github.com/microsoft/vscode/issues/15452
https://github.com/microsoft/vscode/issues/108804#issuecomme...
[0] https://software.sil.org/ukelele
> It's handy for things like remapping CapsLock to anything
It's a built-in MacOS feature that you can find in the keyboard settings.
Since then I've moved to Amphetamine (same purpose menu-bar app, even stronger chemicals, has timed keep-awake etc).
On [2] you might find the aliases to pbutil for X11-based Linuxes.
[0]: If not, why not run apt-get command to install one of them, like xclip?
[1]: https://git.zx2c4.com/password-store/tree/src/password-store...
[2]: https://ostechnix.com/how-to-use-pbcopy-and-pbpaste-commands...
Also, hidutil (https://developer.apple.com/library/archive/technotes/tn2450...).
Example:
For my PC keyboard, remaps "Ins" (normally useless under macOS) to something ("PC Execute") I can trap and remap with Keyboard Maestro.I use pbcopy and pbpaste probably all day long and always miss it in Linux environments.
I use open regularly. Often to open a directory in finder and preview a file. Looks like I can just use qlmanage from now on.
Not sure I'd use screencapture manually, but I'm sure there are some automations that could benefit from this.
It'll stay connected/running/screen-on/etc but it's still locked.