Sublime Forum

[Proof Of Concept] MatchReplace from Source To Target Document

#1

Written in response to this StackOverflow question asking how to replace matched selections between a source & target file.

 



#Demo:

 



#Code:

@ GitHub

 



#Implementation:

 

  • get active views of each group in a 2 group window
  • find RegEx matches of a user-defined array of keys which will be matched at both documents
  • store value regions of both documents, and string values of the source document
  • sort stored values by region
  • iterate over regions at the target document, replacing all matched values from the source

 

The demo is written to work with single tier JSON files, but can be adjusted as necessary.

RegEx patterns to precede & follow replacementKeys are:

  • queryPrefix
  • querySuffix

 



#Usage:

 

To use MatchReplace:

  • copy the plugin folder to your Packages directory
  • edit the replacementKeys array in the run function to suit your match+replace needs
  • save the plugin & restart SublimeText
  • open a 2-group window layout using Shift + Alt + 2
  • move the document you want as the SOURCE to the left group
  • move the document you want to MATCH with the source to the right group
  • open the Command Palette with Ctrl + Shift + P and run the Match Replace: Demo command
     

###Note:

replacementKeys must be an exact match at both documents ( leading whitespace is ignored ).

If you want to allow for variation within the replacementKeys, you will need to implement an additional layer of RegEx processing.

1 Like

Show the lines that are found (or found partially) in another file
Insert characters inside every part of line