I don’t know if I’m missing something but the plugin from sublimator https://forum.sublimetext.com/t/set-cursor-position/187/1 reveals a strange behavior of view.replace() and view.erase().
Have a look at the comments
for sel in view.sel():
end_pt = sel.end()
# if len(sel) == 0 the textposition will become bof!
# to prevent this insert a space
if end_pt != view.size():
view.insert (end_pt, ' ')
view.runCommand ('invertSelection')
for sel in view.sel():
# now remove the inserted space
print '|%s|%d' % (view.substr (sel), len (sel)) # to verify selection is just ' '
if len (sel) == 1:
view.replace (sel, '') # removes too many spaces
elif len (sel) > 1:
view.erase (sublime.Region (sel.begin(), sel.begin()+1)) # removes too many spaces
Example (| is the cursor position, _ is a space):
-
before running the command:
view.replace_____|(sel, ‘’) -
after running:
view.replace|(sel, ‘’) -
all spaces left to | should not be replaced as len(sel) = 1 (because of the inserted space above)
-
it prints:
| |1
View.erase behaves the same. All preceeding spaces will be erased. No tabs, real spaces.