Click to See Complete Forum and Search --> : reformatting src file ( reindenting )


siqe
07-17-2003, 07:54 PM
I have alot of code I wrote with emacs and I'm switching over to vi. The problem is, emacs only indents 3 spaces or something, whilst vi indents a tab. So I'm going through here manually in vi and correcting it, but its taking me forever, and if i ever put a block of code inside a bracket set all of the lines need to be tabbed over one.

In emacs you can highlight, right click and select indent and it will reformat the highlighted lines. Is there some way to do this in vi, or with another tool?

mindcooler
07-17-2003, 08:36 PM
No matter what editor you use, using TAB for indentation is a bad thing<tm>. Keep the spaces

siqe
07-17-2003, 09:28 PM
Really. I thought tabs were better becuase you can set the number of spaces you want a tab to be in your own editor.

baldguy
07-17-2003, 10:10 PM
do you mean something like this?
%s/^ /<tab>/
that is 3 spaces after the carat and <tab> is an actual tab or control i

mindcooler
07-17-2003, 10:26 PM
Originally posted by siqe
Really. I thought tabs were better becuase you can set the number of spaces you want a tab to be in your own editor.

Then you must be one of those who never has to move code between different environments/editors or post code. Keep the spaces, anyone with experience will tell you that.

bwkaz
07-17-2003, 10:53 PM
mindcooler -- and why are tabs not the same across editors now? I post code here all the time with tabs in it (and just about as much with spaces, but only because the tab character moves input focus in Mozilla and I'm typing, not pasting)...

Not that it even matters...

In vi (actually, this might be a vim-only thing, but since you're using Linux, you probably have vim anyway, because vi is not open-source, because ex is not open-source), you can get into visual mode ('v' key), highlight the region you want to reindent, and hit the '=' key to reindent it. It does the reindentation based on the "sw" and "ts" settings -- "sw" or "shiftwidth" controls how many characters each additional indentation is, and "ts" or "tabstop" controls how many spaces there are per tab. So if you have sw set to 8 and ts set to 4, vim will indent each level with two tab characters. If sw and ts are both set to 4 or both set to 8, then it'll indent one tab character. If sw is set to 4 and ts is set to 8, then the first level will be 4 space characters, the second will be a tab, the third will be a tab then 4 spaces, the fourth will be two tabs, etc., etc. Not a great setup, but whatever.

What I don't know is which indentation style it uses. It won't move anything to another line (so if you use the 1TBS, it won't force you to use Java braces or something like that), I just don't know where it'll put each line.

siqe
07-18-2003, 10:37 PM
Hey bwkaz, thanks alot. You saved me a wad of time. I couldn't do the v highlight with mouse thing, but if i did vvGV, it would select all and then = would work.

bwkaz
07-19-2003, 08:53 AM
Oh, yeah, should've explained that. Move the cursor to the beginning of what you want to highlight, hit 'v', and then move the cursor more (with the arrows or h, j, k, l keys) to do the selection.

You could also do selection with the mouse if you used gvim rather than just plain vim (or vi).

But you got it anyway, so whatever. :)