在大括号内时 vim 中自动换行和缩进?
例如,如果我在 vim 中编写 jQuery,并且我在插入模式下有这个(“|”是当前光标位置):
$('#something').click(function(){|})
...然后我按回车键,我想得到这个:
$('#something').click(function(){
|
})
...而当前,我得到这个:
$('#something').click(function(){
|})
PHP 中的函数定义也会发生同样的事情。有没有办法在大括号内按回车键时自动插入额外的新行并缩进光标位置?
(如果之前有人问过这个问题,我深表歉意 - 这似乎是一个常见的请求,但我搜索了一段时间但找不到它。)
For example, if I'm coding jQuery in vim, and I have this in insert mode ("|" is the current cursor position):
$('#something').click(function(){|})
...and I hit enter, I would like to get this:
$('#something').click(function(){
|
})
...while currently, I'm getting this:
$('#something').click(function(){
|})
The same thing happens with function definitions in PHP. Is there a way to automatically insert the extra new line and indent the cursor position when hitting enter inside braces like that?
(Apologies if this has been asked before - it seems like a common request but I searched for awhile and couldn't find it.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我最终只是将其硬编码到 .vimrc 中,如下所示
部分似乎不是必需的,但由于某种原因,它没有像O 命令通常都是这样,所以我必须添加它。I ended up just hardcoding this into .vimrc, as follows
It would seem like the
<BS><Tab>
parts aren't necessary, but for some reason it wasn't indenting correctly like the O command usually does, so I had to add it.只需安装 auto-pairs 插件 就可以了。
Just install auto-pairs plugin and be happy.
我使用 javascript.vim 它(几乎)可以满足您对 JavaScript 的需求代码。
还有一个php.vim,但我没有测试它。
I use javascript.vim which (almost) does what you want for JavaScript code.
There's also a php.vim, but I didn't test it.
我的版本类似,但使用
而不是
因为它会触发 JavaScriptLint.vim 的验证。另外,我并不希望每个
{}
都位于多行,因此它会映射到 ctrl + ↩。My version is similar but uses
<C-o>
instead of<Esc>
since it would trigger validation by JavaScriptLint.vim.Also, I don't really want every
{}
to be on multiple lines so it's mapped to ctrl + ↩.