Path: utzoo!attcan!uunet!lll-winken!csd4.csd.uwm.edu!mailrus!purdue!tut.cis.ohio-state.edu!pt.cs.cmu.edu!cadre!pitt!amanue!oglvee!norm From: norm@oglvee.UUCP (Norman Joseph) Newsgroups: comp.unix.questions Subject: Re: tabs in vi Message-ID: <497@oglvee.UUCP> Date: 24 Aug 89 13:05:37 GMT References: <15184@dartvax.Dartmouth.EDU> Organization: Oglevee Computer Systems, Connellsville, Pa Lines: 52 From article <15184@dartvax.Dartmouth.EDU>, by andyb@coat.com (Andy Behrens): >>In article <36790001@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//<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 */