zsh-users
 help / color / mirror / code / Atom feed
From: Phil Pennock <zsh-workers+phil.pennock@spodhuis.org>
To: Ray Andrews <rayandrews@eastlink.ca>
Cc: zsh-users@zsh.org
Subject: Re: sharing environment between terminals.
Date: Sun, 18 Nov 2012 19:22:19 -0500	[thread overview]
Message-ID: <20121119002219.GA47996@redoubt.spodhuis.org> (raw)
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------------------------------


  parent reply	other threads:[~2012-11-19  0:39 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-18  0:39 Ray Andrews
2012-11-18 18:35 ` Bart Schaefer
2012-11-18 19:22   ` Ray Andrews
2012-11-18 19:33   ` Peter Stephenson
2012-11-18 23:11     ` Ray Andrews
2012-11-19  0:22 ` Phil Pennock [this message]
2012-11-19 16:23   ` Ray Andrews

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20121119002219.GA47996@redoubt.spodhuis.org \
    --to=zsh-workers+phil.pennock@spodhuis.org \
    --cc=rayandrews@eastlink.ca \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).