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