From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 28034 invoked by alias); 19 Nov 2012 00:39:55 -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: 17415 Received: (qmail 20217 invoked from network); 19 Nov 2012 00:39:53 -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=-3.3 required=5.0 tests=BAYES_00,DKIM_ADSP_ALL, DKIM_SIGNED,RCVD_IN_DNSWL_MED,T_DKIM_INVALID,UNPARSEABLE_RELAY autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at spodhuis.org does not designate permitted sender hosts) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=spodhuis.org; s=d201210; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=RMqnc4Mn6BkWX0lBYlIp6cwJuRDzw3U9Ux3kVBda+Ro=; b=AYvfUZ8/K2QV5aeMPHbzdpd7Sz7KvVZIPHhVZepc/SM+EpriPnFokmp+/XsrXSOoBY5OdFrxULDLU5mRdxENj1O8imLRRWm/oU4ClZF6YpGIR+gNeHksqAn+DqrUU3UwEmZyLUngMc16Yp0qRecR86WtGJ4Q1H31OtS0XxGdBZ0=; Date: Sun, 18 Nov 2012 19:22:19 -0500 From: Phil Pennock To: Ray Andrews Cc: zsh-users@zsh.org Subject: Re: sharing environment between terminals. Message-ID: <20121119002219.GA47996@redoubt.spodhuis.org> Mail-Followup-To: Ray Andrews , zsh-users@zsh.org References: <50A82E58.4020104@eastlink.ca> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <50A82E58.4020104@eastlink.ca> 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. 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------------------------------