Sublime Forum

[SOLVED] Plugin delete 1 character left of the cursor

#1

I’ve got the current line I want to edit and the position of the cursor, I want to be able to delete 1 character left of the cursor for each time the method is run.

class DeleteOneCharacterLeftOfCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        line = self.view.substr(self.view.line(self.view.sel()[0].b))
        pos = self.view.sel()[0].begin()

Does anybody know how this can be done?

0 Likes

#2

what is the relevance of getting the line as a string in your code?

0 Likes

#3

Hey @kingkeith I just realised I didn’t need it :slight_smile:

I’ve managed to do it.

class DeleteOneCharacterLeftOfCursor(sublime_plugin.TextCommand):
    def run(self, edit):
        pos = self.view.sel()[0].begin()
        pos_end = self.view.sel()[0].begin() - 1

        region = sublime.Region(pos_end, pos)

        self.view.erase(edit, region)
0 Likes

#4

Note that the left_delete built in command does this already, so you don’t need to implement a new command to do it unless you want to only delete to the left of the first selection when there are many.

0 Likes

#5

I can’t believe I forgot about that, I remember seeing that a couple of days ago too, could of saved me a headache.

Thanks :slight_smile:

0 Likes

#6

Yep you’re right, thanks haha

0 Likes

#7

Probably you meant this to be a private message since nobody else is going to understand the question.

The way the code is written you probably don’t want to have too large a value for the maximum number of fake ID values or opening and closing files (or browsing for a file in Goto Anything) is going to start seriously bogging down.

All else being equal the general idea originally was that since you hardly ever have many files open, assigning them fake view ID’s to make referencing them easier later was a way to stop having arbitrarily large view ID’s in the panel. If you want to grow the number as the number of views grows, then you’re artificially making the numbers arbitrarily large anyway.

0 Likes