Repository: xonsh/xonsh

Issue:
preserve path in the new shell tab
is there a way to preserve path in the new tab of gnome-shell for example by any chance? 



Discussion:
do you mean preserve the PWD? if that's the case try this in a different terminal emulator, this happens for me in terminator but not in gnome-shell

If gnome-terminal has a hook that we can use, we'd be happy to have it. Could you try to figure out how that works please, @guy-do-or-die?

Related discussion: https://groups.google.com/forum/#!topic/xonsh/Cv2AWFYOpeQ

Something that might help with this is to draw inspiration from `/etc/profile.d/vte.sh`, which is how one makes `gnome-terminal` open new tabs/windows in the present working directory for bash and zsh:

``` bash
# Copyright © 2006 Shaun McCance <shaunm@gnome.org>
# Copyright © 2013 Peter De Wachter <pdewacht@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Not bash or zsh?
[ -n "$BASH_VERSION" -o -n "$ZSH_VERSION" ] || return 0

# Not an interactive shell?
[[ $- == *i* ]] || return 0

# Not running under vte?
[ "${VTE_VERSION:-0}" -ge 3405 ] || return 0

__vte_urlencode() (
  # This is important to make sure string manipulation is handled
  # byte-by-byte.
  LC_ALL=C
  str="$1"
  while [ -n "$str" ]; do
    safe="${str%%[!a-zA-Z0-9/:_\.\-\!\'\(\)~]*}"
    printf "%s" "$safe"
    str="${str#"$safe"}"
    if [ -n "$str" ]; then
      printf "%%%02X" "'$str"
      str="${str#?}"
    fi
  done
)

# Print a warning so that anyone who's added this manually to his PS1 can adapt.
# The function will be removed in a later version.
__vte_ps1() {
  echo -n "(__vte_ps1 is obsolete)"
}

__vte_osc7 () {
  printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "$(__vte_urlencode "${PWD}")"
}

__vte_prompt_command() {
  local pwd='~'
  [ "$PWD" != "$HOME" ] && pwd=${PWD/#$HOME\//\~\/}
  printf "\033]0;%s@%s:%s\007%s" "${USER}" "${HOSTNAME%%.*}" "${pwd}" "$(__vte_osc7)"
}

case "$TERM" in
  xterm*|vte*)
    [ -n "$BASH_VERSION" ] && PROMPT_COMMAND="__vte_prompt_command"
    [ -n "$ZSH_VERSION"  ] && precmd_functions+=(__vte_osc7)
    ;;
esac

true
```


Resolving patch:
diff --git a/xonsh/environ.py b/xonsh/environ.py
--- a/xonsh/environ.py
+++ b/xonsh/environ.py
@@ -1170,6 +1170,19 @@ def env_name(pre_chars='(', post_chars=')'):
     USER = 'USER'
 
 
+def vte_new_tab_cwd():
+    """This prints an escape squence that tells VTE terminals the hostname
+    and pwd. This should not be needed in most cases, but sometimes is for
+    certain Linux terminals that do not read the PWD from the environment
+    on startup. Note that this does not return a string, it simply prints
+    and flushes the escape sequence to stdout directly.
+    """
+    env = builtins.__xonsh_env__
+    t = '\033]7;file://{}{}\007'
+    s = t.format(env.get('HOSTNAME'), env.get('PWD'))
+    print(s, end='', flush=True)
+
+
 FORMATTER_DICT = LazyObject(lambda: dict(
     user=os.environ.get(USER, '<user>'),
     prompt_end='#' if is_superuser() else '$',
@@ -1183,6 +1196,7 @@ def env_name(pre_chars='(', post_chars=')'):
     branch_bg_color=branch_bg_color,
     current_job=_current_job,
     env_name=env_name,
+    vte_new_tab_cwd=vte_new_tab_cwd,
     ), globals(), 'FORMATTER_DICT')
 
 


Test patch: