Substitution variables

Sometimes it is necessary to tranfer some information about the match to the replacement string. This is done with variables. There are 10 variables, indicated by the 10 digits. Such a variable is used by specifying the character @ followed by the corresponding digit. In the pattern the variable is filled with the contents of what the single object to the left of it matched to. In the replacement string the contents of the variable are substituted. Example:

    //<[^>]*@1>/=/@1/
In each line objects of the type "<return>" would be replaced by the plain string "return". The pattern says: First a character <, then any number of characters, unless it is the character >. This sequence of characters is put together into the variable 1. Then the pattern needs a >. If such a match is found the whole thing is taken out and replaced by the contents of the variable 1. The following is a little fancier:
    //!+@1!^+@2!+@3/=/@3@2@1/
The object !+ is a sequence of characters that can belong to a word, in other words: a word. We put it in variable 1. Then a sequence of non word characters should be put in variable two. The word after it goes into variable 3. The replacement string has then the two words exchanged. This single replacement exchanges all pairs of words in a file!

Multiple occurrences of the same variable in the pattern force the pattern matcher to have these match identical objects:

    //[0-9]@1[0-9]@1/=/:@1:/
Here all pairs of identical digits are replaced by a single digit enclosed by semicolons. It can be even wilder:
    //(&@1){2,}/=/@1/
Here all strings of at least two the same alphabetic characters are replaced by a single occurrence of that character.

There is a special variable that exists in the replacement string only. The character & in the replacement string signifies the whole match of the pattern. So

    //./=/&=/r
puts an equals sign after each character in the current range between the cursor and the mark.