Skip to main content
added 53 characters in body; added 2 characters in body; added 539 characters in body
Source Link
D. Ben Knoble
  • 27.7k
  • 3
  • 31
  • 68

Yank all the matching text first, then do your substitute:

  1. Clear a register: qaq
  2. Set a mark at the original point: gg/pattern then mo
  3. Yank text: :g/pattern/yank A
  4. 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)

Yank all the matching text first, then do your substitute:

  1. Clear a register: qaq
  2. Yank text: :g/pattern/yank A
  3. Substitute: :%s//replacement/flags

Now paste: "ap

Yank all the matching text first, then do your substitute:

  1. Clear a register: qaq
  2. Set a mark at the original point: gg/pattern then mo
  3. Yank text: :g//yank A
  4. Substitute: :%s//replacement/flags

Now paste: '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)

Source Link
D. Ben Knoble
  • 27.7k
  • 3
  • 31
  • 68

Yank all the matching text first, then do your substitute:

  1. Clear a register: qaq
  2. Yank text: :g/pattern/yank A
  3. Substitute: :%s//replacement/flags

Now paste: "ap