106

How do I tell ZSH to reload itself, as if it's a freshly invoked shell, but without losing the history?

The context for this is that I've spent a long time building my ZSH setup, and I'd hate to lose it if my current machine fails, or the drive gets corrupted, etc. To this end, I've put all my local ZSH config files in a git repository. Nothing new so far.

But now I want to add an 'install' script to the repository, to ease the process of installing my setup on a new machine. Once the files are installed (actually, symlinks created in ${ZDOTDIR-~} that point to the files in the repository), I want the script to reload them, without replacing the current process via exec (and therefore losing the history), and without sourcing the files one-by-one (and risking the possibility that I may load them in the wrong order or miss some other part of the ZSH startup process).

Is there some facility built in to ZSH, or some other way to tell it to reload everything, as if it was a freshly started instance, while preserving the command history?

EDIT: Hah. Um.. hrm.. of course, I've now wiped out my .zshrc and .zlogin, Though the latter is no great hardship (It just had RVM's setup in it, which is easily recovered). The former, however, hurts. Anyone who can tell me how to recover a .zshrc from a shell that has sourced it gets all my super bonus points. An answer to the original question will still be marked as accepted, of course.

I had a problem. I wrote a shell script. Now I have two problems :)

0

6 Answers 6

174

Usually a source ~/.zshrc should do it.

2
  • 2
    Do source ~/.zshenv to reload stuff from that file.
    – Jonny
    Commented Sep 16, 2021 at 0:38
  • 4
    This doesn't reload aliases on macOS. Use exec zsh to launch a new shell instead. Commented Feb 29 at 5:51
84

For oh-my-zsh users

Here is the way to reload the .zshrc without losing the terminal

omz reload

Found here: https://github.com/ohmyzsh/ohmyzsh/wiki/Cheatsheet

5
  • not working for me
    – ng10
    Commented Nov 25, 2022 at 15:05
  • @ng10 Which version of omz are you using ? did you checked that omz was enabled by default / the terminal app you are using ?
    – G. Frx
    Commented Jan 13, 2023 at 10:24
  • thank you - my bad. did not notice this was a specific answer for omz
    – ng10
    Commented Jan 15, 2023 at 16:04
  • oddly calling source didn't work in my case (changed alias to a function) but this did
    – jcollum
    Commented Mar 22, 2023 at 16:31
  • Note that omz reload is the same as exec zsh. So if you do not have oh-my-zsh installed you can just useexec zsh which is 2 characters shorter BTW.
    – PRouleau
    Commented Mar 12 at 13:27
22

You can enable the option INC_APPEND_HISTORY. From the manpage:

INC_APPEND_HISTORY

This options works like APPEND_HISTORY except that new history lines are added to the $HISTFILE incrementally (as soon as they are entered), rather than waiting until the shell exits. The file will still be periodically re-written to trim it when the number of lines grows 20% beyond the value specified by $SAVEHIST (see also the HIST_SAVE_BY_COPY option).

This way you can then do exec zsh without losing history.

3
  • 2
    This is the closest thing I think is probably possible to what I was trying to do. Much obliged.
    – Jon Carter
    Commented Dec 21, 2017 at 9:43
  • How can I enable INC_APPEND_HISTORY?
    – alper
    Commented Aug 11, 2021 at 22:36
  • 2
    @alper Add setopt inc_append_history to your ~/.zshrc.
    – JoL
    Commented Aug 12, 2021 at 3:31
11

This is a bit older, but you can always add a function to your .zshrc that sources all config files for you.

function reload(){
       source ~/.zshrc
       source ~/.zshenv
       ...
   }

So all you'd have to do is to reload in your shell.

2
  • 5
    The order matters. Usually .zshenv is read before .zshrc. Commented Jan 2, 2021 at 13:55
  • If you do this over and over again the shell slowes down
    – alper
    Commented Aug 11, 2021 at 22:34
6

If

source .zshrc

outputs

source: no such file or directory: .zshrc,

you should run below command instead

. ~/.zshrc
1
  • 2
    you just need to source the full path in that case: source ~/.zshrc
    – taj
    Commented Jul 19, 2022 at 17:49
2

To respect the ZDOTDIR, which should be set by .zshenv, use this combination:

source $HOME/.zshenv && source ${ZDOTDIR:-$HOME}/.zshrc

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