We will now learn the standard actions
Rewrite has 10 built-in actions for modifying text listed below:
insertremovereplaceswapshiftcopyprefixappendsetapply
We will begin by learning about the insert action.
You can use the insert action to place text into your file at a specified position(s), or before or after specified text. The insert action has the format insert [what to insert] [where to insert].
Insert at
We have already touched this command in part 1. Take the following command for example:
insert "foo" at pos 0
This will tell Rewrite to insert the text “foo” at the first position. You can see the this in action below:
Hello world!>> insert "foo" at pos 0fooHello world!
The insert at action also supports multiple positions.
Hello world!>> insert "foo" at pos 0, 5, lastfooHellofoo world!foo
Insert before/after
You can use this command to insert text before or after a specified text. Take the following commands for example:
Hello world!>> insert "foo" before every "l"Hefoolfoolo worfoold!
Hello world!>> insert "foo" after every "l"Helfoolfooo worlfood!
This command supports all types of instances. The following commands are also valid:
insert "foo" before first "l"insert "foo" before last "l"insert "foo" before inst 1 "l"insert "foo" before inst 1, 3, 5 "l"
The insert before/after command also works with character indexes.
Hello world!>> insert "foo" before char 1fooHello world!
Hello world!>> insert "foo" after char 1Hfooello world!
Hello world!>> insert "foo" before char 1, 3, 5fooHefoollfooo world!
Hello world!>> insert "foo" after char 1, 3, 5Hfooelfoolofoo world!
Regex and xcase
There are two special text modifiers that is supported by the insert before/after command: regex and xcase. These modifiers can be used on their own, with each other, and with any instance.
Regex
Using the modifier regex will turn the proceeding text into a regular expression. You can see it in action below:
Hello world!>> insert "foo" before every regex "\w+"fooHello fooworld!
As mentioned above, you can specify which regex match to insert before or after using instances.
insert "foo" before first regex "\w+"
insert "foo" before last regex "\w+"
insert "foo" before inst 1 regex "\w+"
insert "foo" before inst 1, 3, 5 regex "\w+"
The regular expressions are based on the .NET regular expression framework. All .NET regex commands will work with Rewrite.
Xcase
Normally the case is ignored when checking for matching text. The xcase (exclusive case) modifier will make sure that the text you are searching for matches the case of the text in your command.
Take the following commands for example:
Hello WORLD!>> insert "foo" before every "o"Hellfooo WfooORLD!
As you can see, the command matched with the lowercase “o” and the uppercase “O”. Let’s add the xcase modifier and see what happens:
Hello WORLD!>> insert "foo" before every xcase "o"Hellfooo WORLD!
This time, the uppercase “O” was not matched because the text in the command was in lowercase.
Documentation
More information on the insert command can be found on the documentation page.
Prefix and append
The prefix and append commands are variations of the insert command.
The prefix command will insert text at the beginning of the input while the append command will insert text at the end of the input. There isn’t much more to these commands, so we will not have a whole page on learning about them.
prefix "foo"append "foo"
More information on these commands can be found on the prefix documentation page and the append documentation page.
Test your knowledge
Next up, there will be a short quiz to make sure you understand everything you have learnt in part 2. If you do not wish to take this test, click skip quiz.
