I’d like to find a character and the cursor to remain at the position of that character and to be able to use that function in a macro (I’ll have to do this the hard way, but for next time)
The function will (plugin) no doubt be a combination of these two (excellently written by @OdatNurd I think)
import sublime
import sublime_plugin
class FindReplaceCommand(sublime_plugin.TextCommand):
FLAGS = {
"LITERAL": sublime.LITERAL,
"IGNORECASE": sublime.IGNORECASE
}
def run(self, edit, pattern, replace_by, flags=[]):
"""Find and replace all patterns.
Arguments:
edit (sublime.Edit):
The edit token used to undo this command.
pattern (string):
The regex pattern to use for finding.
replace_by (string):
The text to replace all found words by.
flags (list):
The flags to pass to view.find()
["LITERAL", "IGNORECASE"]
"""
found = self.view.find_all(pattern, self._flags(flags))
if not found:
return
for idx, region in enumerate(found):
if idx == 0:
self.view.replace(edit, region, replace_by)
else:
difference = len(replace_by) - len(pattern)
if difference != 0:
modified_region = sublime.Region(region.begin() + (idx * difference),
region.end() + (idx * difference))
self.view.replace(edit, modified_region, replace_by)
else:
self.view.replace(edit, region, replace_by)
def _flags(self, flags):
"""Translate list of flags."""
result = 0
for flag in flags:
result |= self.FLAGS.get(flag, 0)
return result
and
import sublime, sublime_plugin
class GotoColCommand(sublime_plugin.TextCommand):
def run(self, edit, col=120):
for s in self.view.sel() :
_ , c_col = self.view.rowcol(s.a)
nb_space = col - c_col
if nb_space > 0:
self.view.insert(edit, s.a, ' '*nb_space)
Something like (and I know no Python)
import sublime, sublime_plugin
class GotoCharacter(sublime_plugin.TextCommand)
def run(self, edit, patter)
found = self.view.find(patter, self)
if not found
return
# I HAVE ABSOLUTELY NO IDEA AT THIS POINT
for s in self.view.sel()
self.view.insert ()
Thank you very much. It’s such a roller coaster
Lozminda