From b4179c1dc64c2396887044a29ee7f5c630f15d5b Mon Sep 17 00:00:00 2001 From: admiralobvious Date: Mon, 16 Mar 2015 23:33:04 -0400 Subject: [PATCH 1/4] update oldpwd when using cd MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cd alias now updates $OLDPWD and also checks if the argument is a ‘-‘ (single dash) and cd to $OLDPWD if it is. Based on bash’s cd behaviour. http://ss64.com/bash/cd.html --- xonsh/aliases.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/xonsh/aliases.py b/xonsh/aliases.py index 20eb7a4d2..b67b1de3e 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -11,10 +11,14 @@ def cd(args, stdin=None): If no directory is specified (i.e. if `args` is None) then this changes to the current user's home directory. """ + cur_oldpwd = builtins.__xonsh_env__['OLDPWD'] + builtins.__xonsh_env__['OLDPWD'] = os.getcwd() if len(args) == 0: d = os.path.expanduser('~') elif len(args) == 1: d = args[0] + if d == '-': + d = cur_oldpwd else: return '', 'cd takes 0 or 1 arguments, not {0}\n'.format(len(args)) if not os.path.exists(d): From ca60a170a2e8d1de695053a4029e1033c0d8a2c3 Mon Sep 17 00:00:00 2001 From: Alexandre Ferland Date: Mon, 16 Mar 2015 23:54:27 -0400 Subject: [PATCH 2/4] Update aliases.py Move update to OLDPWD down so we don't overwrite it accidentally. --- xonsh/aliases.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xonsh/aliases.py b/xonsh/aliases.py index b67b1de3e..79fea03d1 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -12,7 +12,6 @@ def cd(args, stdin=None): changes to the current user's home directory. """ cur_oldpwd = builtins.__xonsh_env__['OLDPWD'] - builtins.__xonsh_env__['OLDPWD'] = os.getcwd() if len(args) == 0: d = os.path.expanduser('~') elif len(args) == 1: @@ -25,6 +24,7 @@ def cd(args, stdin=None): return '', 'cd: no such file or directory: {0}\n'.format(d) if not os.path.isdir(d): return '', 'cd: {0} is not a directory\n'.format(d) + builtins.__xonsh_env__['OLDPWD'] = os.getcwd() os.chdir(d) builtins.__xonsh_env__['PWD'] = os.getcwd() return None, None From cc143e28dc04adad99151697f5701375250ce7d2 Mon Sep 17 00:00:00 2001 From: Alexandre Ferland Date: Tue, 17 Mar 2015 11:22:50 -0400 Subject: [PATCH 3/4] check if oldpwd exists I use get() as you suggested now to check if OLDPWD exist before attempting to use it. At the same, I just use update() on the keys now instead of the previous builtins.__xonsh_env__[key] = value, thought it looks cleaner that way. --- xonsh/aliases.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/xonsh/aliases.py b/xonsh/aliases.py index 79fea03d1..0910ee140 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -11,7 +11,8 @@ def cd(args, stdin=None): If no directory is specified (i.e. if `args` is None) then this changes to the current user's home directory. """ - cur_oldpwd = builtins.__xonsh_env__['OLDPWD'] + env = builtins.__xonsh_env__ + cur_oldpwd = env.get('OLDPWD') or os.getcwd() if len(args) == 0: d = os.path.expanduser('~') elif len(args) == 1: @@ -24,9 +25,9 @@ def cd(args, stdin=None): return '', 'cd: no such file or directory: {0}\n'.format(d) if not os.path.isdir(d): return '', 'cd: {0} is not a directory\n'.format(d) - builtins.__xonsh_env__['OLDPWD'] = os.getcwd() + env.update({'OLDPWD': os.getcwd()}) os.chdir(d) - builtins.__xonsh_env__['PWD'] = os.getcwd() + env.update({'PWD': os.getcwd()}) return None, None def exit(args, stdin=None): From 4a6d1017faac079af063893b86d87b7202304393 Mon Sep 17 00:00:00 2001 From: Alexandre Ferland Date: Wed, 18 Mar 2015 20:32:18 -0400 Subject: [PATCH 4/4] change_update Set the value instead of update(). --- xonsh/aliases.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/xonsh/aliases.py b/xonsh/aliases.py index 0910ee140..bad47f298 100644 --- a/xonsh/aliases.py +++ b/xonsh/aliases.py @@ -25,9 +25,9 @@ def cd(args, stdin=None): return '', 'cd: no such file or directory: {0}\n'.format(d) if not os.path.isdir(d): return '', 'cd: {0} is not a directory\n'.format(d) - env.update({'OLDPWD': os.getcwd()}) + env['OLDPWD'] = os.getcwd() os.chdir(d) - env.update({'PWD': os.getcwd()}) + env['PWD'] = os.getcwd() return None, None def exit(args, stdin=None):