How do I apply the changes to the .zshrc file after editing it?

Restart zsh

Zsh reads .zshrc when it starts. You don't need to log out and log back in. Just closing the terminal and opening a new one gives you your new .zshrc in this new terminal. But you can make this more direct. Just tell zsh to relaunch itself:

exec zsh

If you run this at a zsh prompt, this replaces the current instance of zsh by a new one, running in the same terminal. The new instance has the same environment variables as the previous one, but has fresh shell (non-exported) variables, and it starts a new history (so it'll mix in commands from other terminals in typical configurations). Any background jobs are disowned.

Reread .zshrc

You can also tell zsh to re-read .zshrc. This has the advantage of preserving the shell history, shell variables, and knowledge of background jobs. But depending on what you put in your .zshrc, this may or may not work. Re-reading .zshrc runs commands which may not work, or not work well, if you run them twice.

. ~/.zshrc

There are just too many things you can do to enumerate everything that's ok and not ok to put in .zshrc if you want to be able to run it twice. Here are just some common issues:

  • If you append to a variable (e.g. fpath+=(~/.config/zsh) or chpwd_functions+=(my_chpwd)), this appends the same elements again, which may or may not be a problem.
  • If you define aliases, and also use the same name as a command, the command will now run the alias. For example, this works:
    function foo { … }
    alias foo='foo --common-option'
    
    But this doesn't, because the second time the file is sourced, foo () will expand the alias:
    foo () { … }
    alias foo='foo --common-option'
    
  • If you patch an existing zsh function, you'll now be patching your own version, which will probably make a mess.
  • If you do something like “swap the bindings of two keys”, that won't do what you want the second time.

Changes to a shells initialisation files will be active in the next shell that you start, for example if you bring up a new graphical terminal or log out and in again. If you've made changes that should affect your desktop environment in some way (I don't know what kind of change that may be), then logging out and in again would be required.

You could source the file with . /path/to/filename (. ~/.zshrc in your case) or start a new shell session from the command line with zsh, but this is almost never a good idea as it may have unwanted consequences such as adding duplicate paths to the $PATH variable or starting extra ssh-agent processes or whatever it is you may be doing in that file. The changes would also not be visible program that have already been started.

For a change such as just adding to the $PATH unconditionally, you could obviously just run the added command in the current shell:

$ path+=/usr/local/openjdk12/bin

This would (in zsh) add the /usr/local/openjdk12/bin directory to the end of $PATH (and to the end of the $path array in zsh) in the current shell session. Again, this change to $PATH would not affect already running processes.


You could source the new file, which would work for some changes, possibly including updating the PATH variable (depending on other lines). However, sourcing it would simply run .zshrc again, and you might execute unexpected duplicate commands. Moreover, if there were deleted lines from the old .zshrc, then they wouldn't be "erased" from the session.

The cleanest way is to just log out and in again. You'd only need to do it for the terminal session, not the whole desktop environment.

Tags:

Zsh

Dot Files