LOGO

Automatically Correct Spelling in 'cd' Command on Linux

August 19, 2016
Automatically Correct Spelling in 'cd' Command on Linux

Automatic Spell Correction for the 'cd' Command in Linux

Typically, word processing applications flag misspelled words. But what about typographical errors made while navigating directories using the Linux command line?

It’s possible to enable automatic correction of typos and misspellings, specifically when utilizing the cd command.

Enabling the 'cdspell' Option

We will demonstrate how to incorporate a straightforward command into the .bashrc file. This command will identify transposed characters, missing characters, and excessive characters within the cd command.

Related: How to Add Tabs to Ubuntu's Terminal

The .bashrc file is a script executed each time a Terminal window is opened (via Ctrl+Alt+T) or a new tab is created within a Terminal window.

Commands can be added to this file to run automatically upon Terminal initialization. To implement spell checking for the cd command, a command will be added to the .bashrc file.

Editing the .bashrc File

To modify the .bashrc file, we will use the gedit text editor. Execute the following command at the prompt:

gedit ~/.bashrc

Alternatively, you can employ any text editor you prefer, such as vi or nano. Simply substitute "gedit" in the command above with the appropriate command for your chosen editor.

how-to-automatically-correct-spelling-and-typos-when-using-cd-on-linux-1.jpgAdding the 'shopt -s cdspell' Command

Scroll to the bottom of the .bashrc file and append the following line:

shopt -s cdspell

A comment can be added above the command to clarify its purpose. Begin the line with a hash symbol (#) followed by your descriptive text.

Save the changes by clicking "Save".

how-to-automatically-correct-spelling-and-typos-when-using-cd-on-linux-2.jpgClose the text editor by clicking the "X" in the upper-left corner of the window.

how-to-automatically-correct-spelling-and-typos-when-using-cd-on-linux-3.jpgActivating the Changes

The modification to the .bashrc file will not immediately impact the current Terminal session.

You must close the Terminal window and log out and back in for the change to take effect. Type exit at the prompt and press Enter, or click the "X" button in the upper-left corner. Then, log out and log back in.

how-to-automatically-correct-spelling-and-typos-when-using-cd-on-linux-4.jpgTesting the Spell Correction

Now, if you introduce a minor error (like transposed characters) when typing a directory name with the cd command, the corrected spelling will be displayed on the subsequent line.

You will then be automatically directed to the directory that most closely corresponds to your input.

how-to-automatically-correct-spelling-and-typos-when-using-cd-on-linux-5.jpgThis straightforward solution significantly simplifies directory navigation on the command line.

#linux#cd command#spelling correction#typos#bash#zsh