Vim 'expandtab' deja espacios en blanco iniciales

I have some C code, with pointing to the character where Vim's cursor is located and · indicating a leading space.

int main()
····return 0;

I currently have 'expandtab' turned on, and I press 'O', yielding:

int main()
····return 0;

I press the 'Enter' key once, and now I have:

int main()
····return 0;

I press the 'Enter' key one more time, and write some text, giving:

int main()

····// bla bla bla⇦
····return 0;

How do I make Vim not put that leading indentation on the first line after the { ? It is smart enough to remove leading spaces on posterior lineas vacias

If I reindent the file with gg=G, that extra space is removed. But it should not be there in the first place, from what I understand.

Also, if I do not have 'expandtabs' turned on, everything works fine. In other words, if I do the same as above with 'noexpandtabs', there is no extra indentation left hanging around.

Is there a setting somewhere to fix this? Is it a bug?

Mi versión es: VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Apr 5 2012 10:17:30)

actualizaciones: Más información

I can reproduce this with a fairly 'vanilla' set of settings, such as by invoking Vim with: vim -u /etc/vimrc foo.c, and then in the session running set expandtab y set cindent. I have also tried 'set autoindent', which doesn't seem to help for this particular case.

Programas de ci, si y ai settings are all disabled.

I am running on CentOS 6.4, for reference.

Muchas Gracias

preguntado el 11 de septiembre de 13 a las 20:09

This behavior can be very plugin dependent. I could not reproduce your problem with the Fedora and Gentoo Vim install. -

@matthiaskrull What versions of Vim were those? -

pls report the output of these commands: set ci? set si? y set ai? -

7.3, 7.4 and now an Ubuntu vim that comes close to your build (VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Nov 15 2012 00:35:35) with :set autoindent cindent smartindent expandtab -

@Kent: updated at the end of the question. They are all off. -

2 Respuestas

Necesita set ai

de ayuda:

'autoindent' 'ai'       boolean (default off)
                        local to buffer
        Copy indent from current line when starting a new line (typing <CR>
        in Insert mode or when using the "o" or "O" command).  If you do not
        type anything on the new line except <BS> or CTRL-D and then type
        <Esc>, CTRL-O or <CR>, the indent is deleted again.  Moving the cursor
        to another line has the same effect, unless the 'I' flag is included
        in 'cpoptions'.
        When autoindent is on, formatting (with the "gq" command or when you
        reach 'textwidth' in Insert mode) uses the indentation of the first
        When 'smartindent' or 'cindent' is on the indent is changed in
        a different way.
        The 'autoindent' option is reset when the 'paste' option is set.
        {small difference from Vi: After the indent is deleted when typing
        <Esc> or <CR>, the cursor position when moving up or down is after the
        deleted indent; Vi puts the cursor somewhere in the deleted indent}.

tenga en cuenta que

If you do not type anything on the new line except <BS> or CTRL-D and then type <Esc>, CTRL-O or <CR>, the indent is deleted again.

Respondido el 11 de Septiembre de 13 a las 21:09

Alas, it does not work for me. The crux for me is that it behaves one way with expandtab on, and another with it off. Autoindent doesn't seem to change either case. - jwd

@jwd you said in your comment, cindent was on, right? so basically, you tested with cin y ai on, it didn't work? - Kent

Yes, correct. I run vim -u /etc/vimrc foo.c, luego use set cindent expandtab (con o sin autoindent as well), type the file as written above, and perform the steps described, and I have a line with extra whitespace. Easy to see with set list - jwd

@jwd, I do this with vim7.4, no leading spaces:. vim -u /etc/vimrc foo.c set expandtab|set cindent|set ai then copy your text, follow your steps (O, Enter etc),- Kent

Well, it turned out to be a wily plugin causing the issue.

I should have tried with --noplugins earlier (didn't know about that option).

I had the 'ctab.vim' plugin in effect this whole time. Disabling it fixes the issue.

Also, there is a bugfix for this issue provided by Konstantin Lepa at this site:

Respondido el 11 de Septiembre de 13 a las 23:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.