Periodically source shell profile

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.

swrobel

Posted 2016-02-18T23:54:57.730

Reputation: 325

Answers

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

Gombai Sándor

Posted 2016-02-18T23:54:57.730

Reputation: 3 325

Very interesting approach. However, I'd rather use killall -s USR1 zsh than this ps-awk-xargs construct. – mpy – 2016-02-20T09:12:03.753

Valid correction. Thx! – Gombai Sándor – 2016-02-20T09:14:45.150

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\$"

Paul

Posted 2016-02-18T23:54:57.730

Reputation: 52 173

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 – 2016-02-19T01:13:40.233

@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 – 2016-02-19T01:22:12.880

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 – 2016-02-19T01:58:23.197

Sorry, fixed the typos. @swrobel – Paul – 2016-02-19T02:29:43.960

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.

dan

Posted 2016-02-18T23:54:57.730

Reputation: 247