Last Updated: October 15, 2022
·
27.1K
· rawswift

Vim auto-indent (4 spaces)

I find this to be very convenient especially if you're doing a quick code editing or even on a long session of coding, using Vim. And you want to enforce a particular indentation style, in this case, a 4 space indentation.

In your ~/.vimrc file, add this options:

syntax enable
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab

Your tabs will now be 4 spaces, new lines will be auto-indented and curly braces will be aligned automatically.

Source: Stack Overflow

Related protips:

Basic Vim commands - For getting started

1 Response
Add your response

I encourage anyone modifying their vimrc to understand why they are setting something. As for me I haven't yet determine what smartindent does and I've grabbed these settings from various other people's vimrcs.

set autoindent " always set autoindenting on

set copyindent " copy the previous indentation on autoindenting

set expandtab " expand tabs by default (overloadable per file type)

set shiftround " use multiple of shiftwidth when indenting with '<' and '>'

set shiftwidth=2 " number of spaces to use for autoindenting

set smartindent

set smarttab " insert tabs on the start of a line according to shiftwidth, not tabstop

set softtabstop=2 " when hitting <BS>, pretend like a tab is removed, even if spaces

set tabstop=2 " tabs are n spaces

over 1 year ago ·