Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the current caret position?

Tags:

sublimetext2

I have written a simple ST2 plugin that should just insert a timestamp at the current caret position. However, I am unable to find out how to get the current position.

I have

def run(self, edit):
    timestamp = "%s" % (datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
    pos = ???
    self.view.insert(edit, pos, timestamp)

What should pos be?

like image 531
kasperhj Avatar asked Oct 10 '12 08:10

kasperhj


1 Answers

Try with

pos = self.view.sel()[0].begin()

This gets the start point of the current selection (if nothing is selected, start and end of selection are the current cursor position).

If you want this to work with multiple selection, you have to iterate on all selections returned by self.view.sel():

for pos in self.view.sel():
    self.view.insert(edit, pos.begin(), timestamp)
like image 65
Riccardo Marotti Avatar answered Sep 22 '22 10:09

Riccardo Marotti