扩展html标签后的Vim光标位置 [英] Vim cursor position after expanding html tag
问题描述
在大多数IDE和现代文本编辑器(Sublime Text 3)中,在html标记之间插入换行符(又称扩展"标记)后,光标已正确缩进:
I most IDEs and modern text editors (Sublime Text 3) the cursor is correctly indented after inserting a newline in between an html tag (aka 'expanding" the tag):
之前:
<div>|</div>
按CR后:
<div>
|
</div>
但是在Vim中,这就是我得到的:
But in Vim, this is what I get:
<div>
|</div>
如何在Vim中获得与大多数其他编辑器相同的行为(见上文)?
How can I get the same behaviour in Vim like in most other editors (see above)?
推荐答案
在插入模式下<CR>
唯一的正确行为是在光标处换行.
The only correct behavior of <CR>
in insert mode is to break the line at the cursor.
您想要的是增强的行为,您需要在配置中添加一些东西才能获得它:映射,简短函数或完整的插件.
What you want is an enhanced behavior and you need to add something to your config to get it: a mapping, a short function or a full fledged plugin.
当我开始使用vim时,这种行为实际上是我添加到vimrc中的第一件事.我过去曾对其进行过多次更改,但是这种映射已经相当稳定了一段时间:
When I started to use vim, that behavior was actually one of the first things I added to my vimrc. I've changed it many times in the past but this mapping has been quite stable for a while:
inoremap <leader><CR> <CR><C-o>==<C-o>O
我已经使用<leader><CR>
来保持<CR>
的正常行为.
I've used <leader><CR>
to keep the normal behavior of <CR>
.
这是一个小功能,似乎可以满足您的要求:
Here is a small function that seems to do what you want:
function! Expander()
let line = getline(".")
let col = col(".")
let first = line[col-2]
let second = line[col-1]
let third = line[col]
if first ==# ">"
if second ==# "<" && third ==# "/"
return "\<CR>\<C-o>==\<C-o>O"
else
return "\<CR>"
endif
else
return "\<CR>"
endif
endfunction
inoremap <expr> <CR> Expander()
这篇关于扩展html标签后的Vim光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!