Set Tab to 4 Spaces in Vim: Complete Guide for Developers

Vim is a powerful text editor, especially loved by developers working in C, Python, HTML, and many other languages. But one common formatting requirement is to use 4 spaces instead of a tab character, to keep code style consistent across environments.

This guide walks you through:

  • Temporarily changing tab to 4 spaces
  • Making it permanent with .vimrc
  • Auto-converting tabs to spaces
  • Filetype-specific settings

🛠 Step 1: Temporary Tab Settings in Vim

If you want to apply tab = 4 spaces for the current session only, you can run the following command inside Vim:

:set tabstop=4 shiftwidth=4 expandtab

What This Does:

  • tabstop=4: Treats a tab character as 4 spaces visually
  • shiftwidth=4: Controls how many spaces are used for auto-indentation (>>, <<, etc.)
  • expandtab: Converts the Tab keypress into space characters

These settings are perfect for aligning indentation and formatting your code without mixing tabs and spaces.


🧩 Step 2: Make Tab = 4 Spaces Permanent via .vimrc

To keep these settings persistent across Vim sessions, add them to your ~/.vimrc file:

set tabstop=4
set shiftwidth=4
set expandtab

Explanation:
Once these lines are saved to your .vimrc, Vim will always convert a tab into 4 spaces, and auto-indentation will follow this setting as well.

💡 You can edit .vimrc using vim ~/.vimrc and restart Vim to apply.


🔁 Step 3: Convert Existing Tabs to Spaces

If your file already contains tab characters and you want to convert them to 4 spaces, use the following command in Vim’s command mode:

:retab

Explanation:
The :retab command scans the file and replaces all tab characters (t) with the appropriate number of spaces based on the current tabstop value.

Make sure expandtab is enabled before running this command, or it won’t replace tabs with spaces.

:set expandtab
:retab

🧠 Step 4: Filetype-Specific Settings (Optional)

You can apply tab settings for specific file types using Vim’s autocmd feature. Add this to your .vimrc:

autocmd FileType python setlocal tabstop=4 shiftwidth=4 expandtab
autocmd FileType html setlocal tabstop=2 shiftwidth=2 expandtab

Explanation:
This ensures Python files use 4-space indentation, while HTML files use 2. This is a powerful way to follow language-specific coding standards automatically.


🧪 Quick Summary of Tab Settings

SettingPurpose
tabstopNumber of spaces a tab character represents
shiftwidthIndent/unindent step size
expandtabReplace Tab key with spaces
retabReplace existing tabs in file

Using consistent tab settings in Vim is essential for clean, maintainable code—especially when collaborating with teams or contributing to open-source. Whether you prefer tabs or spaces, understanding Vim’s indentation system ensures your code looks right everywhere.

By setting tabstop, shiftwidth, and expandtab, you tell Vim to treat every tab keypress as four actual spaces, and retab helps fix legacy files in seconds.

Have questions about Vim indentation or want to share your .vimrc tricks?
Drop a comment below and share this with a fellow coder who still uses hard tabs! 👇

Leave a Comment