[comp.editors] Question on tab stops in vi

jerry@Kesa.COM (Jerry E. Dunmire) (05/10/91)

Is there a way, short of changing editors, to deal with the
following problem?

   Is there a way to get a 4 space indent in vi without changing tab stops?

   I've tried re-mapping the tab key, but without any luck.

Background:
   We have a corporate style the calls for indenting blocks in 4 space
	increments.  In older-simpler days, we set the tabstops in vi to 4,
	the hardtabs to 8, and used pr -e4 whenever we printed.  But in
	today's environment things aren't so simple.  There are such a
	wide variety of tools that we use, under both UNIX and MS-DOS,
	that specifying tab stops for each tool is a burden.  In addition,
	many of the tools do not even support adjustable tab stops.

Thanks in advance for you help.
-- 
Jerry E. Dunmire                      Kesa Corporation
net: jerry@kesa.com                   4701 Patrick Henry Dr. - Suite 1801
att: (408) 748-1814                   Santa Clara, CA 95054  USA

martelli@cadlab.sublink.ORG (Alex Martelli) (05/11/91)

jerry@Kesa.COM (Jerry E. Dunmire) writes:
	...
:   Is there a way to get a 4 space indent in vi without changing tab stops?
Yup, :set shiftwidth 4.  You now need a ^T to indent (and a ^D to unindent),
although I guess you could :map! ^I to ^T if this bothers you.  This of
course also affects the < and > commands.
MY own dream would be to have autoindent *without* allowing vi to use
tabs or blanks according to some optimization criterion, but rather
forcing it to use, say, the same leading whitespace as the above line
(whose indent is being copied).  Unfortunately, having leading ws as
tabs or spaces is NOT equivalent for many tools, such as f77 and make.
This may have been an unfortunate design decision on said tools' authors
part, but it would be nice to :set notaboptimize and cease worrying...
-- 
Alex Martelli - CAD.LAB s.p.a., v. Stalingrado 53, Bologna, Italia
Email: (work:) martelli@cadlab.sublink.org, (home:) alex@am.sublink.org
Phone: (work:) ++39 (51) 371099, (home:) ++39 (51) 250434; 
Fax: ++39 (51) 366964 (work only), Fidonet: 332/407.314 (home only).

hansm@cs.kun.nl (Hans Mulder) (05/13/91)

In <1991May09.190644.13627@Kesa.COM> jerry@Kesa.COM (Jerry E. Dunmire) writes:


>Is there a way, short of changing editors, to deal with the
>following problem?

>   Is there a way to get a 4 space indent in vi without changing tab stops?

Just put the line

:set shiftwidth=4

in your .exrc file.  If you absolutely hate typing, you can abbreviate it to:

se sw=4

>   I've tried re-mapping the tab key, but without any luck.

You're supposed to use ^T to increment indent level, and ^D to decrement it.
Vi will automagically produce to optimal mix of tabs and spaces.


Hans Mulder	hansm@cs.kun.nl

kbe@dde.dk (Kjeld Borch Egevang) (05/16/91)

hansm@cs.kun.nl (Hans Mulder) writes:

>   I've tried re-mapping the tab key, but without any luck.

It is possible. Type

:map! ^V^V<tab> xxxx

to <tab> to xxxx

| kbe@dde.dk      (Kjeld Borch Egevang) | Dansk Data Elektronik
|                                       | Denmark

alien@hpdmd48.boi.hp.com (Tom von Alten) (05/18/91)

And of course, if you want to get rid of the tabs for compatibility
with foreign software, you can pipe the file through 'expand'.  The inverse
function is provided by 'unexpand'.

gaynor@paul.rutgers.edu (Silver) (05/18/91)

I personally never put tabs in ANYTHING unless the tab character itself is
required (eg, most `make's require tabs, shudder).  They're more trouble than
they're worth.  Plus, this leaves you a big, fat key on the keyboard to which
esoteric indenting functions can be bound.

Regards, [Ag]

asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) (05/23/91)

Written in article <1991May09.190644.13627@Kesa.COM>
    by jerry@Kesa.COM (Jerry E. Dunmire):

>    Is there a way to get a 4 space indent in vi without changing tab stops?
> 
>    I've tried re-mapping the tab key, but without any luck.

This is going to kill you, until you get used to it, but this is at
least one solution that will work.

What you want is a shift-width of 4.  (:set sw=4)

Then, when entering your input, instead of hitting "tab", you hit
Control-T (^T).

No, I'm sorry, you can _not_ map the "tab" into ^T.  I've tried, and it 
won't work.  Depending on your terminal, you may be able to map some 
other key into it, eg., if it's located conveniently, map F3 or some 
such.  But tab?  No way.  It may be possible, but I doubt it.  

Rather than entering the "set" command each time, put it in either your 
EXINIT environment variable, or add it to your $HOME/.exrc file.  (I 
prefer the latter method, as I can add lots of sets and maps and stuff 
without the en-var getting too long to read.)

This will kill you, as it did me, because after implementing this
change, you will spend the first month hitting "tab" instead of ^T,
getting a tab instead of a 4-space indent, then cursing yourself as you
fumble around setting it back to what it should be.

If you have auto-indent set (:set ai), you'll also be able to do C
and/or Pascal type coding, e.g:

    if (truth) then
        Do it
        Do some more
        Do everything else
    else
        Do some other thing
    endif

With auto-indent set, you won't have to ^T again after the "Do it."
It'll just bounce back to the same indent level for "Do some more" and
"Do everything else."  When it's time to put in the "else," enter ^D,
which is a "reverse-tab" of sorts.  Entering 0^D brings you back to the
left margin.

So, your $HOME/.exrc file would look like:

    set sw=4
    set ai

*MY* problem is that vi automagically replaces every 8 spaces with a
_real_ hardtab (^I), which, at least for posting to the net, I'd like 
to be able to turn off.  So far, if I use indents for anything, I 
double-check at the end with (:%s/^I/        /g).

Does anybody know how to turn _this_ "feature" off?
--
Alvin ===== asylvain@felix.UUCP ===== hplabs!felix!asylvain ===== 
"hplabs!felix!asylvain"@uunet.uu.net
(I always try to respond to mail, if possible.  If you don't hear back 
from me, try changing "hplabs" to "ccicpg," "spsd," or "lawnet.")
DISCLAIMER: It's all in fun, folks, no flames intended.  Any similarity
between my opinion and that of my employer is purely coincidental and
sufficient reason to change my opinion, although I'll still be right.

hansm@cs.kun.nl (Hans Mulder) (05/24/91)

In <163509@felix.UUCP> asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) writes:
>Written in article <1991May09.190644.13627@Kesa.COM>
>    by jerry@Kesa.COM (Jerry E. Dunmire):
>>    Is there a way to get a 4 space indent in vi without changing tab stops?
>> 
>>    I've tried re-mapping the tab key, but without any luck.

>This is going to kill you, until you get used to it, but this is at
>least one solution that will work.

>What you want is a shift-width of 4.  (:set sw=4)

Yes.

>Then, when entering your input, instead of hitting "tab", you hit
>Control-T (^T).

>No, I'm sorry, you can _not_ map the "tab" into ^T.  I've tried, and it 
>won't work.  Depending on your terminal, you may be able to map some 
>other key into it, eg., if it's located conveniently, map F3 or some 
>such.  But tab?  No way.  It may be possible, but I doubt it.  

Try quoting it with the correct number of control-V's:

" set tabstop to 4 spaces:
:set shiftwidth=4
:map! ^V	 ^T
" That's control-V tab space control-T
" It's entered as control-V control-V tab space control-V control-T

>*MY* problem is that vi automagically replaces every 8 spaces with a
>_real_ hardtab (^I), which, at least for posting to the net, I'd like 
>to be able to turn off.  So far, if I use indents for anything, I 
>double-check at the end with (:%s/^I/        /g).

Why don't you use G!1Gexpand^M  ?  That has the advantage of doing the
right thing when you have a tab in the middle of a line.

>Does anybody know how to turn _this_ "feature" off?

No.  The closest I have gotten is :set tabstop=99
This works if you don't ever want a tab in your file and your lines are
never indented that far.


Btw, I tend to use the long names of the variables (shiftwidth, tabstop)
in my .exrc file, so I can remember which is which when I look at it
again 3 months later.  I use the shortforms (sw, ts) interactively.

--
Have nice day,

Hans Mulder	hansm@cs.kun.nl

ken-w@is.s.u-tokyo.ac.jp (WAKITA Ken) (05/24/91)

In article <163509@felix.UUCP> asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) writes:

 > *MY* problem is that vi automagically replaces every 8 spaces with a
 > _real_ hardtab (^I), which, at least for posting to the net, I'd like 
 > to be able to turn off.  So far, if I use indents for anything, I 
 > double-check at the end with (:%s/^I/        /g).
 > 
 > Does anybody know how to turn _this_ "feature" off?

My setting is not a solution but is rather a compromise.

set ts=100

--
Ken WAKITA (ken-w@is.s.u-tokyo.ac.jp)

			  		Department of Information Science,
						University of Tokyo, Japan.

ian@unipalm.uucp (Ian Phillipps) (05/24/91)

asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) writes:

>Written in article <1991May09.190644.13627@Kesa.COM>
>    by jerry@Kesa.COM (Jerry E. Dunmire):

>>    Is there a way to get a 4 space indent in vi without changing tab stops?
>> 
>>    I've tried re-mapping the tab key, but without any luck.

>What you want is a shift-width of 4.  (:set sw=4)

>Then, when entering your input, instead of hitting "tab", you hit
>Control-T (^T).

>No, I'm sorry, you can _not_ map the "tab" into ^T.  I've tried, and it 
>won't work.

Yes you can! You need a few extra Ctrl-Vs. Type in

:map! ^V^V^V^I ^V^T

and it works. What this monstrosity looks like in a .exrc, I've not tested.

Ian

kpicott@alias.UUCP (Socrates) (05/27/91)

In <1991May24.163037.24150@unipalm.uucp> ian@unipalm.uucp (Ian Phillipps) writes:
>>Then, when entering your input, instead of hitting "tab", you hit
>>Control-T (^T).
>>No, I'm sorry, you can _not_ map the "tab" into ^T.  I've tried, and it 
>>won't work.
>Yes you can! You need a few extra Ctrl-Vs. Type in
>:map! ^V^V^V^I ^V^T

Yes this works except that you lose the visual feedback of the line shifting
right when you insert a "tab" into the middle of a line.  I suppose you
could use >> for the beginning of a line but it's always annoying to have
what you see on the screen be different from what exists in reality.

asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) (05/29/91)

Written in article <1991May16.153123.260@dde.dk>
    by kbe@dde.dk (Kjeld Borch Egevang):

: hansm@cs.kun.nl (Hans Mulder) writes:
: 
: >   I've tried re-mapping the tab key, but without any luck.
: 
: It is possible. Type
: 
: :map! ^V^V<tab> xxxx
: 
: to <tab> to xxxx

Unfortunately, this does not work for ^T, which sure as heck would be
useful.  It complains, "missing rhs [right hand side]."
--
Alvin ===== asylvain@felix.UUCP ===== hplabs!felix!asylvain ===== 
"hplabs!felix!asylvain"@uunet.uu.net
(I always try to respond to mail, if possible.  If you don't hear back 
from me, try changing "hplabs" to "ccicpg," "spsd," or "lawnet.")
DISCLAIMER: It's all in fun, folks, no flames intended.  Any similarity
between my opinion and that of my employer is purely coincidental and
sufficient reason to change my opinion, although I'll still be right.

hansm@cs.kun.nl (Hans Mulder) (05/31/91)

In <163851@felix.UUCP> asylvain@felix.UUCP (Alvin "the Chipmunk" Sylvain) writes:
>Written in article <1991May16.153123.260@dde.dk>
>    by kbe@dde.dk (Kjeld Borch Egevang):
>: hansm@cs.kun.nl (Hans Mulder) writes:
>: >   I've tried re-mapping the tab key, but without any luck.

I didn't.  Jerry E. Dunmire said that.  I said something similar to Kjeld:

>: It is possible. Type
>: 
>: :map! ^V^V<tab> xxxx
>: 
>: to <tab> to xxxx

>Unfortunately, this does not work for ^T, which sure as heck would be
>useful.  It complains, "missing rhs [right hand side]."

It works for ^T, at least in vi version SVR3.1.  Your best bet is typing
three control-Vs in both relevant places:

:map! ^V^V^V<tab> ^V^V^V^T

This is echoed as

:map! ^V	 ^T

Remember to type a <space> after the <tab>.
You'll get the dreaded "missing rhs" message if you forget it.

And yes, this :map! is very useful when you :set shiftwidth to
something other than 8.

--
Have a nice day,

Hans Mulder	hansm@cs.kun.nl