Is there a way one could make some commands "sticky" in a shell history?
I want to save some favourite commands that would be searchable with Ctrl+R, like the rest of the history, but they should never expire.
bashcommand historyshellzsh
Is there a way one could make some commands "sticky" in a shell history?
I want to save some favourite commands that would be searchable with Ctrl+R, like the rest of the history, but they should never expire.
Best Answer
A simple way is to put code in your shell startup file to read an additional history file, and make sure that you store sufficiently many history lines in memory so that the sticky lines aren't forgotten.
bash
In
~/.bashrc
:Also make sure that
HISTSIZE
is at leastHISTFILESIZE
plus the number of lines in~/.bash_history.sticky
plus the number of commands you execute in a long session, e.g.If you want to ensure that the sticky history entries remain in memory without having a very large
HISTSIZE
, you can do it by manually trimming the history inPROMPT_COMMAND
withhistory -d
, but it's difficult to get right if you haveerasedups
inHISTCONTROL
.zsh
In
~/.zshrc
:Also make sure that
HISTSIZE
is at leastSAVEHIST
plus the number of lines in~/.zsh_history.sticky
plus the number of commands you execute in a long session, e.g.If you want to ensure that the sticky history entries remain in memory without having a very large
HISTSIZE
, you can do it by manually trimming the history inprecmd
, but it's cumbersome (zsh doesn't really support rewriting history, you have tofc -W
into a temporary file and read back an edited version) and difficult to get right if you have thehist_ignore_dups
orhist_ignore_all_dups
option set.