Sublime Forum

Get Region by line numbers?

#1

I’d like to get a region by line numbers. For example, the region between the beginning of line 1 and the end of line 2. Worst case scenario, I can write some helper method using goto line number and view.sel() to get a and b and construct a new Region this way. But is there an easier way?

0 Likes

#2

Answering my own question. view.text_point(row, col) can be used to get the necessary points, where row is line number - 1. Something like:

def getRegionByLines(line1, line2, view): a = view.text_point(line1-1, 0) lastCol = view.line(view.text_point(line2-1, 0)).b return Region(a, lastCol)

3 Likes

#3

As view.line already return region, you can do:

return view.line(sublime.Region(view.text_point(line1-1, 0), view.text_point(line2-1, 0)))
0 Likes