There are a handful of commands which I use Ctrl+R to lookup.

In some cases it makes sense to put them into aliases/shell scripts, however for commands that typically need some adjusting - this isn't practical (involved ssh/git commands for example).

Is there a way to load in commands on top of regular history (which may have a limited length). So I can clear history, keeping useful commands always accessible from Ctrl+R search?

Is there a function to prepend history or load in history from a separate file that is never written to?

I realize this may seem obscure, however very occasionally I loose my history (it becomes corrupt or gets overwritten somehow), and I end up loosing handy commands I only use occasionally. While I can keep a backup, I'd rather be clearing history every so often - and keeping handy commands in a separate file.

  • I would instead recommend defining several zsh functions in your ~/.zshrc Oct 21 '17 at 5:05
  • I do this already, as noted in the question, some commands need edits on each use - so don't make much sense to wrap into an alias/function/script. While they could take arguments, this means I'm writing my own commands with their own argument syntax - which is too much overhead for my taste. Said differently, there are times when I'd rather use history search.
    – ideasman42
    Oct 21 '17 at 6:13

With zsh, you can add history entries to the current history, from a file, using

fc -R yourfile

You can add -I to only load entries which aren’t already present.

fc -W can be used to write history to a file. See man zshbuiltins or the online manual for details.

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.