When I create a curly braces and press enter inside it I get this behavious

{|} -> enter key


Now how do I get vim to do this

{|} -> enter key


my vimrc brief

filetype plugin indent on
set lazyredraw
set smartcase
set smartindent
set ignorecase
set nocompatible
set number
set numberwidth=1
set autoindent
set backspace=2
set backspace=indent,eol,start
set tabstop=4
set shiftwidth=4
set shellslash
set clipboard=unnamed
set smarttab
  • For what filetype? Please also show the output of the various indent settings. – Christian Brabandt Jun 17 '19 at 10:39
  • it happens for all filetypes. I have notice it in C and CSS. That's the only indent settings I have rest of all are just mappings – neelabalan Jun 17 '19 at 10:59
  • Please show us what you have tried so far. – mMontu Jun 17 '19 at 11:06
  • as of now I am using iab { {<CR>}<Esc>ko to have the kind of behaviour I want. Do you need to know any other thing? VS Code does what I described in my question. So I thought why not it is possible with vim. – neelabalan Jun 17 '19 at 11:08
  • I've asked because there are several plugins providing that sort of behavior. I'll post an answer with an example. – mMontu Jun 17 '19 at 11:15

While mappings and abbreviations in insert mode should work, Vim has a large number of plugins -- some of them may have proven and largely tested solutions since what you are asking is probably a common feature.

Some of the keywords you could try searching plugins for are complete and braces. This last search, for example, brings vim-c-cr:

Finish braces, brackets and parentheses with ctrl+enter in vim

A very simple and small (33 lines of vimscript) plugin that maps ctrl+enter () and ctrl+c ( for terminal-vim users) in insert mode to finish braces ({}), brackets ([]) and parentheses (()) for you and puts your cursor tabbed over on a blank line between them.

But you certainly can find more plugins with similar behavior.

| improve this answer | |
  • I will take a look at the plugins you mentioned. Thank you very much – neelabalan Jun 17 '19 at 11:48
  • @neelabalan I hope it helps – mMontu Jun 17 '19 at 11:59
  • It did. I am now using the vim-c-cr plugin you mentioned. Thank you – neelabalan Jun 17 '19 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.