Set Vim to Indent With Spaces
Learn how to configure Vim to indent with spaces, replace existing tab characters, and control tab width without assuming every tab equals a fixed number of spaces.
What to Set
To make Vim indent with spaces instead of tab characters, use these settings:
set expandtab
set shiftwidth=4
set softtabstop=4
These settings apply to the current editing session. They affect how Vim creates new indentation and how it handles the Tab key in insert mode. They do not automatically change tab characters that already exist in the file.
How Each Setting Works
- expandtab: Makes Vim insert spaces when the Tab key is used or when automatic indentation is added. The actual character inserted is a space, not a tab.
- shiftwidth: Controls the number of spaces used for each indentation level when Vim applies automatic indentation with commands such as the reindent command or the indent operators.
- softtabstop: Controls how many columns the Tab key and Backspace key use while editing. With expandtab enabled, pressing Tab inserts this many spaces; pressing Backspace can remove the spaces as if they formed a tab-sized unit.
Making the Settings Permanent
To keep these options across Vim sessions, place the same lines in your Vim configuration file. The file is typically named vimrc. Adding them there makes Vim start with the chosen behavior.
set expandtab
set shiftwidth=4
set softtabstop=4
Converting Existing Tabs to Spaces
The expandtab setting only affects new input. If the file already contains tab characters, they remain in place. To convert existing tabs in the whole buffer, use the retab command after confirming that the tabstop value matches your intended displayed tab width:
:set tabstop=4
:retab
The retab command replaces runs of spaces that match a tab stop with tab characters, or replaces tab characters with spaces, depending on the expandtab setting. With expandtab enabled, it replaces tabs with spaces. With expandtab disabled, it can replace suitable runs of spaces with tabs.
Why Tab Character Width Is Not a Fixed Number of Spaces
A tab character does not encode a fixed number of spaces. It advances the cursor to the next tab stop, and the number of columns it occupies depends on the current column position and the tabstop setting. Because display width varies between editors and settings, a file that relies on tab characters can appear differently in different tools.
When Whitespace Carries Meaning
Whitespace conversion is not universally safe. In languages where indentation controls structure, changing tabs to spaces can change how the source is interpreted if the language is sensitive to leading whitespace. Even where whitespace is not syntactically significant, changing tab stops can alter human-readable column alignment, especially when tabs are used inside table-like layouts or aligned comments.
Summary
Set expandtab to make Vim insert spaces for new indentation, and use shiftwidth and softtabstop to define indentation width and editing behavior. Use retab only when you deliberately want to convert existing tab characters, and keep tabstop consistent with the file's existing display assumptions.