[comp.editors] Some VI macros and ideas

jrd@stl.stc.co.uk (J.R.Davis) (08/15/90)

A few macros and ideas for vi hackers:
(all on a Sun (4.0.3), don't know how portable they are...)

** When you set up some clever mapping, have it tell the user what it
has done by, e.g.

    map q :set list^M:"--- in list mode ---"^M

will display "--- in list mode ---" on the bottom (command) line.

** Since it is simpe to have a macro remapping itself, it is possible
to set up keys which 'toggle' operations - for example, to toggle
line numbering, define

    map @NU@ :set nu^M:map # @NONU@^M
    map @NONU@ :set nonu^M:map # @NU@^M
    map # @NU@

Now pressing `#' will switch line numbering on or off.
I use similar code to toggle autoindent, list mode and tabs (4 or 8 spaces).

** Similarly, you are not limited to just two modes:

    map @C@ :so ~/.exrc.c^M:map g @TEXT@^M:"--- C mode ---"^M
    map @TEXT@ :so ~/.exrc.text^M:map g @LISP@^M:"--- Text mode ---"^M
    map @LISP@ :so ~/.exrc.lisp^M:map g @LISP@^M:"--- Lisp mode ---"^M
    map g @TEXT@

Now, vi starts in no particular mode, but hitting 'g' repeatedly switches
through the different customised environments.

** A mapping to underline a line with -'s:

    map _ "zyy"zp^i^M^[<<<<<<<<<<<<:s/./-/g^M-J

This causes the screen to flash, due to the `p', I can't see any
way round it but it still works. Any ideas out there?
Without the "z's, vi complains "Cannot put inside global/macro".
This one could probably be improved a lot, but I can't figure it...

** One for Suns only, I think:

    map #2 :r /tmp/winselection^Mdd:w /dev/null^M^]

(#2..#9 map the F2..F9 keys on a sun - your keyboard may be different)
In a shelltool, highlight a word with the left & centre mouse buttons,
then hit F2; this is the same as typing ":ta word", but a lot quicker.
DO NOT DO THIS IF YOU WANT TO SAVE THE FILE FIRST - it doesn't ask/let you.

** A set I use a lot:

    map #3 my
    map #4 "yy'y
    map #5 "yd'y
    map #6 "yp
    map #7 'y
    map #8 :'y,.w! ~/.vi.buffer^M
    map #9 :r ~/.vi.buffer^M

use F3 to mark a line, then
F4 to yank up to that line;
F5 to delete to the line;
F6 to put pack the yanked or deleted text (at current position);
F7 to go back to the marked line;
F8 to write from the current line to the marked line
    to a temporary file;
F9 to read from the temporary file written with F8 into
    the current position.

F8 and F9 are especially useful for transferring text between
two files - for example

    vi file1
    go to start of block to move
    F3
    go to end of block to move
    F8
    F5
    :wq
    vi file2
    go to place you want text from file 1
    F9

Voilla!

** for spell checking a document:
(uses a program `vspell', which I list below)

    map @V1@ 1G!Gvspell^M:map V @V2@^M
    map @V2@ 1Gd/---- spell check complete ---/+0^M:map V @V1@^M
    map V @V1@

Use `V' ("verify spelling") to spell check a file - puts a list of the
spelling errors at the top of the file. Use `V' again to get rid of them.

Here's the `vspell' program - a little shell script:

    #!/bin/sh
    # vspell - spell checker to invoke from vi
    #
    # john davis

    tmp1=/tmp/$program.1.$$

    trap "" 1 2 3 15

    echo -------------- spelling check ---------------
    tee $tmp1 | spell -b
    echo ----------- spell check complete ------------
    cat $tmp1

    rm -f $tmp1
    # end of vspell ------------------------------------

Note that it checks the file being edited, you don't need to save first.
The geniuses among you can probably improve this tenfold...

** that's all, folks... if anyone can see obvious (or even obscure)
improvements, please mail me...

                              -jrd
--
John Davis  ( J.R.Davis@stl.stc.co.uk +44-279-429531 Ext 3618 )

yuf@sequent.UUCP (Kyle Grieser) (08/16/90)

In article <3309@stl.stc.co.uk> jrd@stl.stc.co.uk (J.R.Davis) writes:
>F8 and F9 are especially useful for transferring text between
>two files - for example
>
>    vi file1
>    go to start of block to move
>    F3
>    go to end of block to move
>    F8
>    F5
>    :wq
>    vi file2
>    go to place you want text from file 1
>    F9
>
>Voilla!

This is much easier done within the same vi session as follows:

	vi file1
	go to begining of block
	mm		/* mark place */
	go to end of block
	"ay'm		/* yank into buffer "a" */
	:n file2
	go to place you want text from file 1
	"ap		/* put contents of "a" buffer */*/

Macros can be used fo the marking, yanking and putting, but I just
wanted to point out that one doesn't need the intermediate temp
file and two vi sessions as vi retains it's named buffers between
files.  This is also nice because one can now do the following:

	:w		/* write the changes to file2 */
	^^		/* control-^ to switch back to file1 */
	move to another block to move
	mm
	go to end of block
	yank again
	^^ again	/* switch back to file2 */
	place new stuff
	...
	...
	...

This can be repeated for quite a while when taking numerous blocks
from one file to another.

----
Kyle Grieser				...!sequent!yuf
Sequent Computer Systems Inc.		sequent!yuf@uunet.uu.net
15450 S.W. Koll Parkway
Beaverton, OR 97006			(503) 626-5700

peter@ficc.ferranti.com (Peter da Silva) (08/16/90)

>     map _ "zyy"zp^i^M^[<<<<<<<<<<<<:s/./-/g^M-J

> This causes the screen to flash, due to the `p', I can't see any
> way round it but it still works. Any ideas out there?

	map _ :.copy.^V^V^V|s/[^ 	]/-/g^M
-- 
Peter da Silva.   `-_-'
+1 713 274 5180.   'U`
peter@ferranti.com (currently not working)
peter@hackercorp.com