From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 506 invoked by alias); 19 Nov 2012 16:54:15 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17416 Received: (qmail 6781 invoked from network); 19 Nov 2012 16:53:59 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.eastlink.ca designates 24.224.136.13 as permitted sender) X-Authority-Analysis: v=2.0 cv=FqZdZBXq c=1 sm=1 a=qvRTcpG2qjghBEcbUGMjgg==:17 a=-1ZKr4ocusoA:10 a=ZZQV8jkXHa0A:10 a=8nJEP1OIZ-IA:10 a=_lcLBkvvF3UA:10 a=ap9U7y3AxbmQfmi6X4IA:9 a=wPNLvfGTeEIA:10 a=qvRTcpG2qjghBEcbUGMjgg==:117 Message-id: <50AA5D19.4080501@eastlink.ca> Date: Mon, 19 Nov 2012 08:23:53 -0800 From: Ray Andrews User-Agent: Mozilla/5.0 (X11; Linux i686; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 MIME-version: 1.0 To: zsh-users@zsh.org Subject: Re: sharing environment between terminals. References: <50A82E58.4020104@eastlink.ca> <20121119002219.GA47996@redoubt.spodhuis.org> In-reply-to: <20121119002219.GA47996@redoubt.spodhuis.org> Content-type: text/plain; charset=ISO-8859-1; format=flowed Content-transfer-encoding: 7bit On 18/11/12 04:22 PM, Phil Pennock wrote: > On 2012-11-17 at 16:39 -0800, Ray Andrews wrote: >> Is there a way to instantly share environment variables between running >> terminals? I often have several open, and when I set an environment >> variable in one, I'd like it to be available in all the others too. >> 'export' doesn't help. > If you use tmux to act as your terminal multiplexer / keepalive, then > this seems to work. > > I just wrote it; this means there are probably bugs, beyond the known > limitations. Phil, This is over my head, but I'll keep it and give it some study. > Limitation: requires tmux 1.7 or greater; 1.6 doesn't support querying > individual environment variables. > > Limitation: if you update an environment variable, it's not present in > other terminals until after the next time you press return in that > shell. > > Limitation: It will invoke tmux before every prompt display, and will do > so N times, where N is 1 plus the number of variables named in the > TMUX_ZSH_SHARED tmux env variable. Optimisation left as an exercise for > the reader. (Probably reading the output of show-environment with no > named variables, so adds 1.6/earlier compatibility; beware that > environment values are shown raw by tmux, so embedded newlines in a > variable value will mess up your output, so it's probably only safe done > the way I did it). > > ----------------------------8< cut here >8------------------------------ > # Needs tmux 1.7 to be able to query individual environment variables > > if [[ -n $TMUX ]]; then > typeset -A latest_seen_tmuxenv > function precmd_tmux { > local exitstatus=$? > local e newenv > local -a shared overriden > e="$(tmux show-environment TMUX_ZSH_SHARED 2>/dev/null)" > [[ $? -ne 0 ]] && return $exitstatus > e="${e#TMUX_ZSH_SHARED=}" > shared=($=e) > for e in ${shared[@]}; do > if [[ -n "${(P)e}" && "${(P)e}" != "${latest_seen_tmuxenv[$e]}" ]]; then > overriden+=($e) > tmux set-environment "$e" "${(P)e}" > latest_seen_tmuxenv[$e]="${(P)e}" > else > newenv="$(tmux show-environment $e 2>/dev/null)" > if [[ $? -ne 0 ]]; then > if [[ -n "${latest_seen_tmuxenv[$e]}" ]]; then > unset $e > fi > unset latest_seen_tmuxenv[$e] > continue > fi > newenv="${newenv#$e=}" > if [[ "$newenv" != "${latest_seen_tmuxenv[$e]}" ]]; then > latest_seen_tmuxenv[$e]="$newenv" > typeset -g $e="$newenv" > fi > fi > done > return $exitstatus > } > > precmd_functions+=(precmd_tmux) > fi > > # tmux setenv TMUX_ZSH_SHARED "FOO BAR" > ----------------------------8< cut here >8------------------------------ >