Skip to main content
Remove "bash" from title and tags
Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

How to make nested indentation in bash in vim?

Source Link
milanHrabos
  • 401
  • 2
  • 5
  • 12

How to make nested indentation in bash in vim?

I would like to make if-fi statement in vim, where the iabbrev will "autocomplete" the if-fi clause:

 autocmd Filetype sh iabbrev if if<esc>ma<cr>ifi<esc>`ak

which upon if generates:

if
fi

That's correct - I do not have to type fi at the end of the clause. So I can continue. For example:

if pwd; then
    echo "bla bla"

fi

Now below the echo, I would like to nest another statement, but this time, If I type if it generates:

if pwd; then
    echo "bla bla"

    if
fi
fi

The second (nested) if-fi broke the indentation. The fi ending should be this:

if pwd; then
    echo "bla bla"

    if
    fi
fi

So the second if-fi clause is withing the first one. But I have no idea how to achieve that. I have

:set autoindent
:set smartindent

So vim remember the level of indentation upon <cr>, so I can get the right indentation in the nested if-fi with one backspace. But I have no idea what key-mapping should I make to achieve that. So what key-mapping should I add to .vimrc as iabbrev in order to generate the right level of indentation in vim?