Question about tabs and autoindent

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

Question about tabs and autoindent

H. S. Teoh
Hi,

I'm sure this is something very simple, but I can't seem to find any
mention of how to do this in the documentation: I want the autoindent
function to *always* use spaces, never tabs, but I want vim to insert
tabs when I manually type tabs in insert mode. How do I do this?

Currently my settings are:

set shiftwidth=2
set cinoptions=>1s,:0
set expandtab

But this doesn't have the desired effect, since expandtab makes all
tabs turn into spaces, including when I type tab. If I turn it off,
then autoindent will use a tab to replace 8 spaces, but I don't want
that. Is there a way to turn on expandtab for autoindent only? Thanks.


T

--
IBM = I'll Buy Microsoft!
Reply | Threaded
Open this post in threaded view
|

Re: Question about tabs and autoindent

panshizhu
I have the same reqirement as you. I do it as the following:

:set expandtab

:inoremap <silent> <tab> <c-v><tab>

Thank works for me.
Hope that helps.

--
Sincerely
Pan, Shizhu. ext: 2221




"H. S. Teoh" <[hidden email]> wrote on 2005-06-10 12:45:57:

> Hi,
>
> I'm sure this is something very simple, but I can't seem to find any
> mention of how to do this in the documentation: I want the autoindent
> function to *always* use spaces, never tabs, but I want vim to insert
> tabs when I manually type tabs in insert mode. How do I do this?
>
> Currently my settings are:
>
> set shiftwidth=2
> set cinoptions=>1s,:0
> set expandtab
>
> But this doesn't have the desired effect, since expandtab makes all
> tabs turn into spaces, including when I type tab. If I turn it off,
> then autoindent will use a tab to replace 8 spaces, but I don't want
> that. Is there a way to turn on expandtab for autoindent only? Thanks.
>
>
> T
>
> --
> IBM = I'll Buy Microsoft!

Reply | Threaded
Open this post in threaded view
|

Re: Question about tabs and autoindent

Gary Johnson
In reply to this post by H. S. Teoh
On 2005-06-09, "H. S. Teoh" <[hidden email]> wrote:

> Hi,
>
> I'm sure this is something very simple, but I can't seem to find any
> mention of how to do this in the documentation: I want the autoindent
> function to *always* use spaces, never tabs, but I want vim to insert
> tabs when I manually type tabs in insert mode. How do I do this?
>
> Currently my settings are:
>
> set shiftwidth=2
> set cinoptions=>1s,:0
> set expandtab
>
> But this doesn't have the desired effect, since expandtab makes all
> tabs turn into spaces, including when I type tab. If I turn it off,
> then autoindent will use a tab to replace 8 spaces, but I don't want
> that. Is there a way to turn on expandtab for autoindent only? Thanks.

You can force vim to accept a tab as a tab in insert mode by
preceding it with Ctrl-V.  See

    :help expandtab

If you do this often, you might want to use a mapping so that you
don't have to type, or even think about having to type, the Ctrl-V:

    :inoremap <Tab> <C-V><Tab>

HTH,
Gary
Reply | Threaded
Open this post in threaded view
|

RE: Question about tabs and autoindent

Keith Roberts
In reply to this post by H. S. Teoh
>-----Original Message-----
>From: Gary Johnson [mailto:[hidden email]]
>Sent: Thursday, June 09, 2005 10:47 PM
>To: [hidden email]
>Subject: Re: Question about tabs and autoindent
>
>On 2005-06-09, "H. S. Teoh" <[hidden email]> wrote:
>> Hi,
>>
>> I'm sure this is something very simple, but I can't seem to find any
>> mention of how to do this in the documentation: I want the
>autoindent
>> function to *always* use spaces, never tabs, but I want vim
>to insert
>> tabs when I manually type tabs in insert mode. How do I do this?
>>
>> Currently my settings are:
>>
>> set shiftwidth=2
>> set cinoptions=>1s,:0
>> set expandtab
>>
>> But this doesn't have the desired effect, since expandtab makes all
>> tabs turn into spaces, including when I type tab. If I turn it off,
>> then autoindent will use a tab to replace 8 spaces, but I don't want
>> that. Is there a way to turn on expandtab for autoindent
>only? Thanks.
>
>You can force vim to accept a tab as a tab in insert mode by
>preceding it with Ctrl-V.  See
>
>    :help expandtab
>
>If you do this often, you might want to use a mapping so that
>you don't have to type, or even think about having to type, the Ctrl-V:
>
>    :inoremap <Tab> <C-V><Tab>
>
>HTH,
>Gary

Now, I like this, sort of.  But there's a glaring problem with this
technique.

If you start a new line in the following ways, you're ok:
        - <cr> from eol in insert mode
        - o or O from normal mode (or <c-o>o, <c-o>O from insert)
The previous indentation is followed, and the indent consists of all
spaces.

But as soon as you need to indent further than the previous line, you're
hosed -- because the command for indenting (in insert mode) is ...
<tab>, which inserts a hard tab now.

Long story short, I'd eliminate the map and just use <c-v><tab> to
insert a hard tab when that's what you really want.  Another advantage
of doing so is that >> doesn't undo the hard tab you inserted; expandtab
is only applied when a new line is created or when the <tab> key is
pressed in input mode (and you're not in input mode after ^V, you're in
operator-pending mode, as showmode shows :).

-Keith