Live data from Hacker News

Fixing macOS Zsh Terminal History Settings

blog.akatz.org

21–30 of 66 posts

Re: Fixing macOS Zsh Terminal History Settings

#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 HIST_IGNORE_ALL_DUPS) will cause duplicated commands to not be written to the history file which helps with `Ctrl-R`ing

Re: Fixing macOS Zsh Terminal History Settings

#22
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…

Oh! I came here to comment that this was the fix I needed: "When I have 2 shells open and close 1, the next shell I open seems to randomly choose which shell's commands to remember."

Looks like this may be what I need!

Re: Fixing macOS Zsh Terminal History Settings

#23

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.

doesn’t mac show a bash deprecation warning or something else that’s obnoxious when you do that? export BASH_SILENCE_DEPRECATION_WARNING=1

In zsh, it should look like this when you launch bash in the shell:

~

$ bash

The default interactive shell is now zsh. To update your account to use zsh, please run `chsh -s /bin/zsh`. For more details, please visit https://support.apple.com/kb/HT208050. bash-3.2$

~

So it's easy to switch between bash and zsh. It's quite rare for me to be in situation where I need to do something that only plays nice with bash, though.

Re: Fixing macOS Zsh Terminal History Settings

#24
I guess zsh is better at handling command histories of several concurrent shells than bash?

But still, you can do better. I'm using a snippet from this page: https://spin.atomicobject.com/2016/05/28/log-bash-history/

export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d %H:%M:%S") $(pwd) $(HISTTIMEFORMAT= history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log; fi'

This give an easily greppable shell history. An entry looks like this:

2022-10-13 09:14:54 /home/bhaak 1000 vim ~/.bashrc

Re: Fixing macOS Zsh Terminal History Settings

#25
post #12

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.

To each their own, but know that you are stuck with a really old version of bash if you do that, and it has some “interesting” bugs in it. See something I wrote a little while ago on this topic: https://jmmv.dev/2019/11/macos-bash-baggage.html

Not necessarily if one installs bash from homebrew.

Re: Fixing macOS Zsh Terminal History Settings

#26

I often have multiple concurrent sessions of terminal open, which leads to messed up history interactions. One solution I've encountered (but have not tested yet) is zsh-histdb [0]. It stores sessions' history to a SQLite db instead of a single appended file, with extra metadata about when commands were run, what session, etc. If you've already got your .zshrc file open to mess with the history settings it might be w…

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

I'm going to give this a go. That looks amazing, and it makes so much sense. I have a ton of commands around, either in my history, which isn't the best place, or in random files in different project folders. The ability to take a command and add to a notebook is a great idea.

Re: Fixing macOS Zsh Terminal History Settings

#27

I often have multiple concurrent sessions of terminal open, which leads to messed up history interactions. One solution I've encountered (but have not tested yet) is zsh-histdb [0]. It stores sessions' history to a SQLite db instead of a single appended file, with extra metadata about when commands were run, what session, etc. If you've already got your .zshrc file open to mess with the history settings it might be w…

Storing history in SQLite is such a wonderful idea, I wonder why someone doesn’t just add it to readline/libedit/etc, then all apps (which use those libraries) can do it.

Although, I wonder if that might cause problems, if apps already link to SQLite and they might expect a different version, or if unstable apps might cause concurrency/corruption/etc issues with a shared per-user history DB. To avoid all that, maybe a per-user history daemon with a Unix domain socket?

Re: Fixing macOS Zsh Terminal History Settings

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

Re: Fixing macOS Zsh Terminal History Settings

#29

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 also learned the lesson to use a different history file recently. I added `HISTSIZE=-1` to `~/.bashrc` to make the number of items stored in the history unlimited, but the history kept being truncated. The problem was that Ubuntu's default `~/.bashrc` file set `HISTSIZE` at the beginning of the file, and it had a side effect of truncating the history immediately. I had tried various methods but in the end, using a different history file felt the cleanest.

Re: Fixing macOS Zsh Terminal History Settings

#30
post #9

Does someone knows of some config to make zsh behave similar to bash for interactive use?

I guess you'll have to elaborate on "similar". What are the differences that you want to resolve? I followed Apple's migration to Zsh and configured it to be Bash-like, at least enough for me to not notice the difference.
Post reply on HN