简体   繁体   中英

Transition between normal mode and insert mode ignores indentation in gVIM

Well, I have already started my journey into Vim, after learning the basis I was in the process of setting my _vimrc in order to achieve the behaviour that I want Vim to have while using it as my editor of preference. I was able to get a pretty solid configuration. However, there is something that I still do not know how to do, what I am trying to do is the following:

Consider that I wrote stuff like:

1

Then I press <ESC> in order to get into normal mode. What I get after that is:

2

Finally, I press I in order to get into insert mode and the position of the cursor is:

3

How can I force Vim to start insert mode with the indent relative to the block the cursor is at? In other words, achieve the starting position in the first screenshot when changing the mode instead of that in the third screenshot.

Almost all of my _vimrc file is made of:

syntax on
au GUIEnter * simalt ~x
colorscheme monokai-pro
set guifont=Consolas:h20
set mouse=a
set tabstop=4
set shiftwidth=4
set smartindent
set number
set autowrite
set autoread
set nowrap
set hls
set is
set expandtab
set cb=unnamed

Thanks in advance. I hope to have explained the issue correctly, if that is not the case please let me know.

First, the behaviour described here is not governed by any option. It is up to the user to handle the situation as they see fit. What happens is that Vim removes any automatically inserted indentation on lines that only consist of indentation in order, presumably, to prevent trailing spaces.

You wouldn't want trailing spaces, would you?

Second, why do you immediately go back to normal mode and enter insert mode again if you are already in insert mode and at the right column? Wouldn't that be simpler and more intuitive to… keep on typing?

Anyway, this is a common trap for beginners with three common approaches.

First approach

Insert something and immediately remove it before pressing <Esc> so that the automatic indentation is preserved:

1

Second approach

Let Vim do its thing and adjust indentation during insert mode:

2

See :help i_ctrl-t .

Third approach

Let Vim do its thing and press S instead of I to enter insert mode at the right indentation level:

3

See :help S .

Another way to look at it

Using Vim efficiently is pretty much all about mindfulness and using the best tool/technique for the current context. Something called "situational awareness" in some domains. Opening a line only to go somewhere else before coming back may or may not be the most appropriate action given the current context. Maybe that line should only be opened when you actually need to insert something, after you have been to somewhere else ? Maybe what you had to do somewhere else could be done differently and you actually don't have to go there to begin with?

4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM