How to make VI use spaces instead of tab even when I press backspace key?

0

1

When I tried to use yml, it requires NO TAB, ONLY SPACE policy. I realized I need a special setting for this.

Anyway, I can't live without tabs. So I want to make my VI behave like this.

  • When I press tab key, input many space characters aligned by tab stops.
  • When I press backspace key, delete many space characters aligned by tab stops.

How can I archive this functionality?

Eonil

Posted 2012-01-11T00:35:12.087

Reputation: 4 206

Answers

1

Original vi: doesn't know about et, you could experiement with different values for ts (tabstop) and sw (shiftwidth, i.e. the amount << and >> indent a line or block of text). Basically, whenever sw is set to something lower (usually a fraction of) the value assigned to ts, indenting will use spaces instead of tabs. Beware that when a multiple of shifts add up to a full tabstop, a tab will be used in that place. Default is for both to be 8, and a number of other commands (such as expand), base their assumptions on that, so a text written with mixed tabs and spaces where ts has been tweaked may be misaligned when viewed lateron with a different setting for tabstop. To achieve your goal, you could first use /bin/expand to replace all tabstops with spaces in your existing text, then pull the text into vi, :set sw=4,ts=20, and try if these settings suit you. If they do, you can make them permanent by writing them into your .exrc

Another way, which would allow you to use tab and backspace the way you prefer (instead of shifting with << and >>) would be to allow yourself to write the text the way you prefer, and pipe it through /bin/expand prior to processing with yml, wither as part of your command chain, or from within vi itself:

:1,$!expand

In vim: you can use the et (expandtab) setting as already suggested, and even convert all tabs into spaces by uning a builtin (which has the advantage of knowing your tabstop settings, incase you tweaked them, without having to be told explicitely, as expand needs to).

:set et|retab

other interesting settings would include sts (softtabstop) and smarttab, with all these settings adapted to suit your needs, even ai (autoindent) would behave consistently and throw spaces instead of tabs.

:set et
:set sw=3
:set sts=3
:set smarttab
:set ai

or, in one line:

:set et sw=3 sts=3 smarttab ai

Again, once you agree with these settings, place them in your .vimrc

Tatjana Heuser

Posted 2012-01-11T00:35:12.087

Reputation: 175

0

:set et 

You'll have to give up backspace and use < < instead.

You might be able to map backspace to unindent but there might be undesirable consequences.

RedGrittyBrick

Posted 2012-01-11T00:35:12.087

Reputation: 70 632