在 Vim 中自动插入匹配的大括号 [英] Automatically insert a matching brace in Vim

查看:30
本文介绍了在 Vim 中自动插入匹配的大括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花费了方式太多时间摸索,因为 Vim 不像大多数 IDE 那样处理右大括号.这是我想要发生的事情:

I spend way too much time fumbling around because Vim doesn't handle closing braces like most IDEs do. Here's what I want to happen:

输入:

if( whatever )
{ <CR>

得到这个:

if( whatever )
{
  |  
}

其中 表示按下 ENTER 键,| 是光标的位置.这就是 Eclipse 所做的.这就是 Visual Studio 所做的.这就是我想让 Vim 做的事情.

where <CR> mean hit the ENTER key and | is the position of the cursor. This is what Eclipse does. It's what Visual Studio does. And it's what I want Vim to do.

我看过一些插件,尝试了一些,但似乎没有一个给我这种行为.我当然不会是第一个想要这个的程序员.

I've seen a few plugins, tried a few, and none of them seem to give me this behavior. Surely I can't be the first programmer to want this.

推荐答案

在 VimL 中,您可以映射 { 以完全按照您的意愿执行:

In VimL, you can map the { to do exactly as you wish:

inoremap { {<CR>}<Esc>ko

根据您的自动缩进设置,您可能希望在 之后添加 .

depending on your autoindent setup, you may want to add a <BS> after <CR>.

要获得更完整的解决方案,我建议您查看 Luc Hermitte 的 vim 插件.到目前为止,他们从未让我失望过.

For a more complete solution, I'd suggest you take a look at Luc Hermitte's vim plugins. They've never failed me so far.

这篇关于在 Vim 中自动插入匹配的大括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆