meyering@cs.utexas.edu (Jim Meyering) (03/18/91)
A couple months ago several people posted command completion macros for VI. I grabbed one of them (sorry, I don't remember whose) and have used and refined it since. Here is what I use with an explanation of the contortions I had to go through to get it to work the way I wanted. One improvement (maybe the only one) over the initial version is that the partial word (to be completed) can be in the middle of a line. That makes it a little more useful. " " This is a word-completion macro. While in insert mode, type " a prefix (presumably enough to distinguish it from other words) " of the word you want and then type ^K. Uses marker q and named " buffer q. " " Explanation: first get out of insert mode and mark the end of the partial " word with a "."; back up to the beginning of the partial word, mark " its position, and insert "?\<"; then back up two spaces and delete " (into the q-buffer) everything up to the period -- this is the search " command. Execute q-buffer to perform the search. Yank the word it " found. Return to marked position and `Put' the completed word before " the mark (the mark points to the inter-char space just before the " period we inserted initially). Note: the completed word we've just " inserted may have included a lot of (or no) trailing white space. So " we insert a single space just after the last non-whitespace char of " the completed word, and "dw" deletes any whitespace there may have " been. Back up to the beginning of the completed word and insert an " `s'. Now, the cursor is on the just-inserted `s' and we delete (into " the q-buffer) everything up to but not including the period. The " q-buffer now contains an `s' followed by the completed word -- it is " a substitute command -- so we execute it, substituting the completed " word for the period and that leaves us in insert mode. " map! ^K ^V^[a.^V^[bmqi?\<^V^[2h"qdt.@q^V^Myw`qPbea ^V^[dwbis^V^["qdt.@q P.S. This macro has been filtered through "cat -v"; there were no literal carets in the original. I think I've seen a perl script to reverse the process. Jim Meyering meyering@cs.utexas.edu -- Jim Meyering meyering@cs.utexas.edu