The part of "using no external plugins" is the tricky one.
To get the behavior you want, you have to inoremap
the <CR>
key, probably using an <expr>
mapping, that will look at the character under the cursor, see if it's a }
and, in that case, move it two lines down, unindent that line (only if the first one was indented), go back up one, re-indent it...
And if it's not a }
, then just behave like a normal <CR>
(this part is easy with an <expr>
mapping.)
The problem is, when you get that far, you're halfway through to writing a plug-in, so you might as well just get one that was thoroughly debugged for all the corner cases that can happen here.
If you want something really simple though:
inoremap <expr> <CR>
\ getline('.')[col('.')-1]=='}'
\ ? "\<CR>\<C-d>\<C-o>O" : "\<CR>"
It uses a <CR>
followed by a CTRL-D
to unindent the }
, then CTRL-O
to use the O
normal mode command, which inserts a new line above this one. (Using O
here to get the indentation right.)
But, as mentioned, this will not always work as expected. It's very simple.
And next you'll want to fix backspace, or fix it that when you type }
if you're on a }
already you'll skip it... You'll want the plug-in, trust me!