Skip to content

Commit

Permalink
Bash-like "cd -"
Browse files Browse the repository at this point in the history
  • Loading branch information
tihirvon committed Apr 26, 2014
1 parent d76f485 commit a948b7f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Documentation/dex.txt
Expand Up @@ -112,7 +112,8 @@ case [-lu]
-u upper case

cd <directory>
Change directory. Updates $PWD and $OLDPWD.
Change directory. Updates $PWD and $OLDPWD. "cd -" changes to
previous directory ($OLDPWD).

center-view
Center view to cursor.
Expand Down
10 changes: 9 additions & 1 deletion commands.c
Expand Up @@ -87,12 +87,20 @@ static void cmd_case(const char *pf, char **args)

static void cmd_cd(const char *pf, char **args)
{
char *dir = args[0];
char cwd[8192];
char *cwdp = NULL;
bool got_cwd = !!getcwd(cwd, sizeof(cwd));
int i;

if (chdir(args[0])) {
if (streq(dir, "-")) {
dir = getenv("OLDPWD");
if (dir == NULL || dir[0] == 0) {
error_msg("cd: OLDPWD not set");
return;
}
}
if (chdir(dir)) {
error_msg("cd: %s", strerror(errno));
return;
}
Expand Down

0 comments on commit a948b7f

Please sign in to comment.