Enlico
Enlico

Reputation: 28416

How can I insert tabs 4 spaces-wide and delete them by two spaces?

I like this type of indentation in C++:

Example:

class A {
    int x;
  public:
    int y;
}
int func() {
    return 1;
}

Therefore I would like

To clarify, I am not and will not use real tab character. Only spaces.

Upvotes: 0

Views: 460

Answers (2)

Dmitry
Dmitry

Reputation: 446

This can be done by cino-g option when using cindent. For example, add this line in .vimrc

:set cinoptions=g2

Also read following help topics to see more about C/C++ indenting:

:h C-indenting
:h 'cinoptions'
:h cinoptions-values
:h cino-g

Upvotes: 3

Matt
Matt

Reputation: 15091

Although the question is probably more about C++ formatting tools, but, anyway, this is how you can do it in Vim:

Tab to insert indentation spaces 4 by 4

shift commands < and > should still act of a 4 spaces basis

" < and > move to the next 'multiple of four' cursor position
set shiftwidth=4 shiftround
" newly inserted tabs follow shiftwidth
set expandtab softtabstop=-1

Backspace to delete indentation spaces 2 by 2

inoremap <expr><BS> search('^\s\+\%#', 'bn', line('.')) ? printf('<C-O>%dX', shiftwidth() / 2) : '<BS>'

That is, if the cursor is preceded only by spaces then hitting backspace once deletes shiftwidth() / 2 chars. Otherwise it's just a single backspace.

Upvotes: 1

Related Questions