如何在 Vim 中将左花括号移动到新行? [英] How to move opening curly braces to a new line in Vim?

查看:25
本文介绍了如何在 Vim 中将左花括号移动到新行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名 C# 开发人员,我是使用 Vim 的新手.我很好奇,你会在 .vimrc 文件中放什么,这样当你输入花括号并按 Enter 时,就像这样:

I'm a C# developer and I'm new to using Vim. I'm curious, what would you put in your .vimrc file so that when you type a curly brace and hit Enter, like this:

public void Foo() {<Enter>

那个大括号换了一行,插入符号移动到大括号的内部,如下所示:

that brace gets put on a new line, and the caret moves to the innards of the braces, like this:

public void Foo()
{
    |
}

任何帮助将不胜感激,谢谢!

Any help would be appreciated, thanks!

推荐答案

你的问题很接近 用大括号自动关闭插件问题 where

Your question is very close to auto-close plugin issuse with curly brackets where

inoremap <expr> <cr> getline(".")[col(".")-2:col(".")-1]=="{}" ? "<cr><esc>O" : "<cr>"

回答问题——这有点像我在 lh-brackets 中使用的内容.

answers the question -- this is somewhat what I use in lh-brackets.

在这里,您似乎没有使用任何括号插件.要搜索的文本变为 '\S\s*{$',您可以插入:{}O,或者只是 {}O 如果该行只有 {.如果光标在 {} 内,您还应该集成之前的测试.这变成了:

Here, it seems you aren't using any bracketing plugin. The text to search becomes '\S\s*{$', and you can insert instead: <BS><cr>{<cr>}<esc>O, or just {<cr>}<esc>O if there is only { on the line. If the cursor is within {}, you should also integrate the previous test. This becomes:

inoremap <expr> <cr> 
   \   getline(".") =~ '\S\s*{$'                 ? "<bs><cr>{<cr>}<esc>O"
   \ : getline('.') =~ '^\s*{$'                  ? "<cr>}<esc>O" 
   \ : getline(".")[col(".")-2:col(".")-1]=="{}" ? "<cr><esc>O"
   \ :                                             "<cr>"

这篇关于如何在 Vim 中将左花括号移动到新行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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