Gundala Viswanath wrote:
Dear Tony and all,


My Linux box indeed hasn't got "cb", but it has got "indent" which is
meant to beautify and reformat C code, e.g. by adding and removing
spaces, tabs, linefeeds and blank lines, according to various parameters
on a command-line and/or in a config file; GNU indent supports
"original", "K&R" and "GNU" styles in addition to custom style. See
"info indent" (where installed) for details.


Thanks all, glad to know gnu indent
is already there. It is the closest thing to perltidy.

What is your favorite options for "indent" Tony.
Second thing is that whenever I do:

:'<,'>!indent -kr

of a particular block. After the formatting,
it pushes the block to the first column, in the vi editor.

Is there a way to maintain the current position
of the block with indent?



Actually, I have no favourite "indent" options, because I don't use it: I'm not a C programmer. What I write nowadays is mostly plain text, HTML, and Vim script, with a few bash scripts thrown in for good measure. (I cut my milk teeth on BASIC, FORTRAN and COBOL in the late sixties, and a little later on various assembly languages.)

indent receives your highlighted block as a (temporary) file, so it acts on it as if it were a full-fledged source module and not part of one. I may have missed something among its plethora of options, but I see nothing about indenting top-level code.

You may try gv to reestablish the visual highlight, followed by [count]> to reindent by [count] shiftwidths; however, IIUC,

a) your end-of-line comments within the block will move with the rest (away from column 33);
b) your boxed and three-piece comments will move right with the rest;
c) your #if #ifdef #define etc. within the block may move away from column 1.

(b) and (c) may be cured by identifying each affected block of lines, and unindenting them back with {Visual}[count]< , or using [count]> on smaller blocks of code excluding them. Curing (a) is trickier (it can be done using blockwise delete: cursor at one corner of rectangular block to delete, Ctrl-V, cursor at opposite corner, and finally d or <Del>), but in K&R style it might be less important.

You might prefer to process the whole file, using % or 1,$ as the range. That way you will have consistent formatting all through, your end-of-line comments will be aligned, your block comments won't move, and your compiler-directing statements will have their # in column 1 even if the rest is indented according to the depth of conditionals (e.g., using -ppi1 to mimic Bram's style in the C source for Vim).


Best regards,
Tony.

Reply via email to