Convert Tabs to Spaces in Emacs

Learn how to convert tabs to spaces in Emacs for individual regions, buffers, and files, with practical settings for code and text editing.

Before you start

This procedure runs locally in GNU Emacs and does not require a network service or external tool. The conversion affects the current buffer or an individually selected region, so you can limit changes to the text you are editing. Check the indentation style first: visible tab characters have a different width setting from indentation created with spaces, and simply changing the tab width does not replace the underlying characters.

Before making bulk changes, save the buffer or keep the original file available. The commands described here can be reversed immediately with Undo, but a saved backup makes it easier to compare the result. If the file is part of a project with an agreed indentation style, confirm that converting tabs is appropriate for the affected language or format.

Step-by-step instructions

To convert a selected region, first select the text you want to change. Then run the command untabify. With the standard Emacs key bindings, you can select text with Ctrl+Space followed by movement commands, and run M-x untabify. The command replaces tab characters in the region with the number of spaces needed to reach the next tab stop.

To convert the whole buffer, select the buffer content before running the same command. Use Ctrl+x h to mark the entire buffer, then run M-x untabify. When no region is active, untabify can still be used after selecting text explicitly, but relying on a visible region makes the scope clearer.

The spacing result depends on the tab-width setting. You can check it with M-x customize-option, then selecting tab-width. For example, a tab stop interval of 8 replaces a leading tab with up to 8 spaces, depending on the column where the tab starts. If your project uses 4-space indentation, set tab-width accordingly before converting.

The reverse operation converts leading spaces into tabs and is available as M-x tabify. Use it after checking the region or buffer scope, because it changes indentation characters even when the visual layout remains similar.

Check the output

Enable whitespace visibility to confirm that tabs have been replaced. One practical method is to run M-x whitespace-mode, which highlights tabs and spaces in the buffer. Another option is to use M-x occur for a search pattern, but whitespace mode gives a clearer visual check for indentation characters.

After conversion, compare the visual alignment with the original. When tab-width is correct, the layout should remain the same, but the file now contains spaces instead of tab characters. If the alignment changes, the tab-width used for conversion did not match the visual tab stops.

The conversion changes character content rather than file format or encoding. For source code, this does not affect compilation, but it can produce larger files with indentation made of spaces. For formats where tabs have special meaning, verify the format rules before committing the change.

Common problems and next steps

If the spacing looks wrong after untabify, check tab-width and repeat the conversion on an affected region. The conversion uses the tab width that is active when the command runs, so changing it afterward does not fix already converted lines.

When tabs are mixed with spaces in the same line or indentation, a whole-buffer operation can change more text than expected. To limit this, select only the affected lines and inspect them with whitespace-mode before and after the command.

For repeated work, you can set the indent-tabs-mode option to nil in a specific mode or buffer. This does not automatically convert existing tabs, but it tells Emacs to use spaces when you press Tab or edit indentation in the supported mode. Apply the setting before entering new text when you want future edits to stay space-based.

When you only need to change the displayed width of tab characters, use tab-width rather than conversion. Adjusting the display width is reversible and useful for viewing files from another system, while untabify permanently changes the stored characters in the buffer or saved file.