cdspell帮你自动更正cd命令中的错误目录

本文介绍如何在Bash中启用cdspell选项,该功能能够在输入cd命令时自动修正目录名称的小错误,如字符错位、缺失或重复。通过简单的设置,提升目录切换的效率与准确性。

原文地址:https://raw.githubusercontent.com/lujun9972/lujun9972.github.com/source/linux和它的小伙伴/cdspell帮你自动更正cd命令中的错误目录.org

使用cd命令跳转目录时难免会有输错目录名称的时候,

[lujun9972@F31 ~]$ cd ~/Download
bash: cd: /home/lujun9972/Download: 没有那个文件或目录

可以通过设置 cdspell 来让bash帮你自动更正输入错误的目录。像这样

shopt -s cdspell

之后当输入的目录中出现字符错位、缺少一个字符或者重复输入同一字符时,bash会尝试查找正确的目录,若找到了则输出正确的目录并跳转其中。

像这样,

[lujun9972@F31 ~]$ shopt -s cdspell
[lujun9972@F31 ~]$ cd ~/Download
/home/lujun9972/Downloads
[lujun9972@F31 Downloads]$ 

但是需要注意的是, cdspell 无法纠正编辑距离超过1的错误,比如当你缺少两个字符时,依然会报错

[lujun9972@F31 ~]$ cd ~/Downloa
bash: cd: /home/lujun9972/Downloa: 没有那个文件或目录

关于 cdspell 的说明可以参见 man bashshopt 的描述

cdspell If set, minor errors in the spelling of a directory com-
        ponent in a cd command will be  corrected.   The  errors
        checked for are transposed characters, a missing charac-
        ter, and one character too many.   If  a  correction  is
        found,  the  corrected filename is printed, and the com-
        mand proceeds.  This option is only used by  interactive
        shells.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值