0

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 shiftwidth=4
set noexpandtab
set nu
set autoindent
set cindent
set encoding=utf-8
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8

I am debugging others' code and when I inserted a new line, it seems automatically indented with the next line. However, when I execute the python program, it complaints. For example, I added a print statement to the following:

for i, third in enumerate(third_list):
    j = i % 4
    third_group4[j] = third
    print('i, j', i, j)
    if j == 3:
     ....

The print seems aligns well with other lines, but it complains:

python test.py 
  File "test.py", line 156
    if j == 3:
    ^
IndentationError: unexpected indent

The next line got the complaints about the indent. I manually checked and found that instead of the original 8 leading spaces in front of 'if', the first 4 spaces are a tab, which is also 4 spaces long. The 1-tab+4 spaces are the same length as the 8 spaces, but that's why it complains.

I tested a few other print statements and it seems it always affected the following line when I inserted a print statemnt.

How can I void this? I don't want to manually fixe the indent whenever I made a little change to the code. I am on Centos 7.

marlon
  • 6,029
  • 8
  • 42
  • 76
  • You might find the following discussion useful to evolve your approach to this problem: https://stackoverflow.com/questions/39172306/can-a-line-of-python-code-know-its-indentation-nesting-level . More importantly, with Python's "variable" indentation policy, it may be best to stick with actual space characters, rather than tabs, to avoid violating the indentation consistency rule by having mixed "4-space" and "4-space tabs" intermingled, because the 4-space tab is NOT 4 characters, but is ONLY 1. Also look at a "linter" for python: https://realpython.com/python-code-quality/ . – Eric Marceau Aug 24 '21 at 00:20
  • @Eric In order to avoid tabs, should I disable 'set tabstop=4 set softtabstop=4 set shiftwidth=4 set noexpandtab' in the vimrc file? – marlon Aug 24 '21 at 00:24
  • You actually want to "set expandtab", not the opposite. The full discussion of this is in a previous answer here: https://stackoverflow.com/questions/1878974/redefine-tab-as-4-spaces . The full vimrc definition line (from that reference) is "set tabstop=8 softtabstop=0 expandtab shiftwidth=4 smarttab". – Eric Marceau Aug 24 '21 at 00:40

1 Answers1

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.

romainl
  • 186,200
  • 21
  • 280
  • 313