1

I've been using Vim for a while heard people saying indent with spaces is a better practice when programming in Python so I configured my .vimrc as below:

au BufNewFile,BufRead *.py,*.java,*.cpp,*.c,*.h,*.md,*.html,*.css,*.js
    \ set expandtab |
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4 |
    \ set textwidth=120 |
    \ set autoindent |
    \ set fileformat=unix |

Everything works well so far. But I noticed unlike other editors for example vscode or sublime. Vim does not keep the indent in a line gap between code.

The cursor will indent itself when I hit enter but if I leave that line blank when I return to that line the cursor will appear at the beginning of the line.

Here is an example (@ as cursor):

Class RandomClass:
    code... # 1) hit <enter> and change line
    @ # 2) cursor will appear in here. auto indented. good 3) now hit <enter> again and leave this as a line gap
    code... # 4) code something and 5) return back to the line gap

    # 6) now the cursor '@' would appear at the beginning of the line without indentation

Is this what it suppose to be?

4
  • 1
    not a vim expert, but why do you have autoindent commented out? Seems like that could be the reason Commented Jan 5, 2021 at 19:27
  • I believe that's just a stack overflow formatting thing. I've edited it so it does not look at being commented out. thanks. Commented Jan 5, 2021 at 19:41
  • 2
    Not sure if this works for Python, but for most languages you can press cc on an empty line to get into insert mode with the cursor already indented. Commented Jan 5, 2021 at 21:19
  • 1
    See vi.stackexchange.com/questions/5760/… for starting insert at the right indentation on a line where Vim stripped it. Commented Jan 6, 2021 at 15:27

1 Answer 1

3

Is this what it suppose to be?

Yes, if there is no code on the line, then there is no need to indent it.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I also followed the tips in this post and there is a work around config I can add to .vimrc which I found in here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.