Skip to content

Adjust tab indentation in Vim

Here’s how you can adjust Vim’s indentation to control how much it indents at a time:

Key Settings That Affect Indentation:

  1. shiftwidth (sw): Controls how many spaces or columns to use when indenting.
  2. tabstop (ts): Sets the number of spaces a Tab character represents.
  3. expandtab: If set, Vim uses spaces instead of tabs.

Check Current Indentation Settings:

To see your current indentation settings, run the following commands inside Vim:

:set shiftwidth?
:set tabstop?
:set expandtab?

This will display the current values for shiftwidth, tabstop, and whether expandtab is enabled.

Adjust Indentation to Your Preference:

Option 1: Set Smaller Indentation Width

If Vim indents too much, you can reduce the shiftwidth value, which controls how many spaces are added when you indent.

For example, if it indents by 8 spaces and you want to reduce it to 4 spaces, use:

code:set shiftwidth=4

You can also adjust tabstop similarly if it’s affecting indentation width:

code:set tabstop=4

Option 2: Use Spaces Instead of Tabs

If you want to use spaces instead of actual Tab characters, make sure expandtab is enabled. You can turn it on with:

code:set expandtab

This ensures that pressing Tab or indenting will insert spaces rather than tab characters.

Option 3: Set Tabs Back to Actual Tabs

If you prefer to use actual tabs (and not spaces), disable expandtab with:

code:set noexpandtab

Option 4: Auto-Adjust Indentation for the Current File Type

Sometimes, Vim indents differently based on the file type (e.g., for Python, HTML, etc.). You can let Vim automatically adjust indentation settings for the current file type with:

code:filetype indent on

Example: Set Indentation to 4 Spaces with Spaces Instead of Tabs

To set Vim to use 4 spaces for indentation and ensure that spaces are used instead of tabs, you can add these lines to your ~/.vimrc file:

codeset tabstop=4
set shiftwidth=4
set expandtab

This will ensure consistent indentation behavior across your files.


With these adjustments, you can prevent Vim from indenting too much at once and control how many spaces or tabs it uses.

Leave a Reply