`
ihuashao
  • 浏览: 4663977 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

为 bash 添加 auto_cd 功能:如果命令行是一个目录,则进入该目录

阅读更多

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

分享到:
评论

相关推荐

    auto_mysql.zip

    标题“auto_mysql.zip”指的是一个包含了自动化MySQL 8.0安装流程的压缩文件。这个压缩包的目的是简化在Linux系统上安装MySQL数据库的过程,通过一个shell脚本来完成,节省了手动配置的时间和精力。让我们详细了解...

    Auto_GH_Project_Public:已创建

    标题 "Auto_GH_Project_Public:已创建" 暗示我们正在讨论一个自动化GitHub项目,可能是一个自动化的脚本或工具,用于管理GitHub仓库。标签 "Shell" 提示这个项目可能使用了Bash shell脚本语言,这是一种在Unix/...

    auto_deploy:一些常用软件安装的脚本日常使用巡检脚本

    这个项目特别强调了Shell脚本的应用,这是一种在Linux和Unix系统中广泛使用的命令行解释器,可以编写执行一系列命令的程序。 1. **Shell脚本基础**:Shell脚本是基于Bash或其他Shell(如Zsh、Csh)的编程语言,它...

    在 Linux 中不使用 CD 命令进入目录/文件夹的方法

    `autocd` 选项就是其中之一,它使得当用户在命令行输入一个存在的目录路径并回车时,shell 会自动执行 `cd` 命令进入该目录。要启用 `autocd`,首先需要编辑用户的 `.bashrc` 文件。`.bashrc` 是一个启动时执行的 ...

    ubuntu安装auto教程

    Ubuntu是一个基于Debian的开源操作系统,深受开发者和计算机爱好者的喜爱。在Ubuntu上安装Auto软件是相对简单的,这个教程将引导你完成整个过程,确保你能顺利地在Ubuntu环境下配置和使用Auto。 **步骤1:更新系统*...

    打造C程序员专用vim

    2. **安装cscope_maps.vim:** 由于cscope_maps.vim并不是一个独立的插件,而是包含了cscope功能增强脚本的文件,可以直接将其放置在`~/.vim/plugin/`目录下。 3. **生成cscope数据库:** 首先,需要确定cscope数据库...

    bash shell

    5. **管道**:Bash允许用户使用管道(|)连接多个命令,将一个命令的输出作为另一个命令的输入,实现数据流的传递。 6. **重定向**:Bash提供重定向操作,允许用户将命令的输出重定向到文件,或将文件内容作为命令的...

    linux命令大全 例子演示

    ### Linux命令大全与实例解析 Linux作为一款强大的开源操作系统,为用户提供了丰富的命令行工具来完成各种任务。本文将详细解读一系列重要的... - 用途:如果前一个命令成功,则执行后面的命令。 - 示例: ```bash ...

    redis6.0.8安装文件.zip

    这包括"redis-6.0.8.tar.gz",这是一个归档文件,包含了Redis源代码的所有文件。我们首先需要解压这个文件,使用Linux的`tar`命令: ```bash tar -zxvf redis-6.0.8.tar.gz ``` 解压完成后,进入源代码目录: ```...

    windows环境下使用命令的方式安装mysql5.7

    此外,还需要创建一个名为`my.ini`的配置文件,该文件用于设置MySQL的一些基础配置参数,如端口号、字符集等。配置文件的内容如下: ```ini [mysql] # 设置mysql客户端默认字符集 default-character-set=utf8 ...

    GNU AutoMake

    例如,如果有一个名为 `FOO` 的变量,那么相关的变量可能会被命名为 `FOO_FLAGS` 或 `FOO_DIR`。 ##### 3.6 保留给用户的变量 为了不干扰用户的环境变量,Automake 预留了一些特殊变量名,例如以 `_` 开头的变量名...

    a notebook about i how to learn ubuntu

    - **&& 运算符**:如果第一个命令执行成功,则执行第二个命令;否则,不会执行第二个命令。 - **|| 运算符**:如果第一个命令执行失败,则执行第二个命令;否则,不会执行第二个命令。 - **; 运算符**:无论前一...

    learn-bash-shell-script:学习BASH Shell脚本

    Bash shell提供了丰富的内置命令,如`cd`用于切换目录,`ls`用于列出目录内容,以及`echo`用于打印文本。此外,它支持流程控制结构,如条件语句(`if`、`else`、`fi`)、循环(`for`、`while`、`until`)和函数定义...

    CTG-BBS集约化 负载均衡环境安装文档v0.1 共22页.docx

    通过本指南,读者可以了解到搭建一个稳定可靠的负载均衡环境所需的具体步骤和技术要点。 #### 二、JDK 安装与环境配置 1. **下载 JDK** - 下载地址:`http://www.wangshangyou.com/linux/119.html` - 文件名:`...

    Linux经典实用的使用技巧33则

    在Bash shell中,使用`ESC + B`可以将光标向左移动一个字符,而`Ctrl + K`则用来删除光标至行尾的所有字符,这些快捷键提高了命令行的编辑效率。 #### 十七、进程筛选 使用`ps`命令结合`grep`可以筛选出符合条件的...

    windows下android源码批量下载.pdf

    - 对于单个包的源码下载,你需要在Git Bash中创建一个新的目录,然后切换到该目录,例如: ```bash mkdir android-source cd android-source ``` - 接着,使用`git clone`命令克隆特定的仓库,如`common.git`...

    mysql5.5.9 安装文档

    在开始安装 MySQL 5.5.9 之前,首先需要准备一个 SUSE Linux 的运行环境,并创建专门用于 MySQL 运行的用户组及用户。 **步骤一:** 创建 MySQL 用户组: ```bash # linux-749p:~# groupadd mysql ``` **步骤二:*...

    windows下Android源码下载(2011.4).pdf

    文中提供了一个名为`autoget.sh`的示例脚本,该脚本会自动克隆一系列的Android源码仓库。你需要将这个脚本内容保存到你的源码下载目录,然后在Git Bash中执行它。例如,如果你将脚本保存在`C:\AndroidSource`,可以...

    windows下android源码下载方法

    首先,创建一个目录作为存放源码的地方,然后在Git Bash(Git提供的命令行环境)中切换到该目录下。例如: ```bash mkdir android_source cd android_source ``` 现在,你可以逐个克隆所需的Git仓库。例如,如果你...

Global site tag (gtag.js) - Google Analytics