Use this. Note that the numbersvars(true) option of the write family just tell these predicates to interpret '$VAR'(N) as A, B, … You have to number the variables yourself. Note that if you need to print multiple terms with possible sharing variables you should numbervars them together, for example by passing a list to numbervars/4.
Finally, the double negation is to make sure that the bindings are undone before proceeding and thus the term is not modified by the operation.
A note, format uses numbervars(true) option by default. So only format('~w', [Term]) also works with numbervars. singletons(true) I like this option to reveal singleton vars.