Vi

Jeff Benjamin benji at hpfcdq.HP.COM
Thu Jun 8 06:42:35 AEST 1989


>    Can Vi reformat a paragraph, justifying it properly(left, right, or center-
> ed)?  I hate editing text when I have to rejustify the right margin by hand
> after deleting or adding to a line at the top of a paragraph.

If you answer the question literally, the answer is no: vi cannot
reformat a paragraph.

However, vi provides the capability to use another system program to
do the work for it, by using the ! command.  This command allows you
to pipe text from vi through a shell command.  The command to use in 
this case is adjust(1).  See the man page for complete usage.

The vi syntax you would use is "![count]<text_object><shell_command(s)>"
where count and text_object are some way of specifying what portion of
text is to be piped to shell_command(s).  For example, "2j" as a text_object
means, "this line and the following two lines," "$" means, "from here to
the end of the file," and "3}" means, "from here to the second following
end of paragraph."  Use vi movement commands to make your text_objects.

So the sequence you would use to adjust your paragraph:

{!}adjust [adjust_options]

This means:

{:		Move to the beginning of current paragraph (this is just in preparation
   		for the actual ! command).
!:		Get ready to use a shell interpreter.  Note that you will not see the
		"!" at the bottom of the screen until you have entered a text_object.
}:		From here to the end of the current paragraph (at this point, you
		see "!" on the bottom line of your screen).
adjust: pipe it all through adjust and replace what's currently there.

You can map this all to one keystroke in your .exrc (this maps it to 
control-A):

:map  {!}adjust [adjust_options]


The same capability can be used to run your file through sort, pr, or
whatever, without ever leaving vi.  Have fun.

-----
Jeff Benjamin                         {ucbvax,hplabs}!hpfcla!benji
Graphics Technology Division          benji%hpfcla at hplabs.HP.COM
Hewlett Packard Co.                   Fort Collins, Colorado	



More information about the Comp.unix.questions mailing list