博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
为 bash 添加 auto_cd 功能:如果命令行是一个目录,则进入该目录
阅读量:4121 次
发布时间:2019-05-25

本文共 2787 字,大约阅读时间需要 9 分钟。

某同事喜用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;

转载地址:http://oqvpi.baihongyu.com/

你可能感兴趣的文章
12 个JavaScript 特性技巧你可能从未使用过
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(上)
查看>>
【视频教程】Javascript ES6 教程27—ES6 构建一个Promise
查看>>
【5分钟代码练习】01—导航栏鼠标悬停效果的实现
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(中)
查看>>
8种ES6中扩展运算符的用法
查看>>
【视频教程】Javascript ES6 教程28—ES6 Promise 实例应用
查看>>
127个超级实用的JavaScript 代码片段,你千万要收藏好(下)
查看>>
【web素材】03-24款后台管理系统网站模板
查看>>
Flex 布局教程:语法篇
查看>>
年薪50万+的90后程序员都经历了什么?
查看>>
2019年哪些外快收入可达到2万以上?
查看>>
【JavaScript 教程】标准库—Date 对象
查看>>
前阿里手淘前端负责人@winter:前端人如何保持竞争力?
查看>>
【JavaScript 教程】面向对象编程——实例对象与 new 命令
查看>>
我在网易做了6年前端,想给求职者4条建议
查看>>
SQL1015N The database is in an inconsistent state. SQLSTATE=55025
查看>>
RQP-DEF-0177
查看>>
Linux查看mac地址
查看>>
Linux修改ip
查看>>