某同事喜用ZSH,ZSH有一个auto_cd选项,打开它之后,如果命令行是一个目录,则进入该目录。例如
.. 进入上层目录,/etc 进入/etc目录。这样省得敲"cd"了。
结合ZSH的auto_pushd功能,可以很方便地在最近使用的目录中跳来跳去。
BASH有pushd/popd/dirs,但用起来总不如ZSH那么便捷。我给BASH加上了这个auto_cd功能,用 shopt -s auto_cd 开启之后,和ZSH效果一样。源代码改动如下:
--- bash-3.2.orig/execute_cmd.c2006-08-26 00:23:16.000000000 -0400
+++ bash-3.2/execute_cmd.c2008-05-12 16:21:44.000000000 -0400
@@ -3641,11 +3641,32 @@
command = search_for_command (pathname);
+ int no_symlinks = 0;
+ extern int auto_cd;
if (command)
{
+ if (auto_cd)
+ {
+ struct stat finfo;
+ if ((stat (command, &finfo) == 0) && (S_ISDIR (finfo.st_mode)))
+ {
+ if (change_to_directory (pathname, no_symlinks))
+ {
+ bindpwd (no_symlinks);
+ return;
+ }
+ }
+ }
+
maybe_make_export_env ();
put_command_name_into_env (command);
}
+ else if (auto_cd && change_to_directory (pathname, no_symlinks))
+ {
+ bindpwd (no_symlinks);
+ return;
+ }
+
/* We have to make the child before we check for the non-existence
of COMMAND, since we want the error messages to be redirected. */
--- bash-3.2.orig/builtins/cd.def2006-07-27 21:35:36.000000000 -0400
+++ bash-3.2/builtins/cd.def2008-05-12 16:14:02.000000000 -0400
@@ -58,16 +58,18 @@
extern int array_needs_making;
extern char *bash_getcwd_errstr;
-static int bindpwd __P((int));
+int bindpwd __P((int));
static void setpwd __P((char *));
static char *resetpwd __P((char *));
-static int change_to_directory __P((char *, int));
+int change_to_directory __P((char *, int));
static char *cdspell __P((char *));
/* Change this to 1 to get cd spelling correction by default. */
int cdspelling = 0;
+int auto_cd = 0;
+
int cdable_vars;
$BUILTIN cd
@@ -103,7 +105,7 @@
}
}
-static int
+int
bindpwd (no_symlinks)
int no_symlinks;
{
@@ -395,7 +397,7 @@
getcwd() will eventually be called), or set to a string corresponding
to the working directory. Return 1 on success, 0 on failure. */
-static int
+int
change_to_directory (newdir, nolinks)
char *newdir;
int nolinks;
--- bash-3.2.orig/builtins/shopt.def2005-02-19 17:25:02.000000000 -0500
+++ bash-3.2/builtins/shopt.def2008-05-12 16:13:32.000000000 -0400
@@ -68,6 +68,7 @@
extern int hup_on_exit;
extern int xpg_echo;
extern int gnu_error_format;
+extern int auto_cd;
#if defined (EXTENDED_GLOB)
extern int extended_glob;
@@ -114,6 +115,7 @@
int *value;
shopt_set_func_t *set_func;
} shopt_vars[] = {
+ { "auto_cd", &auto_cd, (shopt_set_func_t *)NULL },
{ "cdable_vars", &cdable_vars, (shopt_set_func_t *)NULL },
{ "cdspell", &cdspelling, (shopt_set_func_t *)NULL },
{ "checkhash", &check_hashed_filenames, (shopt_set_func_t *)NULL },
@@ -253,6 +255,7 @@
cdable_vars = mail_warning = 0;
no_exit_on_failed_exec = print_shift_error = 0;
check_hashed_filenames = cdspelling = expand_aliases = check_window_size = 0;
+ auto_cd = 0;
source_uses_path = promptvars = 1;