[comp.editors] simple tricks in Vi

mad@math.keio.ac.jp (MAEDA Atusi) (06/28/90)

(I am posting this for Tanaka.)

-----------------------------------------------------------

Dear People:

Encouraged by the positive responses to my last article, I
decided to describe some more simple Vi tricks which I have
found useful.


(1) === moving text between files ===

If you open two files simultaneously as "vi file1 file2",
you can use a named buffer to move text between those files.
But this is not how I use Vi; I open one file at a time.
So I have these lines in the ".exrc" file:
     ab wt w! /tmp/temp.vi.tanakat
     ab rt r  /tmp/temp.vi.tanakat
I save lines in one file as:
     :.,.+3 wt     (save next 4 lines)
     :.,'a  wt     (save up to mark A)
and restore these lines in another file by ":rt".


(2) === commenting out 1 Lisp expression ===

(putting semicolons at the beginning of the lines)
Place this line in the ".exrc" file:
     map q mx%my:'x,'ys/^/;/^M`xl
('^M' is a single newline character, not two characters '^'
and 'M'.)


(3) === reformatting paragraphs within Vi ===

Place this line in the ".exrc" file:
     map q !}fmt -61^M
(It is better to map this to a function key if your keyboard
has one.)


I wonder if there are other simple but useful tricks in Vi.
(Please send responses, if any, to my BITNET address below.)

(Mr.) TANAKA Tomoyuki (Tanaka is my family name.)

;;; IBM Research, Tokyo Research Laboratory
;;; 5-19, Sanbancho, Chiyoda-ku, Tokyo 102, Japan
;;;
;;; BITNET:  tanakat@jpntscvm.bitnet
;;; JUNET:   tanakat@trl.ibm.co.jp

blair@entec.Wichita.NCR.COM (Brian Lair) (07/09/90)

In article <MAD.90Jun28203733@cabbage.math.keio.ac.jp>, mad@math.keio.ac.jp (MAEDA Atusi) writes:
> (3) === reformatting paragraphs within Vi ===
>      map q !}fmt -61^M

Here's a naive-user question:
When I try to use this, I get the message:
	sh: fmt: not found
Is this some special program not normally supplied with Unix?
Thanks in advance for any assistance.
-- 
+---- Brian R. Lair  ------------------------------------------------------+
|     NCR Corporation, PPD Wichita, VLSI/CAE Operations                    |
+---- <Brian.Lair@Wichita.NCR.COM> or <uunet!ncrlnk!ncrwic!brian.lair> ----+

lee@sq.sq.com (Liam R. E. Quin) (07/11/90)

I mailed this, and then decided to post it, too...

blair@entec.Wichita.NCR.COM (Brian Lair) writes:
>	sh: fmt: not found

fmt is on V7 Unix and then generally only on BSD-derived Unix...
so it's not on System V, for example...

Here are a couple of simple (completely public domain...) sketches for
replacements to the fmt command.  They both work, but have less functionality
than some newer (e.g. SunOS) versions of fmt.

You can simulate it easily, though.  Here are two approaches -- one
with nroff, and one with awk.

--- cut here for the nroff version
:
(
cat << 'xxx'
.pl 1 \" page length of 1 so we don't get page breaks
.ll 61 \" set line length to max number of characters wanted per line
.nh \" disable hyphenation
.ad l \" don't pad lines with spaces to make an even right margin
. \" The next three lines turn off nroff's special processing of lines
. \" starting with a . or ' and also disable the in-line escape character,
. \" normally the backslash (\) so that the input text is passed through
. \" with no changes except line filling...
.eo \" disable backslashes (and comments) from here on...
.c2 ^A\" change the ^A to be a real control-A and delete this comment
.cc ^B\" and a control-B here... and delete this comment (from the \)
xxx
cat ${@+"$@"}
) | nroff -
--- end here

If not, here's an awk script off the top of my head....
You could put it in a shell script.... and make the 61 a parameter.
I have tried this, and it works.

cut here for the awk version. You will need to put a wrapper shell-script
around this on System V.
-- -- -- snip -- -- --
#! /bin/awk -f
BEGIN {
    MaxLength = 61
}

{
    for (i = 1; i <= NF; i++) {
	l = length($i)
	if (LengthSoFar + l + SpaceWidth > MaxLength) {
	    printf "%s\n", LineSoFar
	    LineSoFar = ""; Space = ""; SpaceWidth = 0
	    LengthSoFar = 0
	}
	LineSoFar = LineSoFar Space $i
	LengthSoFar += SpaceWidth + l
	Space = " "; SpaceWidth = 1
    }
}

(NF == 0) {
    # blank line -- end paragraph
    if (LengthSoFar) {
	printf "%s\n", LineSoFar
	LineSoFar = ""; Space = ""; SpaceWidth = 0
	LengthSoFar = 0
    }
    printf "\n" # pass the blank line through to the output
}

END {
    printf "%s\n", LineSoFar
}
-- cut here.... ---

Again, one could do this in perl, snobol or C (but I won't).  Nroff and
awk are the most wide-spread of the Unix text processing tools, so you
are most likely to have them available.

Lee
-- 
Liam R. E. Quin,  lee@sq.com, {utai,utzoo}!sq!lee,  SoftQuad Inc., Toronto
``Sun. n.s.,  Any thing eminently splendid'' [Johnson's dictionary (1837)]

roise@sumax.UUCP (Linda Roise) (07/15/90)

In article <579@entec.Wichita.NCR.COM >  blair@entec.Wichita.NCR.COM
(Brian Lair) writes:

 > In article <MAD.90Jun28203733@cabbage.math.keio.ac.jp
 > mad@math.keio.ac.jp (MAEDA Atusi) writes:
 >> * (3) === reformatting paragraphs within Vi ===
 >> *      map q !}fmt -61^M
 >> 
 >> Here's a naive-user question:
 >> When I try to use this, I get the message:
 >> 	sh: fmt: not found
 >> Is this some special program not normally supplied with Unix?
 >> Thanks in advance for any assistance.

Some systems substitute the word [adjust] for [fmt].  Try:

    map q !}adjust -61^M


-- 
Linda L. Roise, M.A.   :uucp    : {uw-beaver,uunet!gtenmc!dataio}!sumax!roise
Dir. Addiction Studies :Internet: roise@milton.u.washington.edu
Seattle University     :BITnet  : roise@max.u.washington.edu
Seattle, WA 98122______:FreeNet_:_aj023@cleveland.freenet.edu__(206)296-5351_