Tabs and spaces in autoindent mode

25 views
Skip to first unread message

Anton Shepelev

unread,
Sep 14, 2020, 1:08:43 PM9/14/20
to vim...@googlegroups.com
Hello, all

In autoindent and noexpandtab mode, Vim will sometimes
repace a carefully chosen combination of tabs and spaces
with another, in which all the spaces are replaced with tabs
except a few trailing ones, corresponding to the remainder
of dividing the indent width by tab width.

The following example uses a tab width of 3 characters. Both
the lines have a single level of indentation, but the second
line is additionally aligned with the `source' inentifier
above, using spaces. It looks thus:

SomeTypeOfMine source,
destination;

If I denote tabs with `_' and spaces with `.' to show the
indentation structure:

___SomeTypeOfMine source,
___...............destination;

When, however, I add a third line, Vim replaces the
alignment spaces with tabs:

___SomeTypeOfMine source,
___...............destination,
__________________buffer;

whereas I expect it not to interfere with my alignment and
retain the tab-space combination:

___SomeTypeOfMine source,
___...............destination,
___...............buffer;

Is it possible?

Gary Johnson

unread,
Sep 14, 2020, 1:22:39 PM9/14/20
to vim...@googlegroups.com
Try setting 'copyindent'. That should do what you want. See

:help 'copyindent'

HTH,
Gary

Gabriele

unread,
Sep 14, 2020, 2:14:48 PM9/14/20
to vim...@googlegroups.com
On 14/09/2020 19.08, Anton Shepelev wrote:
> Hello, all
>
> In autoindent and noexpandtab mode, Vim will sometimes
> repace a carefully chosen combination of tabs and spaces
> with another <snip>

This is solved (hopefully) by enabling the 'copyindent' and
'preserveindent' options.

The problem resurfaces periodically in the mailing lists.

I have run into it recently and have been meaning to say something about it.

There is terrible documentation (in :h 'autoindent' and :h 'smartindent'
'copyindent' and 'preserveindent' are not mentioned and there are lies
about what they do by default - "Copy indent from current line"? Not
really! -), a puzzling arrangement of options (but that's for historical
reasons) and arguably senseless defaults.

The real behaviour of 'autoindent' and 'smartindent' is better described
(only) in the documentation of 'copyindent' and 'preserveindent' (but
even there it's not complete, there should probably also be a mention of
how Vim converts spaces to tabs - namely looking at the 'shiftwidth'
option, if I'm not mistaken).

Looking in the repository it seems that the missing correction of the
'autoindent' and 'smartindent' documentation was an oversight of the
original patch that introduced copyindent and preserveindent (vim-6-2d),
and was never corrected.

Does really anyone today require 'auto/smartindent' without 'copyindent'
and 'preserveindent'??? And for that to be the default??

I read somewhere in vim-dev that some tools did seem to rely on the
default autoindent behaviour, but even if it were still so breaking
changes are not unheard of in Vim.

In any case, I think that there should at the minimum be very prominent
notices that unless you have very peculiar needs you should enable
copyindent and preserveindent before enabling auto or smartindent.

Anton Shepelev

unread,
Oct 6, 2020, 1:04:34 PM10/6/20
to vim...@googlegroups.com
Gary Johnson to Anton Shepelev:

> > In autoindent and noexpandtab mode, Vim will sometimes
> > repace a carefully chosen combination of tabs and spaces
> > with another, in which all the spaces are replaced with
> > tabs except a few trailing ones, corresponding to the
> > remainder of dividing the indent width by tab width.
> > [...]
>
> Try setting 'copyindent'. That should do what you want.
> See
>
> :help 'copyindent'

Thank you, Gabriele and Gary. That is exactly what I wanted.

Reply all
Reply to author
Forward
0 new messages