Keeping tab indentation of blank lines

classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Keeping tab indentation of blank lines

Laurent Birtz
Hello,

I need help to change the default behavior of the 'autoindent' option.
The doc says:

    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> or <CR>, the indent is deleted again.

I *do not* want vim to remove the tabs when empty lines are inserted by
typing
'escape' or 'return'. It's a pain afterwards to bring the cursor back to the
correct indentation level (both with vim and other editors). Is there
any way
of doing this aside of hacking the sources? I do not use 'cindent' or
'smartindent' options.

Thanks a lot,
Laurent Birtz

Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

A.J.Mechelynck
Laurent Birtz wrote:

> Hello,
>
> I need help to change the default behavior of the 'autoindent' option.
> The doc says:
>
>    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> or <CR>, the indent is deleted again.
>
> I *do not* want vim to remove the tabs when empty lines are inserted by
> typing
> 'escape' or 'return'. It's a pain afterwards to bring the cursor back to
> the
> correct indentation level (both with vim and other editors). Is there
> any way
> of doing this aside of hacking the sources? I do not use 'cindent' or
> 'smartindent' options.
>
> Thanks a lot,
> Laurent Birtz
>
>

I don't think there is a straightforward way. What you could do is start
the first nonempty indented line, and then (once it _is_ nonempty) add
the empty line(s) above it. "Write the code first, beautify afterwards".
To add an empty line above the current line in Insert mode you can use
Ctrl-O O (control-oh followed by shift-oh).


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

iler.ml
In reply to this post by Laurent Birtz
On 9/9/06, Laurent Birtz <[hidden email]> wrote:

> Hello,
>
> I need help to change the default behavior of the 'autoindent' option.
> The doc says:
>
>     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> or <CR>, the indent is deleted again.
>
> I *do not* want vim to remove the tabs when empty lines are inserted by
> typing
> 'escape' or 'return'. It's a pain afterwards to bring the cursor back to the
> correct indentation level (both with vim and other editors). Is there
> any way
> of doing this aside of hacking the sources? I do not use 'cindent' or
> 'smartindent' options.

I do not quite understand your complaint. When I set 'autoindent', and
press Enter Enter, idnent is preserved (and that's after empty line).
Is this not enough ?

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

iler.ml
In reply to this post by Laurent Birtz
On 9/9/06, Laurent Birtz <[hidden email]> wrote:

> Hello,
>
> I need help to change the default behavior of the 'autoindent' option.
> The doc says:
>
>     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> or <CR>, the indent is deleted again.
>
> I *do not* want vim to remove the tabs when empty lines are inserted by
> typing
> 'escape' or 'return'. It's a pain afterwards to bring the cursor back to the
> correct indentation level (both with vim and other editors). Is there
> any way
> of doing this aside of hacking the sources? I do not use 'cindent' or
> 'smartindent' options.

Can you try this to see whether it does what you want ?:
         :imap <Enter> <Enter><space><bs>

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

Laurent Birtz

> Can you try this to see whether it does what you want ?:
>         :imap <Enter> <Enter><space><bs>
>
I get the following behavior:

When I press 'o', it starts an empty line correctly indented. If
I press 'escape' immediately, the indentation is removed and the
cursor moves to column 1. If I press 'enter' instead, the
indentation is removed for the first line, but the subsequent lines
remain indented when I finally press 'escape'. Picture:

if(1 < 2)
{
        int i; // press 'o' here.
.              // press 'enter' here: not correctly indented.
        .      // press 'enter' here: correctly indented.
        .      // press 'escape' here: correctly indented.
}

I tried adding ':imap o o<space><bs>', but it made no difference.
I suspect that 'vim' checks if the line is really empty and thus
is not fooled by the 'space'-'backspace' sequence.

Thanks for the help!
Laurent Birtz
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

iler.ml
On 9/10/06, Laurent Birtz <[hidden email]> wrote:

>
> > Can you try this to see whether it does what you want ?:
> >         :imap <Enter> <Enter><space><bs>
> >
> I get the following behavior:
>
> When I press 'o', it starts an empty line correctly indented. If
> I press 'escape' immediately, the indentation is removed and the
> cursor moves to column 1. If I press 'enter' instead, the
> indentation is removed for the first line, but the subsequent lines
> remain indented when I finally press 'escape'. Picture:
>
> if(1 < 2)
> {
>         int i; // press 'o' here.
> .              // press 'enter' here: not correctly indented.

Correctly indented here for me, after o<Enter>.

Somewthing is wrnog either with eyour settings,
or with vim version. You didn't answer my question
regarding your vim version (:version)

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

Benjamin Esham
Yakov Lerner wrote:

> Laurent Birtz wrote:
>
>> When I press 'o', it starts an empty line correctly indented. If I  
>> press
>> 'escape' immediately, the indentation is removed and the cursor  
>> moves to
>> column 1. If I press 'enter' instead, the indentation is removed  
>> for the
>> first line, but the subsequent lines remain indented when I finally
>> press 'escape'.
>
> Correctly indented here for me, after o<Enter>.
>
> Somewthing is wrnog either with eyour settings, or with vim  
> version. You
> didn't answer my question regarding your vim version (:version)
I get this behavior with 7.0.94, but it's done this for as long as I can
remember (I've been using Vim since 6.2, I think).  I always just  
assumed
that it was standard (if annoying) behavior.  Some relevant options  
set are
autoindent, ts=4, sw=4, noet, and bs=2.

And actually, if I press 'o' on an indented line, press Enter a bunch of
times, and then Esc, /none/ of the newly-created lines are indented.

--
Benjamin D. Esham
[hidden email]  |  AIM: bdesham128  |  Jabber: same as e-mail
“...when two opposite points of view are expressed with equal
intensity, the truth does not necessarily lie exactly halfway
between them. It is possible for one side to be simply wrong.”
                                                — Richard Dawkins

PGP.sig (193 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

iler.ml
On 9/10/06, Benjamin Esham <[hidden email]> wrote:

> Yakov Lerner wrote:
>
> > Laurent Birtz wrote:
> >
> >> When I press 'o', it starts an empty line correctly indented. If I
> >> press
> >> 'escape' immediately, the indentation is removed and the cursor
> >> moves to
> >> column 1. If I press 'enter' instead, the indentation is removed
> >> for the
> >> first line, but the subsequent lines remain indented when I finally
> >> press 'escape'.
> >
> > Correctly indented here for me, after o<Enter>.
> >
> > Somewthing is wrnog either with eyour settings, or with vim
> > version. You
> > didn't answer my question regarding your vim version (:version)
>
> I get this behavior with 7.0.94, but it's done this for as long as I can
> remember (I've been using Vim since 6.2, I think).  I always just
> assumed
> that it was standard (if annoying) behavior.  Some relevant options
> set are
> autoindent, ts=4, sw=4, noet, and bs=2.
>
> And actually, if I press 'o' on an indented line, press Enter a bunch of
> times, and then Esc, /none/ of the newly-created lines are indented.

Yes with these two mappings mentioned earlier in this thread,
all lines are indented after o<Enter><Enter><Enter>:

                  :inoremap <Enter> <Enter><space><bs>
                  :nnoremap o o<space><bs>

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

iler.ml
In reply to this post by Benjamin Esham
On 9/10/06, Benjamin Esham <[hidden email]> wrote:
> I get this behavior with 7.0.94, but it's done this for as long as I can
> remember (I've been using Vim since 6.2, I think).

Some RedHat-8-based appliences around here have vim 6.1 :-)

Yakov
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

Laurent Birtz
In reply to this post by iler.ml

> Yes with these two mappings mentioned earlier in this thread,
> all lines are indented after o<Enter><Enter><Enter>:
>
>                  :inoremap <Enter> <Enter><space><bs>
>                  :nnoremap o o<space><bs>


Got it! With these two mappings, it works correctly.
Apparently on my system (vim version 6.4, modified by Gentoo)
':imap o o<space><bs>' does not work as expected.

Thanks!
Laurent Birtz
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

A.J.Mechelynck
In reply to this post by Benjamin Esham
Benjamin Esham wrote:

> Yakov Lerner wrote:
>
>> Laurent Birtz wrote:
>>
>>> When I press 'o', it starts an empty line correctly indented. If I press
>>> 'escape' immediately, the indentation is removed and the cursor moves to
>>> column 1. If I press 'enter' instead, the indentation is removed for the
>>> first line, but the subsequent lines remain indented when I finally
>>> press 'escape'.
>>
>> Correctly indented here for me, after o<Enter>.
>>
>> Somewthing is wrnog either with eyour settings, or with vim version. You
>> didn't answer my question regarding your vim version (:version)
>
> I get this behavior with 7.0.94, but it's done this for as long as I can
> remember (I've been using Vim since 6.2, I think).  I always just assumed
> that it was standard (if annoying) behavior.  Some relevant options set are
> autoindent, ts=4, sw=4, noet, and bs=2.
>
> And actually, if I press 'o' on an indented line, press Enter a bunch of
> times, and then Esc, /none/ of the newly-created lines are indented.
>
> --
> Benjamin D. Esham
> [hidden email]  |  AIM: bdesham128  |  Jabber: same as e-mail
> “...when two opposite points of view are expressed with equal
> intensity, the truth does not necessarily lie exactly halfway
> between them. It is possible for one side to be simply wrong.”
>                                                — Richard Dawkins

Indentation is a moot point on an empty line.

If I hit "o" (in Normal mode) on an indented line, or <Enter> after it,
then <Enter> a bunch of times, and then type some text, it will be
indented like the original line. The blank lines in between will be
truly empty, not filled with enough spaces and/or tabs to make them as
long as the indent, which is proper behaviour IMHO (I hate editors which
leave a bunch of useless spaces or tabs at the end of lines).

If I hit <Enter> at the end of an indented line, then <Enter> a bunch of
  times, then hit the space bar, the Backspace key, and _then_ the Esc
key, the last line keeps its indenting spaces and/or tabs. The blank
lines before it are empty. The cursor goes back to column 1, but hitting
A on this last line restores the cursor to after the indent, where text
can be typed. (Hitting <Esc> immediately after the last <Enter> without
<Space><BS> loses the indent.)

I'm using Vim 7.0.97, huge version with GTK2/Gnome GUI, on SuSE Linux
9.3 with X11 and kde; and I have (among others) the following settings:

   :filetype plugin on
   :filetype indent off
   :set autoindent smartindent nocindent indentexpr=
   :set list listchars=tab:\ \ ,eol:¶,nbsp:~
   " the latter two are helpful to spot end-of-line spaces or tabs


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

A.J.Mechelynck
In reply to this post by Laurent Birtz
Laurent Birtz wrote:

>
>> Yes with these two mappings mentioned earlier in this thread,
>> all lines are indented after o<Enter><Enter><Enter>:
>>
>>                  :inoremap <Enter> <Enter><space><bs>
>>                  :nnoremap o o<space><bs>
>
>
> Got it! With these two mappings, it works correctly.
> Apparently on my system (vim version 6.4, modified by Gentoo)
> ':imap o o<space><bs>' does not work as expected.
>
> Thanks!
> Laurent Birtz
>

":imap o o<space><bs>" means "add a space and delete it whenever you
insert a lowercase o in _Insert_ mode". This creates an undo point after
each lowercase o, which is probably not what you want.

See ":map-overview"


Best regards,
Tony.
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

Laurent Birtz

>
> ":imap o o<space><bs>" means "add a space and delete it whenever you
> insert a lowercase o in _Insert_ mode". This creates an undo point
> after each lowercase o, which is probably not what you want.
>
> See ":map-overview"
>
Eep! Indeed. I'm a vim newbie and I didn't RTFM properly.

Thanks!
Laurent Birtz
Reply | Threaded
Open this post in threaded view
|

Re: Keeping tab indentation of blank lines

Yegappan Lakshmanan
In reply to this post by Laurent Birtz
Hello,

On 9/9/06, Laurent Birtz <[hidden email]> wrote:

> Hello,
>
> I need help to change the default behavior of the 'autoindent' option.
> The doc says:
>
>     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> or <CR>, the indent is deleted again.
>
> I *do not* want vim to remove the tabs when empty lines are inserted by
> typing
> 'escape' or 'return'. It's a pain afterwards to bring the cursor back to the
> correct indentation level (both with vim and other editors).
>

To bring the cursor to the correct indentation level in an empty line,
you can use CTRL-F in insert mode. For more information, read

  :help i_CTRL-F
  :help 'indentkeys'
  :help 'cinkeys'

- Yegappan