Sublime Forum

How to get the current cursor location/point

#1

Hello;

I’m trying to paste some text into wherever the cursor happens to be, so I need to figure out how to extract the current location of the cursor.

My hunch is that I need to use view.rowcol(), but I can’t get it working, so I’m wondering if someone can suggest a solution to getting the current location/point of the cursor such that view.insert() could be used to paste something right on it.

Regards;

0 Likes

#2

Firstly, if you don’t know about it, check out the api reference: sublimetext.com/docs/api-reference.
To get the cursor, you call

for region in view.sel():

Since Sublime Text works with multiple cursors, view.sel() will give you back a RegionSet (a list of regions), each region representing one “selection” (this can either be just a cursor, in which case the reigon’s two points are at the same place, or an actual selection if you’ve got selected text).

So let’s say I wanted to insert text into all the places where my cursor is, I’d do:

for region in view.sel(): # Loop over all the cursors view.insert(region.begin(), "string to insert") # Insert into the view at the start of each selection.

1 Like