Celeb Glow
general | March 12, 2026

How to set tab to 4 spaces in vim?

By default, the tab in vim is set to 8 spaces. How can we set the tab to 4 spaces in vim permanently?

3 Answers

Create a ~/.vimrc file with this line:

set ts=4 sw=4

This will set tabstops every 4 spaces and set the shiftwidth (that amount you indent) as 4 spaces also to match your tabstops.

0

Make ~/.vimrc and add this:

set tabstop=4

Here are some other helpful ones:

syntax on " Syntax highlighting
set showmatch " Shows matching brackets
set ruler " Always shows location in file (line#)
set smarttab " Autotabs for certain code
set shiftwidth=4
0

If you are just using vi (some people do not have vim available) put this in your .exrc file in your home directory or where you are editing code if you have/want a specific startup for a particular directory of code:

set shiftwidth=4 set tabstop=4 set expandtab "this might not work on all hosts" "quotes make nice comments in your .exrc files"

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy