News for hackers. Stuff that matters.
Re:Fixing macOS Zsh Terminal History Settings by saagarjha on Thursday October 13, 2022 @01:58AM (#33187749)
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 rogue shell (missing your settings) will dump its history into the default file and leave your custom file untouched.
Re:Fixing macOS Zsh Terminal History Settings by misnome on Thursday October 13, 2022 @05:02AM (#33188790)
I got burned a couple of times in the past with bash-history, and go a small step beyond this, even on zsh, and added to .zshrc:

    preexec_custom_history() {
      echo "$HOSTNAME $$ $(date "+%Y-%m-%dT%H:%M:%S%z") $1" >> "~/.fullhistory"
    }
    preexec_functions+=(preexec_custom_history)
I work on a lot of shared-filesystem computers, and so it's useful to be able to filter for commands ~year ago when I know what computer I was on when I ran it. I have a fzf binding to search through this with ctrl-r, and it's otherwise easily greppable.

When I was still using bash, I had this same thing using https://github.com/rcaloras/bash-preexec .

Re:Fixing macOS Zsh Terminal History Settings by cassianoleal on Thursday October 13, 2022 @06:27AM (#33189285)
Thanks, this looks great!

How does the history know that the prefix (hostname, PID, date) is not part of the command though?

I mean, I tested it and it works as expected (without them) but I'm really curious about how it works.

Re:Fixing macOS Zsh Terminal History Settings by misnome on Thursday October 13, 2022 @07:07AM (#33189496)
For searching/expansion, it's a constant number of fields I use my own ctrl-r handler, which just runs `cut -d" " -f 4-`. Before that, I just used the standard history and used this as a comprehensive backup - grep, and seeing everything in context was valuable for me alone.
Re:Fixing macOS Zsh Terminal History Settings by barosl on Thursday October 13, 2022 @04:06AM (#33188479)
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 by MawKKe on Thursday October 13, 2022 @06:06AM (#33189191)
Also I don't understand why modern shells are so conservative about the defaults. I understand that if a single history file is large, it might slow down shell startup and operation, but at the very least shells could/should do periodic history file rotation. It's very very very unlikely that the history files will be eating all the disk space.
Re:Fixing macOS Zsh Terminal History Settings by opk on Thursday October 13, 2022 @07:27AM (#33189606)
The shell's defaults are conservative because when you change the defaults you change the setup of many existing users. So unconfigured, the shells feel much the same as they did thirty years ago.

There can be other reasons for wanting a smallish history besides fast startup like wanting low history numbers if you learnt all the ancient `!` history escapes that were the main way of using history with csh going back forty-plus years ago. This is also one of the reasons why extension theme frameworks like oh-my-zsh have become popular - much of what they do is enable bells and whistles that are features of the core shell.

Re:Fixing macOS Zsh Terminal History Settings by arno_v on Thursday October 13, 2022 @03:56AM (#33188429)
I will try this, recently mine got truncated (don't know why) and it's super annoying.
Re:Fixing macOS Zsh Terminal History Settings by euoia on Thursday October 13, 2022 @04:28AM (#33188606)
If you have time machine, you may be able to recover the old history file.
Re:Fixing macOS Zsh Terminal History Settings by saagarjha on Thursday October 13, 2022 @05:12AM (#33188857)
I lost two history files to the ether, and repaired several more with Time Machine before I got fed up with dealing with this anymore. (I still have Time Machine enabled, but I haven't had to restore my bash history from it in years.)
Re:Fixing macOS Zsh Terminal History Settings by mmsc on Thursday October 13, 2022 @10:18AM (#33191306)
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 by LucasLarson on Thursday October 13, 2022 @01:40PM (#33194169)
Zsh doesn’t actually have a default history-file location:⁰

HISTFILE The file to save the history in when an interactive shell exits. If unset, the history is not saved.

0. https://web.archive.org/web/20221012011928id_/zsh.sourceforg...