Live data from Hacker News

Fixing macOS Zsh Terminal History Settings

blog.akatz.org

51–60 of 66 posts

Re: Fixing macOS Zsh Terminal History Settings

#51

Earlier quoted context omitted.

If somebody is interested in macos app for storing your history (bash,zsh,fish) and syncing it with icloud, I have built an app ShellHistory, v2 is available via TestFlight if you want to try it. https://loshadki.app/blog/2022-09-22-public-beta-shellhistor... (monterey and ventura) And this is a link to v1 https://loshadki.app/shellhistory/ (big sur+)

Been using this for about a year, well worth the $12. Thanks man! Saves me finding a command I ran but I don't remember when, or in which of my 50+ open and not committed shells. Also the exit code of every command is super helpful.

>...or in which of my 50+ open and not committed shells...

I suffered this frustration too, but adding this to my .bashrc solved it:

    #write to history file at each shell prompt
    export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Every time a command is executed it's written to history right away. What you loose: the scroll-back buffer in each window pertaining to that session. So if you have a window running a set of commands, and you just 'up arrow' to re-run, you loose that because all history gets appended.

I too would generally have 20+ open xterm windows but this gave instant command-to-history that I needed. fzf also helps instead of relying on up-arrow.

Re: Fixing macOS Zsh Terminal History Settings

#52
post #39

Earlier quoted context omitted.

If somebody is interested in macos app for storing your history (bash,zsh,fish) and syncing it with icloud, I have built an app ShellHistory, v2 is available via TestFlight if you want to try it. https://loshadki.app/blog/2022-09-22-public-beta-shellhistor... (monterey and ventura) And this is a link to v1 https://loshadki.app/shellhistory/ (big sur+)

Warning while this looks sweet, but this a security breach in the making. Having mistakes done in CLI local to the machine and session has saved me many times from storing really stupid stuff in the command history. I am not sure what safe guards are needed before I can use this. Which makes it hard to recommend it. Especially since security is not mentioned at all on that page.

A good best practice is to always change tokens that get stored in history, no matter where that history is stored (which in this case is iCloud).

IIRC, putting a space in front of the command will prevent it from appearing in history...

Re: Fixing macOS Zsh Terminal History Settings

#53

OT but the first command I type on a new install of MacOS is $ chsh -s /bin/bash because I have 20 years worth of bash scripts that I've carefully tailored to work on bash in MacOS and Linux and I don't have time to rewrite them all just because Apple decided they don't like GPLv3.

I recommend not changing the default shell, but rather having Terminal.app (or whatever) start the shell of your choice (in my case: /opt/local/bin/bash). You can't trust that the files in /opt/local or /usr/local will always be there, whereas the ones bundled by macos will.

Re: Fixing macOS Zsh Terminal History Settings

#54
I just tried this:

  >echo bollox
  >history | grep bollox
and got a long line of results, dating back to 2019 [Who knew I swore so often in my terminal!]

I don't have any of the fixes suggested in the article in my .zshrc so either it's duff info or not all OSX zsh installs are setup the same. Mind you, I'm still running OSX Mavericks so my zsh may not even have come bundled as the default shell. I may have enabled or installed it myself at a later date.

Re: Fixing macOS Zsh Terminal History Settings

#56
post #21

if we're talking about zsh history settings, I also recommend setopt SHARE_HISTORY HIST_IGNORE_DUPS SHARE_HISTORY will cause zsh to write to the history file after every command which means that two shells running in parallel won't override changes of each other and it will write a timestamp to the file too in order to have the history in chronological order even in light of multiple instances. HIST_IGNORE_DUPS (or H…

Oooh thank you!

Re: Fixing macOS Zsh Terminal History Settings

#58
post #28

This is zsh specific, but here's something I've been burned by enough times that I specifically wanted to mention it: save your history in a file other than the default one. Why? Because if you increase the history limit, and your profile isn't sourced for any reason, the shell will read your history file and use the default history size, clobbering everything that's there. If you use a non-default history file this…

I will try this, recently mine got truncated (don't know why) and it's super annoying.

For some reason, macOS will truncate your bash_history if your free space on your drive is low. No idea why they chose to truncate a small text file.

Re: Fixing macOS Zsh Terminal History Settings

#59
post #39

Earlier quoted context omitted.

If somebody is interested in macos app for storing your history (bash,zsh,fish) and syncing it with icloud, I have built an app ShellHistory, v2 is available via TestFlight if you want to try it. https://loshadki.app/blog/2022-09-22-public-beta-shellhistor... (monterey and ventura) And this is a link to v1 https://loshadki.app/shellhistory/ (big sur+)

Warning while this looks sweet, but this a security breach in the making. Having mistakes done in CLI local to the machine and session has saved me many times from storing really stupid stuff in the command history. I am not sure what safe guards are needed before I can use this. Which makes it hard to recommend it. Especially since security is not mentioned at all on that page.

Just to clarify:

1. iCloud sync is optional. You can sync the history, or notebooks (or both). Or keep everything always local only. 2. ShellHistory syncs with your private iCloud account, nobody other than person having access to your AppleID (with 2nd factor auth) cannot get access to it. 3. I would say it could be even better than storing your history in a file. Considering that with the ShellHistory you have access to "Full-Text-Search" and can really quickly find any accidental leaked information. 4. In v2 I have implemented an ignore pattern with RegEx, where you can define which commands you want to be ignored from saving to database. 5. I am on purpose build the app sandboxed and distribute it via App Store. It declares what it does. I don't and would never implement any custom telemetry collection, other than what App Store provides me already.

Re: Fixing macOS Zsh Terminal History Settings

#60

OT but the first command I type on a new install of MacOS is $ chsh -s /bin/bash because I have 20 years worth of bash scripts that I've carefully tailored to work on bash in MacOS and Linux and I don't have time to rewrite them all just because Apple decided they don't like GPLv3.

That changes your default shell, bash scripts (which should have the sheband `#!/usr/bin/env bash` will still get executed by bash normally.
Post reply on HN