zsh-users
 help / color / mirror / code / Atom feed
* sharing environment between terminals.
@ 2012-11-18  0:39 Ray Andrews
  2012-11-18 18:35 ` Bart Schaefer
  2012-11-19  0:22 ` Phil Pennock
  0 siblings, 2 replies; 7+ messages in thread
From: Ray Andrews @ 2012-11-18  0:39 UTC (permalink / raw)
  To: zsh-users

All,

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.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  2012-11-18  0:39 sharing environment between terminals 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-19  0:22 ` Phil Pennock
  1 sibling, 2 replies; 7+ messages in thread
From: Bart Schaefer @ 2012-11-18 18:35 UTC (permalink / raw)
  To: zsh-users

On Nov 17,  4:39pm, Ray Andrews wrote:
}
} Is there a way to instantly share environment variables between running 
} terminals?

Generally speaking, no.  The environment of each process is private to
that process, allocated by the operating system at the time the process
is forked.

If desperate enough, you could set up something using the zsh/tcp module
to have your shells cooperatively swap new environment strings around.
One shell (or an external daeamon of some kind -- condsider the model
used by ssh-agent) would have to control the "master" environment, and
hand out changes to the other shells on request.  The other shells would
check at opportune times (during precmd for example) whether changes are
available, and apply them.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  2012-11-18 18:35 ` Bart Schaefer
@ 2012-11-18 19:22   ` Ray Andrews
  2012-11-18 19:33   ` Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: Ray Andrews @ 2012-11-18 19:22 UTC (permalink / raw)
  To: zsh-users

On 18/11/12 10:35 AM, Bart Schaefer wrote:
> On Nov 17,  4:39pm, Ray Andrews wrote:
> }
> } Is there a way to instantly share environment variables between running
> } terminals?
>
> Generally speaking, no.  The environment of each process is private to
> that process, allocated by the operating system at the time the process
> is forked.
>
> If desperate enough, you could set up something using the zsh/tcp module
> to have your shells cooperatively swap new environment strings around.
> One shell (or an external daeamon of some kind -- condsider the model
> used by ssh-agent) would have to control the "master" environment, and
> hand out changes to the other shells on request.  The other shells would
> check at opportune times (during precmd for example) whether changes are
> available, and apply them.
>
Most of that sounds way above my head for the moment.  But I did get it 
working quite well using, as you suggest, precmd(). Basically, I have a 
command that sets whatever env. variable and also writes a script that 
can set that variable.  precmd() sources that script, which adds the 
variable to the local env of any xterm.

I use this to create shortcuts to the current directory on each xterm. 
'$t0' being automatically set to the current dir on 'pts/0', etc. I can 
then change to a different terminal and do stuff like:

cp ./* $t0
cd $t5
mv $t2/* $t3
etc.

... which saves a lot of typing.

Still, I would have thought that there would be an easy way to share 
stuff between xterms. I think of my xterms as different windows into the 
same house--I want them to share everything--history, environment, 
everything.



^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  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
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2012-11-18 19:33 UTC (permalink / raw)
  To: zsh-users

On Sun, 18 Nov 2012 10:35:14 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Nov 17,  4:39pm, Ray Andrews wrote:
> }
> } Is there a way to instantly share environment variables between running 
> } terminals?
> 
> Generally speaking, no.  The environment of each process is private to
> that process, allocated by the operating system at the time the process
> is forked.
> 
> If desperate enough, you could set up something using the zsh/tcp module
> to have your shells cooperatively swap new environment strings around.
> One shell (or an external daeamon of some kind -- condsider the model
> used by ssh-agent) would have to control the "master" environment, and
> hand out changes to the other shells on request.  The other shells would
> check at opportune times (during precmd for example) whether changes are
> available, and apply them.

It might be easier to intercept typeset etc. and turn them into shell
functions that save the environment.  You'd probably need to do some
fairly nifty history-style timestamping to ensure you didn't simply
reimport the entire environment each time, however --- particularly to
ensure you got the latest results over all shells.

Hmm, I wonder if doing something clever examining the actual saved
history (with INC_APPEND_HISTORY set) would help?

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  2012-11-18 19:33   ` Peter Stephenson
@ 2012-11-18 23:11     ` Ray Andrews
  0 siblings, 0 replies; 7+ messages in thread
From: Ray Andrews @ 2012-11-18 23:11 UTC (permalink / raw)
  To: zsh-users

On 18/11/12 11:33 AM, Peter Stephenson wrote:
> On Sun, 18 Nov 2012 10:35:14 -0800
> Bart Schaefer <schaefer@brasslantern.com> wrote:
>> On Nov 17,  4:39pm, Ray Andrews wrote:
>> }
>> } Is there a way to instantly share environment variables between running
>> } terminals?
>>
>> Generally speaking, no.  The environment of each process is private to
>> that process, allocated by the operating system at the time the process
>> is forked.
>>
>> If desperate enough, you could set up something using the zsh/tcp module
>> to have your shells cooperatively swap new environment strings around.
>> One shell (or an external daeamon of some kind -- condsider the model
>> used by ssh-agent) would have to control the "master" environment, and
>> hand out changes to the other shells on request.  The other shells would
>> check at opportune times (during precmd for example) whether changes are
>> available, and apply them.
> It might be easier to intercept typeset etc. and turn them into shell
> functions that save the environment.  You'd probably need to do some
> fairly nifty history-style timestamping to ensure you didn't simply
> reimport the entire environment each time, however --- particularly to
> ensure you got the latest results over all shells.
>
> Hmm, I wonder if doing something clever examining the actual saved
> history (with INC_APPEND_HISTORY set) would help?
>
Peter,

For now, I'm not going to get out of my depth with things. My nastly 
little hack does what I need it to do, so I'm going to rest with that 
for now unless there is a 'lite' fix that I can understand.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  2012-11-18  0:39 sharing environment between terminals Ray Andrews
  2012-11-18 18:35 ` Bart Schaefer
@ 2012-11-19  0:22 ` Phil Pennock
  2012-11-19 16:23   ` Ray Andrews
  1 sibling, 1 reply; 7+ messages in thread
From: Phil Pennock @ 2012-11-19  0:22 UTC (permalink / raw)
  To: Ray Andrews; +Cc: zsh-users

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------------------------------


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: sharing environment between terminals.
  2012-11-19  0:22 ` Phil Pennock
@ 2012-11-19 16:23   ` Ray Andrews
  0 siblings, 0 replies; 7+ messages in thread
From: Ray Andrews @ 2012-11-19 16:23 UTC (permalink / raw)
  To: zsh-users

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------------------------------
>


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-11-19 16:54 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-18  0:39 sharing environment between terminals 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
2012-11-19 16:23   ` Ray Andrews

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).