Skip to content Skip to sidebar Skip to footer

How To Set Vimrc Configuration So That It Automatically Indent With 4 Spaces Instead Of A Tab?

This is my current .vimrc: cat ~/.vimrc set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936 set termencoding=utf-8 set encoding=utf-8 set tabstop=4 set softtabstop=4 set shif

Solution 1:

First, let's remove the redundant lines from your vimrc:

set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
set tabstop=4
set softtabstop=4
set shiftwidth=4
set noexpandtab
set nu
set autoindent
set cindent

Second, let's fix the encoding part:

set encoding=utf-8
set fileencodings+=gb18030,gbk,gb2312,cp936
  • :help 'termencoding' gets its value from :help 'encoding' so you only "need" set encoding=utf-8 if you want both options to have the same value.

    Note that Vim gets its default value for 'encoding' from your environment so it might be a good idea to set it up properly instead of hacking individual programs to work the way you want.

  • The default value of :help 'fileencodingd' if 'encoding' is utf-8 is fine and covers a lot of ground already. Adding your local encodings makes more sense than redefining the whole thing.

and normalise option names:

set number

Now we can turn our attention to the rest…

  • set cindent is specifically made for C so it is useless if you are working on Python code and thus can safely be removed.

  • set noexpandtab explicitly tells Vim to use hard tabs. There are two problems with this line:

    1. :help 'expandtab' is disabled by default so there is no point doing it manually.

    2. You actually want spaces, not tabs, so the right value would be set expandtab.

Here is how your vimrc should look at this stage:

set encoding=utf-8
set fileencodings+=gb18030,gbk,gb2312,cp936
set tabstop=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
set number

which doesn't seem to mess with the formatting of your sample code.

Note that Vim comes with a built-in filetype detection mechanism that, among other things, sets the standard indentation rules for Python. Just add the following line to your vimrc to benefit from it:

filetype plugin indent on

See :help :filetype.


Post a Comment for "How To Set Vimrc Configuration So That It Automatically Indent With 4 Spaces Instead Of A Tab?"