How to Automatically Correct Spelling and Typos When Using “cd” on Linux

00_lead_image_cdspell_command_in_action

We’re used to word processing programs telling us when we’ve misspelled a word, but what about when your fingers are flying through directories on the Linux command line? You can actually have your typos and misspellings automatically corrected, at least when using the cd command.

We’ll show you how to add a simple command to the .bashrc file that will check what you type into the cd command for transposed characters, missing characters, and too many characters.

RELATED: How to Add Tabs to Ubuntu’s Terminal

The .bashrc file is a script that runs every time you open a Terminal window by pressing Ctrl+Alt+T or open a new tab in a Terminal window. You can add commands to the .bashrc file that you want to run automatically every time you open a Terminal window. To add a spell check to the cd command, we’re going to add a command to the .bashrc file.

To edit the .bashrc file, we’re going to use gedit. Type the following command at the prompt.

gedit ~/.bashrc

You can use any text editor you’re comfortable with, such as vi and nano. Simply replace “gedit” in the above command with the command to run your chosen text editor.

01_opening_bashrc_file

Scroll to the bottom of the .bashrc file and add the following line to the end of the file.

shopt -s cdspell

You can add a comment above the command so you remember what the command does. Simply put a number sign (#) at the beginning of the line, and then any description you want to add.

Click “Save”.

03_adding_cdspell_to_bashrc

Close gedit (or other text editor) by clicking the “X” in the upper-left corner of the window.

04_closing_gedit

The setting you just added to the .bashrc file will not affect the current Terminal window session. You must close the Terminal window and log out and back in for the change to take affect. So, type exit at the prompt and press Enter or click the “X” button in the upper-left corner of the window. Then, log out and back in.

05_closing_terminal_window

Now, if you make a small mistake (such as transposed characters) when typing the name of a directory in the cd command, the correct spelling displays on the next line and you are automatically taken to the directory that most closely matches what you typed.

06_corrected_cd_command

This is a simple solution that makes navigating directories on the command line much easier.

Lori Kaufman is a writer who likes to write geeky how-to articles to help make people's lives easier through the use of technology. She loves watching and reading mysteries and is an avid Doctor Who fan.