Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!know!zaphod.mps.ohio-state.edu!usc!apple!snorkelwacker!bloom-beacon!eru!luth!sunic!mcsun!ukc!stl!jrd From: jrd@stl.stc.co.uk (J.R.Davis) Newsgroups: comp.editors Subject: Some VI macros and ideas Message-ID: <3309@stl.stc.co.uk> Date: 15 Aug 90 07:53:47 GMT Sender: news@stl.stc.co.uk Reply-To: jrd@stl.stc.co.uk (J.R.Davis) Organization: STC Technology Limited, London Road, Harlow, Essex, UK Lines: 124 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 )