0

I use the history / CtrlR thing quite a bit, but from time to time either

  • a command I use regularly (but not regularly enough, apparently) "fades out" behind my command history threshold, or

  • some system hang-up caused me to hard-reset the machine and the history log becomes a garbled mess

Now is there a way to make entries in history "stick", so CtrlR doesn't rely on a specific pattern to be present in history log? Can I define "default entries" in my .dotfiles somewhere, like .bashrc, or history's config?

I know there's the option to have the history threshold set to infinity, but I don't want that.

2

I used to be heavily reliant on using CtrlR to recall previously entered commands – both commonly used commands and long complicated ones. I encountered very similar issues to those you describe.

To overcome these problems, I modified my Bash configuration so that when the shell exits it appends to the history file rather than over-write it:

shopt -s histappend

However, if the shell didn’t exit cleanly (as in a hard reset), I’d still lose the history for that session so I added the following to save the history after each command is entered.

PROMPT_COMMAND='history -a'

Shell aliases

In the end, I came to embrace shell aliases as the best way to save the commonly used commands and those long complicated ones that take time to get right. As long as they’re saved to my aliases file, they’ll always be available to me – even years later.

I store all my aliases in ~/.bash_aliases and then source that file from my .bashrc as suggested by the default .bashrc provided by Ubuntu:

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi
  • That bit about .bash_aliases is also in the default .bashrc. – muru Jun 8 '16 at 19:56
  • @muru That's where I got the idea. Cygwin's default also has something very similar. I've now edited my question to provide attribution -- and thanks for the keyboard tags. – Anthony Geoghegan Jun 8 '16 at 20:08
  • 1
    So the answer is: "No"? No way to make commands sticky?? ;) -- Well, the second part of your post is very helpful, as a workaround seems to be the only way. Apart from shell aliases, I had the idea to have a small startup-script that injects my list of common commands into history's log every now and then. -- The first part is a discussion I've already heard when I first encountered a garbled history log and investigated solutions, but I'm not such a fan of histappend or even write after each command... Seems I just have to keep handy commands separate of history somewhere. – isync Jun 8 '16 at 23:10
1

Not sticky, but prolong their life in history:

#store not 2k commands but 20k, feel free to increase if needed
export HISTFILESIZE=20000
export HISTSIZE=20000
#don't store duplicates, so save some space too
export HISTCONTROL="ignoredups"

Should be added to .bashrc

And yes, aliases are very helpful.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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