What is Rewrite?
Rewrite is a natural English language script to edit any text in any way imaginable. Rewrite is also extensible as you can import methods that you or other people have written to do very complex tasks. We will learn about this later.
This tutorial does not assume previous knowledge with other programming languages, however it would be an advantage if you already understand basic programming concepts. Some of the more complex commands might be a bit difficult for people new to programming.
Let’s get started learning Rewrite!
The structure of a script
Rewrite is very dependent on spaces and line breaks. Each line is a new command and spaces break up each keyword. You cannot split a command into multiple lines and each keyword must have one or more spaces between them.
insert "foo" at pos 0
replace every "foo" with "bar"
Below are examples of incorrectly structured scripts. These scripts will not run.
insert "foo"
at pos 0
replace every"foo"with"bar"
Positions, characters, and instances
Before we continue with this tutorial, it is important to understand what positions, characters, and instances are and how to use them.
Positions
A position is represented by the keyword pos or a pair of square brackets [].
A position is a number which represents the spaces between letters and other characters. The first position appears before the first letter in your file and has the value 0.
0H1e2l3l4o5 6w7o8r9l10d11!12
For example, take the following text:
Hello world!
If we insert the word “foo” at position 0, we would get the following output:
insert "foo" at pos 0fooHello world!
If we insert the word “foo” at position 1, we would get the following output:
insert "foo" at pos 1Hfooello world!
Shorthand
As mentioned above, square brackets [] can also represent positions. Take the following code for example:
insert "foo" at pos 0
This can be rewritten as:
insert "foo" at [0]
Multiple positions
There are cases where you might want to apply the same rule to multiple positions at once. You can do this by adding a , between position indexes.
insert "foo" at pos 0, 2, 4fooHefoollfooo world!
or
insert "foo" at [0, 2, 4]fooHefoollfooo world!
Hint: you can add as many or as few spaces between commas and inside brackets as you wish. You only need to keep keywords and quotes separated by spaces.
Characters
A character is represented by the keyword char or a pair of parentheses ().
A character represents the index of letters or other characters where they appear in the text. The first character is the first letter in your file and has the value 1.
For example, take the following text:
Hello world!
If we replace the character 1 with the word “foo”, we will get the following output:
replace char 1 with "foo"fooello world!
If we replace the character 2 with the word “foo”, we will get the following output:
replace char 2 with "foo"Hfoollo world!
Shorthand
Just like with positions, you can use parentheses () to represent characters. Take the following code for example:
replace char 1 with "foo"
This can be rewritten as:
replace (1) with "foo"
Multiple characters
Just like with positions, you can apply a rule to multiple characters at once by adding a , between character indexes.
replace char 1, 3, 5 with "foo"fooefoolfoo world!
or
replace (1, 3, 5) with "foo"fooefoolfoo world!
Instances
Sometimes there might be more than one match to a condition. Each match is referred to as an “instance”.
For example, take the following text:
Hello world!
If you were to run the following command:
replace every "o" with "bar"
The command would match two “o”s as highlighted below:
Hello world!
The first match is referred to as instance 1 and the second match is referred to as instance 2.
There are three predefined instances with you can use in your commands:
everyfirstlast
You can see them in action below:
replace every "o" with "bar"Hellbar wbarrld!
replace first "o" with "bar"Hellbar world!
replace last "o" with "bar"Hello wbarrld!
Sometimes however, you might want to define exactly which instance will be affected. You can do this by using the inst keyword or a pair of curly brackets {}.
Let’s go back to our sample input
Hello world!
and replace instance 1 of “o” with “bar”
replace inst 1 "o" with "bar"Hellbar world!
or replace instance 2 of “o” with “bar”
replace inst 2 "o" with "bar"Hello wbarrld!
or perhaps we would like to replace instance 1 and instance 2 of “o” with “bar”
replace inst 1, 2 "o" with "bar"Hellbar wbarrld!
Hint: if you do not specify an instance, Rewrite will use the default value every.
Shorthand
Just like with positions and characters, you can use curly brackets {} to represent instances. Take the following code for example:
replace inst 1 "o" with "bar"
This can be rewitten as:
replace {1} "o" with "bar"
And take the following code:
replace inst 1, 3, 5 "o" with "bar"
This can be rewitten as:
replace {1, 3, 5} "o" with "bar"
Aliases
Positions, characters, and instances have the following aliases you can use:
firstlastoddeven
You can use these to replace actual index numbers and simplify your code.
... pos first ...
... pos last ...
... pos odd ...
... pos even ...
You can use these aliases in conjunction with other indexes:
... pos first, 2, last ...
... char first, 2, last ...
... inst first, 2, last ...
And of course, you can use shorthand:
... [first, 2, last] ...
... (first, 2, last) ...
... {first, 2, last} ...
You can also completely replace positions with the following aliases:
startend
You can see them in action below:
insert "foo" at startfooHello world!
insert "foo" at endHello world!foo
However, ideally you should not be using these last two aliases in this case as the following commands already exist and have the same result:
prefix "foo"
append "foo"
You will find that the start and end aliases have some more practical uses later on.
Adding comments
Sometimes you may want to add a comment to your code to help another person understand what the script does, or to help you remember what the script does in the future.
There are two types of comments you can add – inline comments and multiline comments.
Inline comments
Inline comments are represented by the keyword note:. Everything on the line after this command will be ignored when the code runs. You can add this comment at the end of a line or on a line of its own.
insert "foo" at pos 0 note: this will add the word foo to the start
note: this will replace all instances of foo with bar
replace every "foo" with "bar"
Note: any code written after note: will be ignored even if it is a legitimate command.
note: insert "foo" at pos 0
Multiline comments
Sometimes a comment may be too long for a single line. If you don’t want to write note: at the start of every line, you can create a multiline comment using the triple quotation mark """. Everything between two triple quotation marks will be ignored when the code runs.
"""
This code does the following:
First, it will insert foo before all text.
Then it will replace all instances of foo with bar.
"""
insert "foo" at pos 0
replace every "foo" with "bar"
Test your knowledge
Next up, there will be a short quiz to make sure you understand everything you have learnt in part 1. If you do not wish to take this test, click skip quiz.
