VI(1) vs. the world

Gregg Wonderly gregg%a.cs.okstate.edu at RELAY.CS.NET
Fri Oct 3 12:58:02 AEST 1986


#define	CASE3		3
#define	CASE2		2
#define	CASE1		1
	With all this fuss about editors, I couldn't resist to get in on the
discussion.  Most of the complaints seem to center around whether or not
VI(1) is mnemonic.  I for one believe that this is not that important for
those who use an editor frequently.  Someone else commented to this effect.

Putting that aside, I use VI(1) ONLY for its functionality.  It gives me
so many functions that I do not find in other editor's.  Not to mention
the fact that I can use my micro from home to edit files while dialed up.
Granted, most other UN*X based editors take advantage of term[cap/info].
The other really BIG plus is Regular Expression searches, and substitutions.
Most people might say "I'd never use that", but I guarantee that I have a
real hard time on other systems which don't have RE's.

Below is my classic example of the power of VI(1).  I had to do this once
apon a time when I took over a project that had been coded by some rather
new C programmers.  The problem was the switch statement.  I needed very much
to have #define macros for the case values in the switch.  The printf's in each
switch were already there, and had strings in them that were satisfactory for
the macro names.  The map statement below creates a keyboard macro which is
invoked by typing the "q" key while in "command" mode.  If you have never used
a keyboard map in VI(1), then you might try this to see what happens.  Before
typing "q" the first time, you should enter the command:

	/case[ <TAB>]*[0-9]*:<ENTER>

This will initiate a search to locate the first case in the switch below.
You can figure out what the following map does pretty easily.  It goes
like this.

1)	Move to the text after the text "case".		-> W
2)	Delete to the ':' character (exclusive of ':').	-> dt:
3)	Mark the current position as A.			-> ma
4)	Go to the top of the file.			-> 1G
5)	Open a line and insert the text "#define<TAB>".	-> O#define<TAB>^V<ESC>
6)	Put the case value with the #define.		-> p
7)	Move back to the position marked as A.		-> 'a
8)	Move down to the printf().			-> j
9)	Move to the " character.			-> f"
10)	Move to the actual text.			-> l
11)	Delete the text.				-> dt\
12)	Put it back.					-> P
13)	Move back up to the previous line.		-> k
14)	Move to the beginning of the line.		-> 0
15)	Move to the ':' character.			-> f:
16)	Put the macro name text in as CASE value.	-> P
17)	Move back to the current #define.		-> 1G
18)	Move past the #define.				-> W
19)	Put the Macro name in.				-> P
20)	Append 2 tabs after the name.			-> a<TAB><TAB>^V<ESC>
21)	Move back to the place marked as A		-> 'a
22)	Find the next case statement to change.		-> n

This is the magical map
-----------------------

<...> denotes a single key to press as in the TAB key, or the ESCAPE key,
and ^V is the CONTROL-V key, which is used to quote control characters that
might otherwise be interpreted.  Here, <ESC> needs quoting or else VI will
assume you want to end the map definition.  Also note that you must type a
':' from command mode to get into 'EX' command mode where maps are entered.

:map q Wdt:ma1GO#define<TAB>^V<ESC>p'ajf"ldt\Pk0f:P1GWPa<TAB><TAB>^V<ESC>'an

And by the way, undo even works on something like this.

------------------------------------------------------------------------------
	switch (i) {
		case 1:
			printf ("CASE1\n");
			func1(p,1,1,1,1,);
			break;

		case 2:
			printf ("CASE2\n");
			func1(p,2,1,2,1,);
			break;

		case 3:
			printf ("CASE3\n");
			func1(p,1,1,1,1,);
			break;

		case 4:
			printf ("CASE4\n");
			func1(p,1,1,1,1,);
			break;

		case 5:
			printf ("CASE5\n");
			func1(p,1,1,1,1,);
			break;

		case 6:
			printf ("CASE6\n");
			func1(p,1,1,1,1,);
			break;

		case 7:
			printf ("CASE7\n");
			func1(p,1,1,1,1,);
			break;

		case 8:
			printf ("CASE8\n");
			func1(p,1,1,1,1,);
			break;

		case 9:
			printf ("CASE9\n");
			func1(p,1,1,1,1,);
			break;

		case 10:
			printf ("CASE10\n");
			func1(p,1,1,1,1,);
			break;
	}

--------------------------------------------------------------------------
Gregg Wonderly
Department of Computing and Information Sciences
Oklahoma State University

UUCP: {cbosgd, ea, ihnp4, isucs1, mcvax, uokvax}!okstate!gregg
ARPA:  gregg%okstate.csnet at CSNET-RELAY.ARPA  
    or
ARPA:  gregg at A.CS.OKSTATE.EDU



More information about the Comp.unix.wizards mailing list