4

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.

  • you've tagged bash and zsh; are you looking for something across shells, or for one in particular? – Jeff Schaller Jan 22 '16 at 20:30
  • Anything, really :) It might be the killer feature that will convince me to switch back to bash. – WGH Jan 22 '16 at 20:31
  • 2
    I used to be very reliant on Ctrl-R and went to a lot of effort to ensure that my history was never over-written by multiple simultaneous sessions or lost after a crash. I eventually came to embrace aliases for storing favoured commands permanently. – Anthony G - justice for Monica Jan 22 '16 at 20:41
4

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:

history -r ~/.bash_history.sticky

Also make sure that HISTSIZE is at least HISTFILESIZE plus the number of lines in ~/.bash_history.sticky plus the number of commands you execute in a long session, e.g.

HISTFILESIZE=1000
HISTSIZE=10000

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 in PROMPT_COMMAND with history -d, but it's difficult to get right if you have erasedups in HISTCONTROL.

zsh

In ~/.zshrc:

fc -RI ~/.zsh_history.sticky

Also make sure that HISTSIZE is at least SAVEHIST plus the number of lines in ~/.zsh_history.sticky plus the number of commands you execute in a long session, e.g.

SAVEHIST=1000
HISTSIZE=10000

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 in precmd, but it's cumbersome (zsh doesn't really support rewriting history, you have to fc -W into a temporary file and read back an edited version) and difficult to get right if you have the hist_ignore_dups or hist_ignore_all_dups option set.

  • Despite some little quirks you mentioned, it's exactly what I was looking for. Thanks! – WGH Jan 25 '16 at 10:59
1

My solution is a cron job and a big number of lines in .*sh_history file

at the end of the day this cron entry runs (mine is set to 11 PM)

0 23 * * * cat /home/username/.bash_history >> /home/username/BIGHISTORY

If the sticky command happens to get out of the regular history scroll buffer, I can find it in BIGHISTORY file with a simple grep command and bring it back to the fold. I know this is not what you are looking for but this is the poor-man's version, that comes closest with minimal effort.

1

In bash (don't know about zsh), you can set

export HISTFILESIZE=-1
export HISTSIZE=-1

in your .bashrc to make your history forever. Not exactly what you're looking for, I know. For your favorite commands, I suggest to create aliases or functions, though.

  • I'd still like other commands in the history to expire, though. – WGH Jan 22 '16 at 20:47
0

In Zsh, I retain history for a long time. The HISTSIZE is 20000. (There are various settings involved in that, so I won’t list them here.) That buys me ~10 months worth. My history file (~/.zhistory) weighs in at 1.1 MiB and doesn’t make startup noticeably slower.

I commonly pull in working history from other terminals with fc -R (when multiples are open — always).

But to answer your question, I “stamp” the commands I really care about saving with a # GOOD comment on the end. These are often lengthy and hard-won, after a lot of experimenting. Some of them do end up becoming little reusable scripts, but a stamp is the quickest way to save it. With the long history, I can just do a history search for # GOOD, and usually come up with what I’m looking for pretty quickly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.