Here is what I use for indentation:
set autoindent
set breakindent
set expandtab
set smarttab
set shiftround
set shiftwidth=2 softtabstop=-1 " or maybe shiftwidth=2 softtabstop=8
" https://vi.stackexchange.com/a/15557
inoremap <tab> <c-v><tab>
When I press >>, this indents my code 2 spaces; and when I press Tab, this inserts the tab character. Fine, isn't it?
However, there is a problem.
t------> // 1 tab
s....... // 7 spaces
Enter the Insert mode, put the cursor after the trailing tab or the trailing spaces, and press the Backspace key.
Here is how I would like it to work:
t // tab has been removed
s...... // 5 spaces
And here is how it actually works:
t..... // 5 spaces
s..... // 5 spaces
Of course, I can replace set shiftwidth=2 softtabstop=-1
with
shiftwidth=2 softtabstop=8
, but there will be another problem:
Instead of
t
s...... // 5 spaces
It will be
t
s
So my question:
How can I set up Vim so that...
- When I press >>, it will indent code with 2 spaces
- When I press Tab in Insert mode, it will insert the tab character
- When I press Backspace and there are spaces before it, Backspace should remove 2 spaces per keypress
- When I press Backspace and there are tabs before it, Backspace should remove 1 tab per keypress