How to make vim folding not open when typing brackets

Viewed 92

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

1 Answers

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
Related