I am trying to loop over a selection, grab the entire line and then replace the line with new text if a condition is met. I have figured out how to loop over the selected regions, grab each line, but I can’t figure out how to replace the line with new text.
Here is the code I have so far:
class TestingCommand(sublime_plugin.TextCommand):
def run(self, edit):
# Get a reference to the selections
sel = self.view.sel()
# Loop over the various selections
for region in self.view.sel():
# grab full lines regardless of where they start in the line
line2 = self.view.line(region)
# split them into individual lines
lines = self.view.split_by_newlines(line2)
for l in lines:
# return the contents as a string
s = self.view.substr(l)
print(s)
Where the print statement is, I would like to replace the entire line. Loop and repeat. Not all lines would be changed or replaced.
Thanks for any help or direction.