Khemia Macros - Simple VSCode ProgrammabilityFeaturesKhemia is a simple macro language to help automate repetitive typing jobs in Visual Studio Code. It is simple to pick up and powerful when needed. Getting StartedA Khemia program is normally saved as a text file with the extension When you the command To run the last Khemia program again, use the Another useful command is The use of keyboard shortcuts is discussed later on. A program in Khemia is made up of a series of commands separated by
semi-colons or new-lines. Comments are introduced by Basic CommandsThe simplest Khemia programs use the following five commands:
These move the cursor or type text in to the document. At its simplest, the
You can use both double and single quotes, as long as they match, to supply the
string. Normally, statements are separated by a
These characters are entered verbatim in to the document. A
The back-slash can also be used to enter tabs The cursor movement characters can optionally be followed by a number to move the cursor multiple times. To move the cursor four characters to the right, use:
Adding the
Finding Text in a DocumentThe
This woul match Word as well as WORD, WoRd and any other case alteration of word. When the text is found, it is also selected (by default). This means you can
immediately use By default, the search starts from the current cursor location and proceeds to the end of the document and stops. If the search fails to find anything, it will cause the Khemia program to stop. A simple search and replace can therefore be built like this:
Running this macro will replace the first instance of Word with Mot. If Word is not found, then nothing happens. Modifying the way that Find worksThere are three ways in which you can change the way that The search Mode determines how Khemia matches text in its results:
The search Direction can be one of the following:
The search Behaviour can be one of the following:
To set the way that find works, you can add any item from each of the three
groups to your
Deleting TextThe selected text can be deleted with the Using VariablesKhemia allows simple expressions to be evaluated in programs. For example:
The Variables are indicated by a name starting with $ for a string variable, or a name starting with % for an integer variable. The following pseudo-variables are defined:
The following operators are defined:
We can therefore write commands like these:
And use the variables in
The string slicing operators work like those in Python and allow the string to be sliced according to three parameters passed within the brackets, as follows: string[start:end:step] The string is split to create a new string using characters from the range starting at index start and ending before index end. All parameters are optional and use sensible defaults when not supplied. If supplied, the step parameter takes every nth character from the range. It is possible to use -ve numbers to specify the indices as an offset from the end of the string. A -ve step may be provided to return ranges from the input string in reverse order. Some examples are shown below, but the Python documentation is a useful reference.
Control StatementsThe command
This types the word Hello, followed by a line-feed, 4 times. You can use the
This produces:
The counter starts with the value 1 and is incremented at the end of each loop. You can set the counter to something different at any point, but it will always have the iteration number at the start of each loop. Find in Repeat StatementAs noted before, A simple search and replace can therefore be defined like this:
In this case, the loop count is left out, because we want to repeat until the
This program will replace up to 4 instances of the word Hello, starting at the beginning of the document and replacing them with the word Bonjour and the instance number. If fewer than 4 instances of the word Hello exist, they will all be replaced. To reduce the possibility of creating a program which never ends, repeat will not allow more than 1000 iterations when no numeric limit is given. You can override this behaviour by simply specifying a very high limit. Running Visual Studio Code CommandsThe
This will toggle the the current line to and from a comment. Commands which take
arguments are supported with the
Activating a Macro with a KeypressTo set up key-bindings for Khemia macros, you need to edit Visual Studio
Code's key bindings file, using
Choose a keyboard combination and program to suit. The argument can be a full file path to a macro or can use the following pseudo-root folders:
For example:
This will run the insert_header macro in the folder that has been saved in the Khemia preferences. For small macros, you can pass the macro as an argument to the inline macro runner.
You can bind a key to re-run the last macro run:
This will run the last successful macro again. It runs the compiled copy of the macro, so if it does not matter whether it was from a file or was inline it will run the macro exactly as before. This happens even if the source file has been changed. Because it uses the compiled copy of the macro, it may be a little faster than compiling and then running a macro. Khemia registers the following keys by default:
Use Cmd instead of Ctrl on Mac. To use the Scratch macro you will need to configure the folder in the Khemia settings and make a suitable file. And Finally…Output can be logged to the output window using the If there us an error in the macro file Khemia will show a pop-up error in the Visual Studio Code window at the bottom right. This will include the precise location where the error was found. The extension provides basic Intellisense suggesstions and program validation for files with the Khemia extension. Khemia always runs scripts in their saved form, that is, if the script is open in Code and modified, running it will run the version on disc, not the version in the editor. RequirementsIf you have any requirements or dependencies, add a section describing those and how to install and configure them. Extension SettingsThis extension contributes the following settings:
Known Issues
Future Features
|