3

I'm a zsh user, but this should apply to any shell in theory. When I edit my .zshrc to add a new alias, I have to source ~/.zshrc in all of my open terminal tabs/windows to be able to use the new alias. I'm wondering if there's some way to have my open shells periodically source my profile automatically.

2

The most complex and most real-time solution is to write a user trap function in .zshrc which when the trapped signal is caught, sources .zshrc itself.

Like this:

~$ cat .zshrc

trap includerc USR1

includerc() {
   source $HOME/.zshrc
}


alias xxx='df'

Within zsh, this can be seen now:

zshsession> alias
which-command=whence
xxx=df

Now some change in .zshrc

~$ cat .zshrc

trap includerc USR1

includerc() {
   source $HOME/.zshrc
}


alias xxx='df -g'
alias yyy='someothercommand'

Here comes the tricky part. Using inotify watching .zshrc or by a watcher script that examines the last modification of .zshrc a command is triggered that sends USR1 signal to all zsh processes of the user. Now I just ran it in another shell:

~$ ps -a | awk '$4=="zsh" {print $1}' | xargs kill -SIGUSR1

And the result is:

zshsession> alias
which-command=whence
xxx='df -g'
yyy=someothercommand
  • Very interesting approach. However, I'd rather use killall -s USR1 zsh than this ps-awk-xargs construct. – mpy Feb 20 '16 at 9:12
  • Valid correction. Thx! – Gombai Sándor Feb 20 '16 at 9:14
1

How about aliasing the command you use regularly?

You may need to adapt this for zsh:

myvi() {

    vi $1 
    source ~/.zshrc
}

alias vi=myvi

So when you vi anything, it will source ~/.zshrc

You could do this with cd or ls for example.

update

Another option would be to build it into your prompt:

export PS1="\$(source ~\.zshrc)\u@\h:\w\$"
  • Thanks for the suggestions! I've already built it into the command I use to edit my profile, but that only helps with the shell that I edited it from. Building it into my prompt seems more promising... Have you tried that? – swrobel Feb 19 '16 at 1:13
  • @swrobel No, I haven't tried it specifically, but it should work fine - you must escape the first $ otherwise it only runs the first time – Paul Feb 19 '16 at 1:22
  • Hmm, the export results in the following error and no prompt: /Users/swrobel/.zshrc:export:8: not valid in this context: ~.zshrc}u@h:w$ zsh: bad substitution – swrobel Feb 19 '16 at 1:58
  • Sorry, fixed the typos. @swrobel – Paul Feb 19 '16 at 2:29
0

You are absolutly right, this function might be constructed in any shell, and so since more than a quarter of century.

It is not, and from my point of vue here is why. Such a function could lead to a fatal termination of all shells running in case of a slightest error. Example: you have a code section for debugging which contains

exit 0

and during editing session of your .zshrc you removed the code protecting this exit to make it the general rule. All your running zsh will end. You won't be able to start another one with the same userid to fix your .zshrc. You will have to use another account which is sudo enabled to fix the killer .zshrc.

Then the best practice is to always test a .zshrc in a newly created shell so as to be able to survive.

This missing function is a very wise one.

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.