在大括号内时 vim 中自动换行和缩进?

发布于 2024-10-13 16:02:17 字数 424 浏览 9 评论 0原文

例如,如果我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

相守太难 2024-10-20 16:02:17

我最终只是将其硬编码到 .vimrc 中,如下所示

" Make it so that a curly brace automatically inserts an indented line
inoremap {<CR> {<CR>}<Esc>O<BS><Tab>

部分似乎不是必需的,但由于某种原因,它没有像O 命令通常都是这样,所以我必须添加它。

I ended up just hardcoding this into .vimrc, as follows

" Make it so that a curly brace automatically inserts an indented line
inoremap {<CR> {<CR>}<Esc>O<BS><Tab>

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.

许久 2024-10-20 16:02:17

只需安装 auto-pairs 插件 就可以了。

Just install auto-pairs plugin and be happy.

玩世 2024-10-20 16:02:17

我使用 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.

赠佳期 2024-10-20 16:02:17

我的版本类似,但使用 而不是 因为它会触发 JavaScriptLint.vim 的验证。

另外,我并不希望每个 {} 都位于多行,因此它会映射到 ctrl +

inoremap <C-Return> <CR><CR><C-o>k<Tab>

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 + .

inoremap <C-Return> <CR><CR><C-o>k<Tab>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文