7

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

 3 years ago
source link: https://www.lujun9972.win/blog/2018/02/09/cdspell%E5%B8%AE%E4%BD%A0%E8%87%AA%E5%8A%A8%E6%9B%B4%E6%AD%A3cd%E5%91%BD%E4%BB%A4%E4%B8%AD%E7%9A%84%E9%94%99%E8%AF%AF%E7%9B%AE%E5%BD%95/index.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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

使用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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK