To configure LazyVim to use a 4-space indentation instead of the default 2-space indentation, you need to adjust both Neovim's settings and any formatter configurations that may override these settings. Here's how you can do it:
Steps to Change Indentation to 4 Spaces
1. Update Neovim's Indentation Settings
Modify the options.lua file in your LazyVim configuration directory to set the desired indentation. Add or update the following lines:
lua
local opt = vim.opt
opt.expandtab = true -- Use spaces instead of tabs
opt.shiftwidth = 4 -- Number of spaces for each indentation level
opt.tabstop = 4 -- Number of spaces a tab character represents
opt.softtabstop = 4 -- Number of spaces for a tab when editing
These settings ensure that:
Tabs are replaced with spaces (expandtab).
The width of an indentation level is 4 spaces (shiftwidth).
A tab character is treated as 4 spaces (tabstop).
Pressing the Tab key inserts 4 spaces (softtabstop).
2. Adjust Formatter Configuration
LazyVim often uses external formatters like stylua for Lua files or other formatters for different languages. These formatters may override your Neovim settings. To ensure consistency:
Locate the formatter configuration file, such as stylua.toml, in your project or LazyVim setup.
Update the indentation settings in the file. For example, in stylua.toml:
toml
indent_type = "Spaces"
indent_width = 4
If you are using other formatters (e.g., Prettier, clang-format), refer to their respective configuration files and set the indentation width to 4 spaces.
3. Disable Autoformat on Save (Optional)
If you notice that saving a file reverts the indentation to 2 spaces, it might be due to autoformatting. You can disable autoformatting on save by adding this line to your options.lua:
lua
vim.g.autoformat = false
Alternatively, configure the formatter to respect your indentation settings.
4. Reindent Existing Files
To reindent an entire file to 4 spaces, use the following command in Neovim:
vim
:set shiftwidth=4 | :set tabstop=4 | :retab
This command adjusts the file's indentation to match the new settings.
5. Verify Treesitter or Plugin Configurations
If you use Treesitter or plugins like mini.indentscope or indent-blankline.nvim, ensure their configurations align with the 4-space indentation. Check their respective settings in your LazyVim configuration.
By following these steps, you can ensure that LazyVim consistently uses 4-space indentation across all files and formats.