1

I normally write my functions this way

public void test() {}

to continue, I need to insert a new line from within the brackets, so the final output is this:

public void test(){
    // cursor is here now
}

To do so in vim, I follow these steps: (the pipe is where the cursor at)

public void test() {}

I press Esc to go to normal mode, then j so now my cursor is here:

public void test() {|}

I press i to go to insert mode and then enter twice so now I have this:

public void test() {

|}

then I press Esc to go to normal mode and then k to go up a line, and then i to go to insert mode and then tab to get to the desired outcome.

Is there a better way?

Note: I really don't wanna use plugins, I hope native vim helps me.

1
2

Turn on :h 'autoindent' and :h 'smartindent' , assume current typing is:

public void test(){

input enter}:

public void test(){
}

input escO:

public void test(){
    |
}

If you prefer :h i_CTRL-O , replace esc with ctrl-o.

FYI, another way to reindent current line in insert mode is :h i_CTRL-F .

0

If anyone else is here from the future, you could also add on to @dedowsdi's answer by making a keybinding that does the operation for you.

In your .vimrc:

inoremap bb {<Enter>}<Esc><S-o>

Reload vim and while in insert mode hit bb.

2
  • 1
    The <S-o> can be written as just O, and you might want to use inoremap. – D. Ben Knoble Mar 12 at 13:34
  • Great additions, thank you, D. Ben Knoble. – CheeseCoke Mar 18 at 5:10

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.