简体   繁体   English

正常模式和插入模式之间的转换忽略 gVIM 中的缩进

[英]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.好吧,我已经开始了进入 Vim 的旅程,在学习了基础之后,我正在设置我的_vimrc以实现我希望 Vim 在使用它作为我的首选编辑器时具有的行为。 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.然后我按<ESC>进入正常模式。 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:最后,我按I进入插入模式,cursor 的 position 是:

3

How can I force Vim to start insert mode with the indent relative to the block the cursor is at?如何强制 Vim 以相对于 cursor 所在的块的缩进开始插入模式? In other words, achieve the starting position in the first screenshot when changing the mode instead of that in the third screenshot.也就是说,在第一个屏幕截图中改变模式而不是在第三个屏幕截图中实现启动position。

Almost all of my _vimrc file is made of:几乎我所有的_vimrc文件都是由:

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.发生的情况是 Vim 删除了仅由缩进组成的行上的任何自动插入的缩进,大概是为了防止尾随空格。

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?其次,如果您已经处于插入模式并且在右列,为什么要立即 go 回到正常模式并再次进入插入模式? 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:在按下<Esc>之前插入一些东西并立即将其移除,以便保留自动缩进:

1

Second approach第二种方法

Let Vim do its thing and adjust indentation during insert mode:让 Vim 做它的事情并在插入模式下调整缩进:

2

See :help i_ctrl-t .请参阅: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:让 Vim 做它的事情并按S而不是I在右侧缩进级别进入插入模式:

3

See :help S .请参阅: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.有效地使用 Vim 几乎都是关于正念和使用当前环境的最佳工具/技术。 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.考虑到当前情况,在返回之前仅在其他地方打开一条线到 go 可能是也可能不是最合适的操作。 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?也许您必须在其他地方做的事情可能会有所不同,而您实际上不必从 go 开始?

4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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