Convert Tabs to Spaces in Neovim
Practical steps to replace tabs with spaces in Neovim, including settings, commands, and how to check the result.
Before you start
Neovim performs this conversion locally in the open file. You do not need a plugin or an online service. Keep a copy of the file if you are testing commands for the first time, because changes from a find-and-replace command are applied to the buffer until you undo or discard them.
Think about whether the file type should contain tabs at all. In Makefiles, tabs can be required for recipe lines, so converting every tab can break the file. For most code and plain text files, replacing tab characters with a consistent number of spaces is safe.
Step-by-step instructions
- Open the file in Neovim by running nvim followed by the file name.
- Press Escape to make sure you are in normal mode.
- Choose how many spaces one tab should become. The common setting is 4, but you can use 2 or another value depending on the project.
- Set the visible width of a tab with the command :set tabstop=4. Replace 4 with your chosen width.
- Set the number of spaces used for an inserted tab with :set expandtab. This makes new Tab presses insert spaces.
- Set the shift width used by indentation commands with :set shiftwidth=4, using the same number you chose.
- If you also want Neovim to use these indentation values when it detects the file type, add an autocommand such as :autocmd FileType * setlocal tabstop=4 shiftwidth=4 expandtab.
- To convert existing tab characters in the whole buffer, use the command :%s/\t/ /g. In this command, \t matches a tab and the replacement is four spaces. Change the replacement to match your preferred width.
- To see the change without committing to it, run :%s/\t/ /gc and confirm each replacement when prompted.
If you only need to fix a section, select the lines in visual mode and run :s/\t/ /g. The command then applies to the selected range instead of the entire buffer.
Save the converted file with :write when you are satisfied. You can still undo until the buffer is closed, but after saving, an external file copy is your only rollback unless undo history remains available.
Check the output
Turn on list mode with :set list to make tab characters visible. Neovim displays tabs according to the listchars option. If listchars does not include a tab representation, add or adjust it with a command such as :set listchars=tab:>-,trail:- and then check the file again.
To confirm that no tab characters remain, search for a tab in normal mode with /\t and press Enter. If the search finds nothing, the conversion removed the tabs that were covered by the command.
Check that the resulting spaces match your intended width. A file converted with four spaces per tab should show four spaces where one tab used to appear. The visual width may differ if your tabstop setting does not match the replacement width, so keep the two values aligned.
Common problems and next steps
If new Tab presses still insert tab characters, expandtab is not active. Run :set expandtab? to see whether it is enabled, then use :set expandtab if necessary.
If indentation looks uneven after conversion, the original file probably mixed tabs and spaces. Check the affected area with list mode and realign the indentation using Neovim's normal-mode commands such as = for an operator-based reindent, but only where the file type supports it.
If you want these settings for every new file, place them in your Neovim configuration instead of entering them repeatedly. For most installations this is the init.vim or init.lua file. The exact path depends on your operating system and Neovim configuration directory; verify the path before editing the file.
If you converted tabs in a Makefile and the build stops working, restore the tabs in recipe lines. For that file type, set noexpandtab rather than treating all tabs as errors.