#+TITLE: cdspell帮你自动更正cd命令中的错误目录 #+AUTHOR: lujun9972 #+TAGS: linux和它的小伙伴 #+DATE: [2018-02-09 五 15:22] #+LANGUAGE: zh-CN #+OPTIONS: H:6 num:nil toc:t \n:nil ::t |:t ^:nil -:nil f:t *:t <:nil #+ID: 79575934 使用cd命令跳转目录时难免会有输错目录名称的时候, #+BEGIN_EXAMPLE [lujun9972@F31 ~]$ cd ~/Download bash: cd: /home/lujun9972/Download: 没有那个文件或目录 #+END_EXAMPLE 可以通过设置 =cdspell= 来让bash帮你自动更正输入错误的目录。像这样 #+BEGIN_SRC shell shopt -s cdspell #+END_SRC 之后当输入的目录中出现字符错位、缺少一个字符或者重复输入同一字符时,bash会尝试查找正确的目录,若找到了则输出正确的目录并跳转其中。 像这样, #+BEGIN_EXAMPLE [lujun9972@F31 ~]$ shopt -s cdspell [lujun9972@F31 ~]$ cd ~/Download /home/lujun9972/Downloads [lujun9972@F31 Downloads]$ #+END_EXAMPLE 但是需要注意的是, =cdspell= 无法纠正编辑距离超过1的错误,比如当你缺少两个字符时,依然会报错 #+BEGIN_EXAMPLE [lujun9972@F31 ~]$ cd ~/Downloa bash: cd: /home/lujun9972/Downloa: 没有那个文件或目录 #+END_EXAMPLE 关于 =cdspell= 的说明可以参见 =man bash= 中 =shopt= 的描述 #+BEGIN_EXAMPLE 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. #+END_EXAMPLE