输入括号时如何使VIM折叠不打开

发布于 2025-01-19 08:35:18 字数 343 浏览 1 评论 0原文

我正在使用 vim 和 Foldmethod=syntax;当我在代码中的某处输入括号时,它会立即打开所有后续折叠。我可以明白为什么会发生这种情况:开括号改变其他括号的对应关系,并且所有折叠同时改变。我能以某种方式阻止这种情况吗?我不知道,也许在所有折叠都打开之前有延迟?

编辑:

Vim 版本(vim --version 的输出):

VIM - Vi IMproved 8.1(2018年5月18日,编译于2022年2月1日09:16:32)

包含的补丁:1-2269、3612、3625、3669、3741

操作系统:Ubuntu 20.04

I am using vim and foldmethod=syntax; When I type brackets somewhere in my code, it immediately opens all following folds. I can see why that happens: the open bracket changes the correspondences of the other brackets and all the folds change at the same time. Can I somehow prevent that? I don't know, maybe something like a delay before all the folds are opened?

Edit:

Vim version (output of vim --version):

VIM - Vi IMproved 8.1 (2018 May 18, compiled Feb 01 2022 09:16:32)

Included patches: 1-2269, 3612, 3625, 3669, 3741

OS: Ubuntu 20.04

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

独留℉清风醉 2025-01-26 08:35:18

你可以

inoremap { }<left>{

按照@eff提到的那样尝试。

inoremap { {} 似乎不再起作用了。

You may also re-map `<cr>` to smartly make `{}` a block

```vim
inoremap <expr> <cr> MapEnterKey()

function! MapEnterKey()
  let line = getline('.')
  let col_pos = col('.')
  let prev_char = line[col_pos-2]
  let next_char = line[col_pos-1]
  if prev_char == '{' && next_char == '}'
    return "\<cr>\<esc>O"
  else
    return "\<cr>"
  endif
endfunction

You can try

inoremap { }<left>{

as mentioned by @eff.

inoremap { {}<left> seems not to work any more.

You may also re-map `<cr>` to smartly make `{}` a block

```vim
inoremap <expr> <cr> MapEnterKey()

function! MapEnterKey()
  let line = getline('.')
  let col_pos = col('.')
  let prev_char = line[col_pos-2]
  let next_char = line[col_pos-1]
  if prev_char == '{' && next_char == '}'
    return "\<cr>\<esc>O"
  else
    return "\<cr>"
  endif
endfunction
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文