Convert Tabs to Spaces in Nano
Learn how to replace tabs with spaces in the Nano text editor, verify the result, and avoid common indentation mistakes.
If a script, config file, or code sample contains tabs and your project expects spaces, Nano can convert them without switching to another editor. The process runs locally on the machine where you open the file; it does not require an online service or account.
Before you start
Open a terminal and check that Nano is installed by running nano --version. The available options depend on the installed version, so this is worth confirming before editing. If the command reports that Nano is not found, install it through your system package manager using the package name for your distribution.
Make a backup copy of the file before changing whitespace. This is especially useful for source files or configuration files where formatting might be significant.
Step-by-step instructions
One practical way to convert tabs is to bind the tab key to insert spaces. Start Nano with the tab size set to the number of spaces you want, and use a temporary key binding that prevents literal tabs from being entered when formatting is rewritten.
For example, if your file uses indentation based on four spaces, open it with the tab size set to four and bind the tab key to the indent function:
- nano -T 4 -Q "'\t' indent" filename
- Run the command from the directory containing the file, replacing filename with the actual file name.
- The -T option sets the displayed tab width, while -Q applies the quoted key binding for the current session.
- Press Ctrl+6 to set the mark at the beginning of the file, move to the end, then use the indent command to reapply indentation with spaces.