Yank all the matching text first, then do your substitute:
- Clear a register:
qaq
- Set a mark at the original point:
gg/pattern
thenmo
- Yank text:
:g/pattern/yank A
- Substitute:
:%s//replacement/flags
Now paste: "ap'o"ap
The paste will paste all of the matching text at the location of the first match pre-substitution.
If you want to save each match and the line it came from, I’d write a function to create a buffer-local dictionary (line -> text
assuming one match per line, or line -> { column -> text }
—dict of dict—for multiple matches anticipated), loop over the matches, grab the text and line number for the dicts, then do the substitution. Then you could write a separate function to "restore" from this dictionary based on line (and column)