Hacker News new | past | comments | ask | show | jobs | submit login

Interesting.

For me, C-r is sufficient (and/or M-r in Emacs, where C-r by default does reverse interactive search on the text in buffer; it's nice to have both at the same time, actually). However, I must have skipped some education about shell history and/or its default settings, because half the time I need it, the command I want isn't there to be found. I also observed the following kinds of behaviors:

- Sometimes, shell history seems to be scoped (or reacting to) current working directory;

- Sometimes, commands executed on remote machines end up being saved in local history;

- When the shell gets killed (e.g. when Emacs crashes and takes down the shells open inside with it), or the system crashes, sometimes the history gets saved, and sometimes nothing remains from a session that spanned multiple days;

- When I have multiple terminals open, it's a coin toss whether only one will have history saved or all of them, and then another toss as to whether histories will be CWD-sensitive or not.

Is there a good primer/community consensus on how to configure shell so all history gets saved in sensible manner (including continuously, so it survives a crash)?




> Is there a good primer/community consensus on how to configure shell so all history gets saved in sensible manner (including continuously, so it survives a crash)?

I find that zsh is quite good at addressing some of the issues you mention.


Just gotta set your bashrc!

   # Ensure history is written after every command, allowing it to persist across sessions
   shopt -s histappend  # Append to history instead of overwriting
   PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

   # Set the history size (adjust as needed)
   HISTSIZE=50000   # Number of commands kept in memory
   HISTFILESIZE=1000000  # Number of commands kept in the history file

   # Add timestamps to history (format: YYYY-MM-DD HH:MM:SS)
   HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "

   # Ignore duplicate and space-prefixed commands
   HISTCONTROL=ignoredups:ignorespace

   # Save multi-line commands as a single entry
   shopt -s cmdhist

   # Allow history expansion with Ctrl + R
   bind '"\e[A":history-search-backward'
   bind '"\e[B":history-search-forward'
Also, if you are on shells you worry about, use gnu screen (or tmux if you prefer) imho. It will give you a second layer of history to be able to dig through.


Install fzf and ctrl-r will become much better.


Fzf has been a district before and after type tool for me. There's so many use cases for it, and the fuzzy search in history is amazing.


Yeah, for example I've started looking at making tui:s in go using tview and here fzf-tmux works great as a (multi-)select fuzzy find popup.


You should definitely give atuin a try. It addresses all these things really well.


This will do what you want: Append this to your .bashrc.

```export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"```

-a = append to history file -c = 'clear' aka write the history file -r = reload

Every time you hit 'enter' this will add the command to the history file, and will reload your bash prompt history. That means that every terminal window will share a common history file and you don't need to close the TERM to 'save' the session to history.

This is ESPECIALLY helpful on remote (SSH) connections. Downside: You 'Up Arrow' history gets altered by every term window you have running. So you can't just blindly `up` to repeat a command.

It takes a little to get used to, but VERY handy.

This allows EV


There are shell options you need to set to, for example, make shell history saving work when multiple terminals are used (the defaults are bad). Read the manual


Fish shell does not have this problems


For bash there’s oh-my-bash that comes with sensible defaults: https://ohmybash.nntoan.com/


.. have added a gist in comments below, i got used to saving separate history files for each project and launch gnome-terminal with that .. and Ctrl-R within that scope




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: