remove only one character at a time.
type result ~
<Tab> ........
<Tab><BS> .......
CHANGING TABS IN SPACES (AND BACK)
Setting 'expandtab' does not immediately affect existing tab characters. In
order to purge a file from all its horizontal tab characters, Vim 5.3
introduced the |:retab| command. Use these commands: >
:set expandtab
:retab
This is a little bit dangerous, because it can also change tabs inside a
string. To check if these exist, you could use this: >
/"[^"\t]*\t[^"]*"
It's recommended not to use actual tab characters inside a string. Replace
them with "\t" to avoid trouble.
The other way around works just as well: >
:set noexpandtab
:retab!
SOFT TAB STOPS
When using only spaces, or a mix of spaces and horizontal tabs, one gets the
unpleasant feeling that the two keys <Tab> and <BS> do not act in mirror, as
they do when using only tab characters.
Vim 5.4 introduced the 'softtabstop' option. On top of the (hard) tab stops
used to display the horizontal tab characters in the text, Vim adds extra
soft tab stops dedicated only to the cursor. When 'softtabstop' is set to a
positive value, and the <Tab> key will push the cursor to the next soft tab
stop. Vim will insert the correct combination of tab characters and spaces to
make the effect visually. Likewise pressing <BS> will have the cursor try to
reach the nearest soft tab stop. The following example uses
`:set softtabstop=4`
type result ~
<Tab> ....
<Tab><Tab>a ------->a
<Tab><Tab>a<Tab> ------->a...
<Tab><Tab>a<Tab><BS> ------->a
To maintain global coherence, one can `:set softtabstop=-1` so that
the value of 'shiftwidth' is use for the number of columns between two soft
tab stops.
If you prefer to have different values for 'shiftwidth' and 'softtabstop',
you can still do so and use <C-t> to indent with 'shiftwidth'. Or you can
use the 'smarttab' option introduced in Vim 5.6, allowing for a unified
<Tab> key that knows what to do in the different situations.
VARIABLE TAB STOPS
As we said before, the ASCII table was designed to remotely control
teleprinters. A given teleprinter could be configured to have their physical
tab stops have variable spacing. After all, the ^I control character was
only stipulating: go to the next tab stop wherever it is.
Vim 7.3 introduced 'vartabstop' to emulate the same functionality. For
example if Vim was compiled with `+vartabs` and `:set vartabstop=2,4` one gets
actual character result ~
^I ->
^I^I ->--->
^I^I^I ->--->--->
Similarly, 'varsofttabstop' was also introduced, to have variably spaced
soft tab stops. With `:set varsofttabstop=2,4` one gets
type result ~
<Tab> ..
<Tab><Tab> ......
<Tab><Tab><Tab> ------->....
EXAMPLES OF CONFIGURATION
By default, Vim is configured to use only tabs: >
:set tabstop=8
:set shiftwidth=8
:set noexpandtab
:set softtabstop=0
:set nosmarttab
<
If you want to write C code as if it were Python (only spaces, with indents
of 4 spaces), here is what you can use: >
:set shiftwidth=4
:set softtabstop=-1
:set expandtab
<
If you want the same behavior but with better control over alignment
(e.g. lining up parameters or comments in multiples of 2 spaces), use: >
:set shiftwidth=4
:set softtabstop=2
:set expandtab
:set smarttab
<
If instead, you would like to write C code like Bram Moolenaar would have
(using a mix of tabs and spaces), you can use >
:set shiftwidth=4
:set softtabstop=-1
<
==============================================================================
*30.6* Formatting comments
One of the great things about Vim is that it understands comments. You can
ask Vim to format a comment and it will do the right thing.
Suppose, for example, that you have the following comment: >c
/*
* This is a test
* of the text formatting.
*/
You then ask Vim to format it by positioning the cursor at the start of the
comment and type: >
gq]/
"gq" is the operator to format