tabs in vi

Norman Joseph norm at oglvee.UUCP
Thu Aug 24 23:05:37 AEST 1989


>From article <15184 at dartvax.Dartmouth.EDU>, by andyb at coat.com (Andy Behrens):
>>In article <36790001 at hpindwa.HP.COM> Shaun Gordon writes:
>> Is there a way to make it so that when ever I hit the TAB key
>> in 'vi' that I get 4 spaces rather than an actual tab?
> 
> What you want is
> 
> 	:set shiftwidth=4	(:set sw=4), possibly combined with
> 	:set autoindent		(:set ai)
> 
> Now, DON'T use the tab key at the left margin; use ^T instead.  Each ^T
> will move 4 positions to the right (inserting combinations of tabs and
> spaces to get to the correct column).  Each ^D at the left margin will
> move 4 positions to the left.

This is -almost- right.  As Andy says, ^T will insert enough -tabs- and
spaces to get you to the next shiftwidth.  It looks at the setting of
``tabstops'' to see how many tabs it needs to use.  If tabstops=4 and
shiftwidth=4 then it will insert one tab to move one shiftwidth.  If
shiftwidth is 6, it will use one tab and two spaces to get to the next
shiftwidth column.

The trick here is to set tabstops to a number much -larger- than shiftwidth.
For example, I set my tabstops to 78.  What this means is that when you
hit ^T for the next shiftwidth position, vi sees that inserting a tab will
move you 78 columns to the right, so it has no choice but to use -real-
spaces to move you shiftwidth columns.

This has worked for me with much success.  There -is- a single drawback,
though.  If you edit a file that has -real- tabs in it it will look
-awful- until you either 1) set tabstops=4 if you don't want to remove
those tabs, or 2) do one of these: ``:%s/<tab>/<4 spaces>/g''.

As another poster pointed out, to make life easier, place all these
``set'' commands in vi's startup file, .exrc, in your home directory.
For example, mine looks like this:

        % cat ~/.exrc
        set ai
        set nomesg
        set report=1
        set shell=/bin/csh
        set shiftwidth=4
        set showmatch
        set tabstop=78
        set terse
        set wrapmargin=1
	%
-- 
Norm Joseph - Oglevee Computer System, Inc.
  UUCP: ...!{pitt,cgh}!amanue!oglvee!norm
    /* you are not expected to understand this */



More information about the Comp.unix.questions mailing list