Skip to content

Instantly share code, notes, and snippets.

@RealYukiSan
Last active June 7, 2024 07:26
Show Gist options
  • Save RealYukiSan/7c986b96e37a15d6c71dfd241be42840 to your computer and use it in GitHub Desktop.
Save RealYukiSan/7c986b96e37a15d6c71dfd241be42840 to your computer and use it in GitHub Desktop.
A note for shell history management

Configuration

put these line on your .bashrc:

...
alias dedup='history -n; history -w; history -c; history -r;'

# https://superuser.com/a/664061/1867794
# Eternal bash history.
# ---------------------
# Undocumented feature which sets the size to "unlimited".
# https://stackoverflow.com/questions/9457233/unlimited-bash-history
HISTFILESIZE=
HISTSIZE=
HISTTIMEFORMAT="[%F %T] "
# Change the file location because certain bash sessions truncate .bash_history file upon close.
# http://superuser.com/questions/575479/bash-history-truncated-to-500-lines-on-each-login
HISTFILE=~/.bash_eternal_history

# https://askubuntu.com/a/667787
HISTCONTROL=ignoredups:ignorespace:erasedups
shopt -s histappend
shopt -s cmdhist
function historymerge {
    dedup
}
trap historymerge EXIT
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

Shell

type set +o history on the shell to disable the history feature

type trap | awk '{ print $NF }' to list active trap

type trap - EXIT to unset the pre-defined trap from .bashrc

type history | less to view the history file

Known Bugs

The problem occurs when: you opened two or more terminal, editing history file in the first terminal and working for other stuff in other terminal

The changes made in the history file will not applied if the dedup is executed (which will be executed soon as EXIT by trap triggered) unless you disable the trap.

But if you disable it, then the history file stored in the memory on each terminal session will ignore any duplicate entries and not removing it.

keep in mind that de-duplicate entries feature only works if in the current shell session you've already type the same command at least once.

@RealYukiSan
Copy link
Author

RealYukiSan commented Jun 7, 2024

every thing has its own history

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment