为 bash 添加 auto_cd 功能:如果命令行是一个目录,则进入该目录
2008-05-13 06:34
323 查看
某同事喜用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.c 2006-08-26 00:23:16.000000000 -0400
+++ bash-3.2/execute_cmd.c 2008-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.def 2006-07-27 21:35:36.000000000 -0400
+++ bash-3.2/builtins/cd.def 2008-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.def 2005-02-19 17:25:02.000000000 -0500
+++ bash-3.2/builtins/shopt.def 2008-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;
.. 进入上层目录,/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.c 2006-08-26 00:23:16.000000000 -0400
+++ bash-3.2/execute_cmd.c 2008-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.def 2006-07-27 21:35:36.000000000 -0400
+++ bash-3.2/builtins/cd.def 2008-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.def 2005-02-19 17:25:02.000000000 -0500
+++ bash-3.2/builtins/shopt.def 2008-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;
相关文章推荐
- Bash在cd进入目录时自动启动脚本
- 如何在命令行中进入一个目录?
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- Linux中cd进入一个目录的权限
- 判断本地系统目录下是否存在XML文件,如果不存在就创建一个XMl文件,若存在就在里面执行添加数据
- [译]教程:Horizon在目录添加一个复杂的功能
- Bash在cd进入目录时自动启动脚本
- 在LINUX中,有一个目录dir 如果想进入目录 必须拥有什么权限?
- fastdfs 新添加一个项目的图片目录 实现独立的上传下载功能
- 博客笑将要添加的一个功能。
- iis中添加虚拟目录,作用在于可以把文件并不放在一个文件夹内,最大的好处是可以用别的服务器上的文件
- Linux 中直接使用cd进入which返回值所在的目录的方法
- mkdir -p 参数,可以直接创建层级的目录,不需要创建一个进入一个
- bash编程 将一个目录里所有文件存为一个array 并分割为三等分——利用bash array切片
- 用rar命令行 添加指定文件到压缩包的指定目录中
- J2ME中,如果要在最后位置添加一个字符,实现的方法
- 编写一个简易的留言薄,实现添加留言和显示留言内容等功能
- MFC添加一个按钮,实现浏览功能
- 进入目录需要哪些权限, 在目录中执行增删查(cd, touch, ls, rm, mv等)改文件动作, 需要哪些权限
- 为FckEditor添加自定义的用户上传目录功能!