Using and managing code snippets

IBM Z Open Editor ships with a library of reusable code snippets for COBOL, PL/I, and JCL to help you improve your productivity, as well as to standardize on code patterns and best practices. In addition to these out-of-the-box snippets, you can also create your own snippets and import snippets from IBM Developer for z/OS.

IBM Z Open Editor utilizes the Snippet facilities provided by VS Code. To learn more about this code snippet support, refer to the VS Code Online Help pagesopen in new window.

Using code snippets

To explore and insert a code snippet that ships with IBM Z Open Editor, use either the VS Code Snippet picker or the code completion feature while editing a program file.

Inserting a code snippet with a picker

  1. Place the cursor on Column 1 in the editor as COBOL snippets come with indentations based on that column.

  2. Open the VS Code command palette with Ctrl+Shift+P (Windows) or Cmd+Shift+P (Mac).

  3. Type Insert Snippet until the command appears, and select it.

  4. Based on the language that you are working with, it will then show a picker drop-down with the available snippets. All snippets are organized into a group of related snippets such as COBOL Embedded SQL or COBOL CICS. Each snippet also has a descriptive name and summary.

    1. Scroll down through the list or just type any substring of the snippet your are looking for.
    2. Select the snippet you want to insert and it will be added to the current cursor location.
  5. Many snippets have place holders for variables or terminals. You can use the Tab key or Shift-Tab to navigate between them.

  6. Type the new value to replace this and all other occurrences of the place holder and press Enter to finish.

Inserting a code snippet using code completion

  1. When you edit a program, press Ctrl+Space to start code completion.

  2. In the drop-down list, you will see first the list of context-sensitive code completion suggestions provided by the language server assuming you are working either in COBOL or PL/I. If you scroll down, you see the list of code snippets below.

  3. Again, you can just type for any code snippet substring to search for a snippet that you are looking for to jump ahead in the drop-down.

  4. Select the snippet and it will be inserted at your current cursor location.

Creating your own code snippets

You can easily complement the list of code snippets with your own as follows.

  1. Follow the VS Code instructionsopen in new window to create your own snippets collection.

  2. In the language drop-down, select type cobol, pl1, hlasm, or jcl as the language identifiers to start a snippet collection in that language. VS Code will open an editor window with instructions for how to write your own snippets. The help page listed above will also provide you with detailed guidance.

  3. Once you have added your first snippet, save it. It will appear in the Snippet Picker described above placed before the IBM snippets.

Importing code snippets from IBM Developer for z/OS

If you are using the IBM Developer for z/OS (IDz) and have created custom code snippets there, you can export these code snippets and import them into the Z Open Editor.

Exporting code snippets in IDz

  1. To display the available snippets in IDz, use the menu Window > Show View > Snippets.

  2. Right-click anywhere in the Snippets view and select Customize...

  3. In the dialog, find the Snippet group you want to export and click the Export button.

  4. Specify the name and location for the export file, which will be an XML file.

Importing IDz code snippets into IBM Z Open Editor

  1. In IBM Z Open Editor, open the VS Code command palette with Ctrl+Shift+P (Windows) or Cmd+Shift+P (Mac).

  2. Start typing IBM Z Open Editor: Import IBM Developer for Z Code Snippets until you see this command and select it.

  3. A drop-down picker appears asking you for the program language of the snippets you want to import. Select one from the drop-down menu.

  4. A file dialog appears. Navigate to and select the file that exported from IDz in the previous step.

  5. Then a final text drop down appears asking you for a prefix string for the imported snippets. This prefix will be placed in front of the name of every snippet to be imported, which allows you to group the snippets by name in the resulting Snippet picker. You can leave this value empty and/or edit it again after the import.

If the import succeeds, it opens a JSON file editor with the imported snippets in the Microsoft VS Code format as documented in the help page listed above. You can now edit and make changes to polish the snippets and to add place holders.

If there were errors during the import, those errors will be displayed in a separate JSON file. That file will show up only if there were indeed any errors.

Last Updated:
Contributors: Shi Kun Li, Peter Haumer, Min Huang, Peter Haumer