[comp.unix.questions] vi

thoyt@ddn-wms.arpa (Thomas Hoyt) (06/07/89)

Folks:
   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 it is possible,
how can I do it?  Thanks much.
******
thoyt@ddn-wms.arpa      |  "Oh no...it's written in COBOL..."
Thomas Hoyt             |  "Government Computers for Government business..."
CRC Systems, Inc., Fairfax, VA -- 703-359-9400       |  "NO FUN ALLOWED..."
******

andyb@coat.com (Andy Behrens) (06/07/89)

In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
>   Can Vi reformat a paragraph, justifying it properly....

Vi can't do it by itself, but it can easily pipe the paragraph through
a formatting program.

If you put the following line in your .exrc file, you can type "V" when
the cursor is anywhere in a paragraph, and the entire paragraph will be
rejustified by being piped through "fmt".

	map V   0}!{fmt^M}

Note: the ^M above represents an actual carriage return.  You will need
to type "<ctrl-V> <return>" when editing the .exrc file.

--
Live justly, love gently, walk humbly.
					Andy Behrens
					andyb@coat.com

also:   andyb%coat@dartmouth.edu
also:   {harvard,rutgers,decvax}!dartvax!coat!andyb
RFD 1, Box 116, East Thetford, Vt. 05043		(802) 649-1258
Burlington Coat, PO Box 729, Lebanon, N.H. 03766	(603) 448-5000

hsiegel%jsbach.prime.com@relay.cs.ne (Howard Siegel 4-2390 x4064) (06/07/89)

Thomas Hoyt <cvbnet!decvax!ddn-wms.arpa!thoyt> asks:
>   Can Vi reformat a paragraph, justifying it properly(left, right, or
>centered)?  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.

"Vi" itself cannot, but in combination with (for instance) "fmt" it can.
Suppose that your paragraph spans lines 12-19 after your addition. Then
the ("ex") command
	:12,19!fmt
will justify your paragraph, and not allow any line to exceed 72
characters (the default for "fmt").  You can, of course, use other
"fmt" options to modify this behavior.

Howard Siegel			hsiegel@piano.prime.com
Prime/Computervision		hsiegel@primerd.prime.com
Bedford, Mass.			hsiegel%piano.prime.com@RELAY.CS.NET
(617) 275-1800 x4064		cvbnet!hsiegel@primerd.prime.com

friedl@vsi.COM (Stephen J. Friedl) (06/07/89)

In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
>   Can Vi reformat a paragraph, justifying it properly....

In article <13816@dartvax.Dartmouth.EDU>, andyb@coat.com (Andy Behrens) writes:
> If you put the following line in your .exrc file, you can type "V" when
> the cursor is anywhere in a paragraph, and the entire paragraph will be
> rejustified by being piped through "fmt".
> 
> 	map V   0}!{fmt^M}
> 
> Note: the ^M above represents an actual carriage return.  You will need
> to type "<ctrl-V> <return>" when editing the .exrc file.

Most Sys V machines don't have fmt, so you can do the same thing
with a shell script in front of nroff:

	# fmt.sh
	{
		cat <<-!
			.hy 0	\' hyphenation off
			.pl 1	\' no pagefill lines
			.na	\' don't adjust right margin (leave ragged)
		!
		exec cat $@
	} | nroff

If you don't have nroff then I'm sure somebody will come up with a
sed script for it :-).

     Steve

-- 
Stephen J. Friedl / V-Systems, Inc. / Santa Ana, CA / +1 714 545 6442 
3B2-kind-of-guy   / friedl@vsi.com  / {attmail, uunet, etc}!vsi!friedl

"Friends don't let friends run Xenix" - me

vwa0201@marst2.UUCP (Larry Baca) (06/07/89)

In article <19890@adm.BRL.MIL>, thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
> Folks:
>    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 it is possible,
> how can I do it?  Thanks much.

/\/\/\/\/\/\/\/\/\/\/\/\
Maybe this will help....
/\/\/\/\/\/\/\/\/\/\/\/\

Printed 5/25/89                                                 1
para(1)             UNIX Programmer's Manual		  para(1)



NNNAAAMMMEEE
     para  - A New vi FEATURE

SSSYYYNNNTTTAAAXXX
     None

FFFUUUNNNCCCTTTIIIOOONNNAAALLL DDDEEESSSCCCRRRIIIPPPTTTIIIOOONNN
     For those users who use the vi editor to do correspondence,
     rather than Qoffice, there is a new feature available now
     for the vi editor which will allow you to insert or delete
     text and have the system re-wrap the text for you.  No
     longer will you have to manually edit each line affected
     when a change is made.

     The program, obtained from Gould as public-domain, is called
     pppaaarrraaa and requires only one addition to each user's library.

     A ...eeexxxrrrccc file must be established (if not already there) in
     each user's home directory that wishes to use para, which
     contains information similar to the following:

       set redraw ai tabstop=0 ignorecase
       map #1 !}para -l79
       map #2 !}para -l75 -i5
       map #3 !}para -l70 -i10
       map #4 !}para -l65 -i15
       map ^[n	!}para -l79 -i5
       map ^[m	!}para -l75 -i10
       map ^[v	!}para -l70 -i5
       map ^[t	!}para -l65 -i10

     The ###111,,, ###222,,, ###333,,, ###444 parameters relate to the PF1-PF4 keys on
     the 10-key pad on the Visual keyboards; the ^^^[[[nnn,,, ^^^[[[mmm,,, ^^^[[[vvv
     aaannnddd ^^^[[[ttt refer to pressing the <<<EEESSSCCC>>> key followed by nnn,,, mmm,,, vvv
     ooorrr ttt respectively.

     The ---lll parameter followed by a number equates to the number
     of characters per line desired; the ---iii parameter equates to
     the number of characters to be indented.

     The rewrap function occurs from the point at which the cur-
     sor is positioned when the appropriate key is pressed
     through the remainder of the paragraph.  Therefore, you may
     want to set up function keys that will indent the first line
     of the paragraph five spaces more than the remainder of the
     paragraph.  For example, positioning the cursor on the first
     line of a pargraph and pressing the #1 (PF3) key would
     indent that line 10 spaces; moving the cursor to the next
     line and pressing the <ESC> v key would indent the remainder
     of the paragraph only 5 spaces.

     You may choose which keys to set up to perform various

Printed 5/25/89                                                 1
para(1)             UNIX Programmer's Manual		  para(1)

     rewapping of lines; be careful, however, that you don't make
     it too complicated for the user to remember!

Printed 5/25/89                                                 2
-- 
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
LARRY BACA, SYSTEM ADMINISTRATOR, [4m(MARST1 MARST2)[0m   marst2!lbaca
DAASO-VWA AIS, DEFENSE AUTOMATIC ADDRESSING OFFICE, WESTERN DIVISION
DDTC TRACY, TRACY CA. 95376-5057  AUTOVON 462-9391  COMERCIAL 832-9391

tanya@adds.newyork.NCR.COM (Tanya Katz) (06/07/89)

In article  <13816@dartvax.Dartmouth.EDU> Andy Behrens write:

|In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
|>   Can Vi reformat a paragraph, justifying it properly....
|
|If you put the following line in your .exrc file, you can type "V" when
|the cursor is anywhere in a paragraph, and the entire paragraph will be
|rejustified by being piped through "fmt".
|
|	map V   0}!{fmt^M}
|
Well I liked this idea, but I usually don't like to overwrite the
vi file, and wanted to just produce an nroff file from within vi.

SO... here are my macros:

	map M :!less %.out^M^M
	map V :!nroff %>%.out^M^MM^M

This brought up an interesting question of file name manipulation
from within vi, and passed to the shell.

% = the current file name, but how can I truncate it to just
the prefix if it is something like:
	name.1 
to get:
	name
or better yet `basename name`??


	Tanya

------------------------------------------------------------------------------
    ###   ######  ######   #####       Tanya Katz       (516) 231-5400 x430 
   #   #  #     # #     # #           
  #     # #     # #     #  #####       ...uunet!ncrlnk!adds!tanya      
  ####### #     # #     #       #      tanya.katz@adds.newyork.ncr.com
  #     # ######  ######   #####       

  Applied Digital Data Systems, Inc.   100 Marcus Blvd., Hauppauge, NY 11788 
------------------------------------------------------------------------------

benji@hpfcdq.HP.COM (Jeff Benjamin) (06/08/89)

>    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@hplabs.HP.COM
Hewlett Packard Co.                   Fort Collins, Colorado	

jbm@uncle.UUCP (John B. Milton) (06/09/89)

In article <1103@adds.newyork.NCR.COM> tanya@adds.newyork.NCR.COM (Tanya Katz) writes:
>In article  <13816@dartvax.Dartmouth.EDU> Andy Behrens write:
>|In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
>|>   Can Vi reformat a paragraph, justifying it properly....
>|
>|If you put the following line in your .exrc file, you can type "V" when
>|the cursor is anywhere in a paragraph, and the entire paragraph will be
>|rejustified by being piped through "fmt".
>|
>|	map V   0}!{fmt^M}
>|
>Well I liked this idea, but I usually don't like to overwrite the
>vi file, and wanted to just produce an nroff file from within vi.
 >
 >SO... here are my macros:
 >
 >	map M :!less %.out^M^M
 >	map V :!nroff %>%.out^M^MM^M
 >
 >This brought up an interesting question of file name manipulation
 >from within vi, and passed to the shell.
 >
 >% = the current file name, but how can I truncate it to just
 >the prefix if it is something like:
 >	name.1 
 >to get:
 >	name
 >or better yet `basename name`??

The prpgram basename can take a second argument, which is a string to remove
from the end of the path after the leading directory has ben stripped off.
So `basename /a/b/c/file.out .out` will be "file", and
`dirname /a/b/c/file.out`/`basename /a/b/c/file.out .out` will be "/a/b/c/file"

John
-- 
John Bly Milton IV, jbm@uncle.UUCP, n8emr!uncle!jbm@osu-cis.cis.ohio-state.edu
(614) h:294-4823, w:466-9324; N8KSN, AMPR: 44.70.0.52; Don't FLAME, inform!

hopewell@cbnewsm.ATT.COM (alan.j.greenberger) (06/10/89)

In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
>Folks:
>   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 it is possible,
>how can I do it?  Thanks much.
>******
>thoyt@ddn-wms.arpa      |  "Oh no...it's written in COBOL..."
>Thomas Hoyt             |  "Government Computers for Government business..."
>CRC Systems, Inc., Fairfax, VA -- 703-359-9400       |  "NO FUN ALLOWED..."
>******

	I use the statement "map ^A !}fill" in .exrc to pipe from the current
line to the end of paragraph out to the following filter:

/* @(#)fill.c	1.3 */
/* This program is a filter for text.  It fills whole words up to LINELEN
columns */

#include	<stdio.h>

#define		LINELEN			79
#define		NOT_SENTENCE_END	0
#define		SENTENCE_END		1
#define		FIRST_WORD		0
#define		NOT_FIRST_WORD		1
#define		SPACES_IN_TAB		8

main()
{
	char	firstchar;
	int	linelen;
	int	lastword = NOT_SENTENCE_END;
	char	word[2*LINELEN];
	int	wordlen;
	int	wordnum = FIRST_WORD;

	if((firstchar = getchar()) != ' ')
	{	/* special case to retain a leading TAB */
		if(firstchar == '\t')
		{
			linelen = SPACES_IN_TAB;
			putchar(firstchar);
		}
		else
		{
			linelen = 1;
			ungetc(firstchar,stdin);
		}
	}

	while(fscanf(stdin,"%s",word) == 1)
	{	/* got a new word from standard input */
		wordlen = strlen(word);
		if((linelen + wordlen) < LINELEN)
		{	/* append to this line */
			if(lastword == NOT_SENTENCE_END || (word[0] < 65 || word[0] > 90 /* not capital letter */))
			{
				if(wordnum == NOT_FIRST_WORD)
				{
					printf(" %s",word);
				}
				else
				{
					printf("%s",word);
				}
				linelen += wordlen + 1;
			}
			else
			{	/* SENTENCE_END */
				if((linelen + wordlen + 1) < LINELEN)
				{
					printf("  %s",word);
					linelen += wordlen + 2;
				}
				else
				{	/* put on next line */
					printf("\n%s",word);
					linelen = wordlen;
				}
			}
		}
		else
		{	/* put on next line */
			printf("\n%s",word);
			linelen = wordlen;
		}
		if(word[wordlen - 1] == '.' || word[wordlen - 1] == '?' || word[wordlen - 1] == '!')
		{	/* looks like end of sentence */
			if((strchr(word,'(') != NULL && strchr(word,')') == NULL) ||
			(strchr(word,'[') != NULL && strchr(word,']') == NULL))
			{	/* inside parenthesis */
				lastword = NOT_SENTENCE_END;
			}
			else
			{
				lastword = SENTENCE_END;
			}
		}
		else if(word[wordlen - 1] == ']' || word[wordlen - 1] == ')')
		{
			if(strchr(word,'.') != NULL)
			{
				lastword = SENTENCE_END;
			}
			else
			{
				lastword = NOT_SENTENCE_END;
			}
		}
		else
		{
			lastword = NOT_SENTENCE_END;
		}
		wordnum = NOT_FIRST_WORD;
	}
	putchar('\n');
}

leo@philmds.UUCP (Leo de Wit) (06/12/89)

In article <1128@vsi.COM> friedl@vsi.COM (Stephen J. Friedl) writes:
|In article <19890@adm.BRL.MIL> thoyt@ddn-wms.arpa (Thomas Hoyt) writes:
|>   Can Vi reformat a paragraph, justifying it properly....
|
|If you don't have nroff then I'm sure somebody will come up with a
|sed script for it :-).

Ok Stephen, I could not let you be called a liar, so here's a sed
script that does basically what 'fmt' does. It's called 4mt, and like
fmt it expands tab to spaces, word wraps at 72 columns, and left aligns
to the first line's indentation.

If your system doesn't support #! <interpreter> it is easy to wrap a
shell script around it. A small explanation is perhaps welcome:
1) the first part /     /{ ... } expands tabs to spaces. Newline is
used as a marker.
2) the second part /..../!{ ... } joins lines into the pattern space
until it is at least 72 characters long.
3) A newline is put into the pattern space after 72 characters, serving
as a marker.
4) If this action splits up the first word of the line, this word is
obviously too long. The branch 2long is taken (the line will contain the
complete word).
5) If not, the newline is placed before the word it is possibly dividing.
6) After either 4) or 5), the initial space part is copied after the
newline, so that subsequent lines will have the same initial spacing.
7) The first line of the pattern space is printed and the next cycle
started.

    Leo.

P.S. It was kind of fun to experience that sed was capable of doing this.

---------->8 cut here 8<-------
#! /bin/sed -f

: tab1
/	/{
	s/^/\
/
	: tab2
	s/\(\n\)	/        \1/
	s/\(\n\)\([^	]\)	/\2       \1/
	s/\(\n\)\([^	][^	]\)	/\2      \1/
	s/\(\n\)\([^	][^	][^	]\)	/\2     \1/
	s/\(\n\)\([^	][^	][^	][^	]\)	/\2    \1/
	s/\(\n\)\([^	][^	][^	][^	][^	]\)	/\2   \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	]\)	/\2  \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	][^	]\)	/\2 \1/
	s/\(\n\)\([^	][^	][^	][^	][^	][^	][^	][^	]\)/\2\1/
	/	/b tab2
	s/\n//
}
/......................................................................../!{
	$p
	$d
	N
	s/\([^ 	]\)\(\n\)/\1 \2/
	s/\n[ 	]*//
	b tab1
}
s/\(........................................................................\)/\1\
/
/^ *[^ \n]*\n/b 2long
s/  *\([^ \n]*\)\(\n\)/\2\1/
: cpspac
s/^\( *\)\([^\n]*\n\)/\1\2\1/
P
D
: 2long
s/\(\n\)\([^ ]*\) */\2\1/
b cpspac
---------->8 cut here 8<-------

mikey@ontek.UUCP (Mike Lee) (06/14/89)

I got tired of ragged edges in my  paragraphs,  so  here  is  yet
another  shell script to invoke nroff.  It done in the C-shell. A
possible portability problem is that it uses "shift_lines"  which
I know is a  given  with  SunOS 4.0, but other OS's may not be so
generous.  If I get some complaints, I could probably make a  few
lines  of   C  code  do  the  same  thing.  These paragraphs were
adjusted using this script.

To use the script, go !}<name of script> N, where you,  YES  YOU,
get to choose the name of the script.  N is the number of columns
to shift the paragraph to the right.  The default  is   currently
two   columns.  If  the  paragraph  is already indented to column
two, the script won't keep on indenting it.  I suggest you change
the  default  to whatever suits you best.  Pass 0 to jam stuff up
against the left column.

NOTE: I know little or nothing about nroff.  Some other kind soul
may  see  fit  to  add  some  more echo statements to use some of
nroff's neater feature eaters.


Here goes.....................................................
# csh script to call up nroff on a paragraph
#
set temp_file = /tmp/foom.$$

# change default indentation to your liking
set columns = 2
if ( $1 != "" ) set columns = $1

echo ".hy 0" >! $temp_file
echo ".pl 1" >> $temp_file

cat | shift_lines -$columns >> $temp_file
nroff $temp_file | shift_lines $columns

rm $temp_file
.............................................................


Mike Lee
mikey@ontek.uucp
...uunet!ontek!mikey
1-714-768-0301

anthis@sleepy.UUCP (mike anthis) (10/04/89)

A coworker suggested I beg for this flame, so here goes. . .

I would like some suggestions about how to use vi with a 1200 baud
vt-100 emulator (which runs on an IBM PC).

I know that there are quite a number of options for dealing with 
slow and/or noisy lines.  Would anyone like to expound on what
some useful settings are?

The emulator I use came with a Hayes Smartcom modem card.  The 
software that came with the card does the vt-100 boogie well 
enough to get along with vi.

Incidentally, when I am at work and using my Sun workstation,
I would rather not be penalized for the choices that help me
work at home.  That is, on the fast tube I would like fast vi,
and on the slow tube I would like smart vi.

Please E-mail responses;  I'll gladly summarize.  If anyone
want's the summary posted, tell me so via Email.  Same goes
for E-mail copies of the summary.

P.S.  I'd welcome suggestions about non-vi work using my 
1200 baud tube.

mike

root@kunivv1.sci.kun.nl (Privileged Account) (05/31/90)

In article <315@nwnexus.WA.COM> michael@nwnexus.UUCP (Michael Shemet) writes:
>Anyone know what the ex mode command for inserting the text from buffer 'a'
>in vi is? I can do this in visual mode IE 'put' the text from buffer 'a' with
>"ap ... I can also yank the text in ex mode with:
>
>:1,5y a

The command is

:put a

You can abbreviate it to

:pu a

but not to :p a, since :p abbreviates :print.

--
Hans Mulder	hansm@cs.kun.nl