0

Which vim mapping/ plugin will help me in auto-indenting when entering inside braces, brackets or any similar characters. For example:

Input {press enter}

Output

{
       
    cursor here

}

I want this behaviour for brackets, parentheses etc also.

  • 3
    This usually works with an indent plugin/expression for your filetype (see :help indent.txt`); which filetype specifically do you want this for? There may already be a setting in the existing indent plugin to change the behaviour of how this works. – Martin Tournoij Aug 3 at 13:42
0

I guess all bracketing plugins have this behaviour for brackets at least. This is the case of lh-brackets for instance.

And it's quite easy to extend to anything else. The important part is CR+ESC+O, O is what will ensure a correct indent. See this related answer on SO: https://stackoverflow.com/questions/35708395/how-to-move-opening-curly-braces-to-a-new-line-in-vim

This will look like to something like the following (untested)

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

Note: you'll also have to activate auto indent in your sessions.

| improve this answer | |

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.