Writing REXX and CLIST edit macros allows you to automate ISPF editor tasks with custom scripts. This tutorial provides step-by-step guidance and practical examples for creating simple macros. You'll learn the essential structure, common ISREDIT commands, parameter handling, error checking, and see complete working examples that you can adapt for your needs.
By learning to write macros, you can automate repetitive tasks, standardize operations, and extend the capabilities of the ISPF editor. This tutorial focuses on practical, working examples that demonstrate real-world macro writing techniques.
REXX macros start with setting the environment and using ISREDIT commands.
Every REXX edit macro needs:
Here's a simple macro that adds a comment line at the top:
12345/* REXX Macro: Add header comment */ Address ISREDIT "MACRO" "LINE_AFTER 0 SET" "/* File Header */" Exit
This macro inserts "/* File Header */" as the first line (after line 0, which is before line 1).
Here's a macro that accepts a parameter:
123456/* REXX Macro: Add custom comment */ Address ISREDIT "MACRO" Parse Arg comment "LINE_AFTER 0 SET" "/*" comment "*/" Exit
Invoke it with: MACRONAME "My Comment"
CLIST macros use ISREDIT commands with CLIST syntax.
Every CLIST edit macro needs:
Here's a simple CLIST macro that adds a comment line:
12345PROC 0 CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT LINE_AFTER 0 SET "/* File Header */" END
This macro inserts "/* File Header */" as the first line.
Here's a CLIST macro that accepts a parameter:
12345PROC 1 COMMENT CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT LINE_AFTER 0 SET "/*" "&COMMENT" "*/" END
Invoke it with: MACRONAME "My Comment"
These ISREDIT commands are essential for writing macros.
Commands for working with lines:
Commands for finding and changing text:
Commands for processing ranges:
Here are practical REXX macro examples you can use and modify.
Adds a standard header comment at the top of the file:
123456789/* REXX: Add header comment */ Address ISREDIT "MACRO" Parse Arg filename "LINE_AFTER 0 SET" "/*" "LINE_AFTER 1 SET" "/* File:" filename "LINE_AFTER 2 SET" "/* Created:" Date() "LINE_AFTER 3 SET" "*/" Exit
Removes all blank lines from the file:
12345678910111213/* REXX: Remove blank lines */ Address ISREDIT "MACRO" "PROCESS RANGE ALL" Do i = 1 to 1000 rc = LINE_GET(i, 'LINE') If rc <> 0 Then Leave If Strip(LINE) = '' Then Do "LINE_DELETE" i i = i - 1 End End Exit
Adds line numbers as comments at the end of each line:
1234567891011/* REXX: Add line numbers */ Address ISREDIT "MACRO" "PROCESS RANGE ALL" Do i = 1 to 1000 rc = LINE_GET(i, 'LINE') If rc <> 0 Then Leave newline = LINE "/* Line" i "*/" "LINE_PUT" i "SET" newline End Exit
Converts the entire file to uppercase:
1234567891011/* REXX: Convert to uppercase */ Address ISREDIT "MACRO" "PROCESS RANGE ALL" Do i = 1 to 1000 rc = LINE_GET(i, 'LINE') If rc <> 0 Then Leave upperline = Translate(LINE) "LINE_PUT" i "SET" upperline End Exit
Here are practical CLIST macro examples.
Adds a standard header comment:
1234567PROC 1 FILENAME CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT LINE_AFTER 0 SET "/*" ISREDIT LINE_AFTER 1 SET "/* File:" "&FILENAME" ISREDIT LINE_AFTER 2 SET "*/" END
Finds and replaces text (CLIST version):
12345PROC 2 OLD NEW CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT CHANGE "&OLD" "&NEW" ALL END
Deletes empty lines (simplified CLIST version):
12345678PROC 0 CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT PROCESS RANGE ALL ISREDIT FIND "" FIRST /* Note: CLIST has limited loop capabilities */ /* This is a simplified example */ END
Both REXX and CLIST support parameters, but the syntax differs.
In REXX, use ARG or PARSE ARG:
123456/* REXX: Get parameters */ Address ISREDIT "MACRO" Parse Arg param1 param2 param3 /* Use param1, param2, param3 */ Exit
Or with ARG:
123456/* REXX: Get parameters with ARG */ Address ISREDIT "MACRO" Arg param1 param2 /* Use param1, param2 */ Exit
In CLIST, use PROC with parameter count and & variables:
12345PROC 2 PARAM1 PARAM2 CONTROL MAIN NOMSG ISREDIT MACRO /* Use &PARAM1 and &PARAM2 */ END
Or access by position:
1234567PROC 0 CONTROL MAIN NOMSG ISREDIT MACRO SET &PARAM1 = &1 SET &PARAM2 = &2 /* Use &PARAM1 and &PARAM2 */ END
Proper error handling makes macros more robust.
Check return codes in REXX:
12345678910/* REXX: Error handling */ Address ISREDIT "MACRO" rc = LINE_GET(1, 'LINE') If rc <> 0 Then Do Say "Error: Could not get line 1" Exit 8 End /* Process LINE */ Exit
Check return codes in CLIST:
12345678910PROC 0 CONTROL MAIN NOMSG ISREDIT MACRO ISREDIT LINE_GET 1 LINE IF &LASTCC <> 0 THEN DO WRITE Error: Could not get line 1 EXIT CODE(8) END /* Process LINE */ END
Looping through lines is common in macros.
Loop through all lines in REXX:
123456789/* REXX: Loop through lines */ Address ISREDIT "MACRO" Do i = 1 to 10000 rc = LINE_GET(i, 'LINE') If rc <> 0 Then Leave /* Process LINE here */ End Exit
Process all lines with PROCESS RANGE:
12345678910/* REXX: Process all lines */ Address ISREDIT "MACRO" "PROCESS RANGE ALL" Do i = 1 to 10000 rc = LINE_GET(i, 'LINE') If rc <> 0 Then Leave /* Process LINE here */ End Exit
Follow these best practices when writing macros:
Steps to create and store your macros.
If you don't have a macro library:
To create a macro:
To test your macro:
Think of writing macros like teaching a robot helper how to do a task:
So writing macros is like teaching a robot helper how to do editing tasks by writing step-by-step instructions in a special language!
Complete these exercises to reinforce your understanding of writing macros:
Create a simple REXX macro: write a macro that adds a single comment line at the top of a file, test it on a sample file, and verify it works. Learn the basic structure.
Create a REXX macro with parameters: write a macro that accepts a parameter and uses it in the operation, test it with different parameters, and verify parameter handling works correctly.
Create a simple CLIST macro: write a CLIST macro that performs a simple operation, test it on a sample file, and verify it works. Learn the basic CLIST structure.
Create a macro with looping: write a REXX macro that loops through lines and processes them, test it on a sample file, and verify the loop works correctly. Practice line iteration.
Add error handling to a macro: take an existing macro and add error checking, test it with error conditions, and verify error messages are displayed. Practice robust macro writing.
1. What command sets the REXX environment to ISPF editor?
2. How do you access the first parameter in a CLIST macro?
3. What ISREDIT command inserts a line after line n?
4. How do you get parameters in REXX?
5. What command processes all lines in a REXX macro?