Have a look at the comments
- Code: Select all
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