Sublime Forum

Passing line and column number?

#1

Please let me begin by admitting to my lack of experience with Sublime Text. It’s entirely possible that some of my questions will reveal an ignorance of some startlingly obvious information, and I appreciate your collective patience with me as I learn more about this wonderful editor. In this case, the topic of this post is a little vague because I have two related questions to ask.

  1. I am still figuring out the best way to call a Windows executable (with arguments) from Sublime Text (It seems like a Build System is the place to do this) but I was wondering if it is possible to extract the current line and column number of the cursor to use as arguments for this external call. I found something called file_regex in the docs and on this forum that seems close to what I want but I’m not sure it works for what I want. In the Build System docs it says the variable $FILE will be expanded to the full path and file name. Is there a similar variable or technique to get the current line and column number of the cursor?

  2. Is it possible to direct Sublime Text to open a file at a specific line number? Opening to a specific column as well would be useful but nowhere near as essential to my purposes. There are some other editors that allow this via command line arguments (even in Windows).

The far inferior editor TextPad has these features and my company has developed some very useful tools that use them. I would like to be able to modify these tools to work with SublimeText if possible. Unfortunately, our Version Control system is very quirky and I am fairly certain I would not be able to use any of the Go To features of SublimeText to find and open the files I need.

Thank you for your help! I hope to get up to speed soon and start contributing some useful things to this fine community.

(I am using Build 2181 on Windows 7)

1 Like

#2

For the build systems, the available variables are listed at docs.sublimetext.info/en/latest/ … stems.html, although there isn’t one for the current line number, which means you’d have to resort to writing a Python plugin.

To open a file at a specific line, append :line or :line:col to the file name, e.g., sublime_text.exe c:\foo.txt:100:8

1 Like

#3

Thank you so much, that’s unbelievably helpful.

I am a programmer but I haven’t used Python very much. Could you give me a hint at how to extract the line and column number in python? Or point me to relevant docs? (I can probably figure it out, but if you can help you could probably save me some serious time)

Once again, thank you so much.

edit I think I found how I’d get the line number but I’m not sure how I could get the column number.
If this proves problematic, would it be possible to extract highlighted text from a document?

1 Like

#4

Would it be very difficult to add line and column number variables to Sublime Text? It seems like an important “power feature” that would be much more efficient and accurate if it came from the application level rather than through external scripts. I mean, the Line and Column are displayed in the footer of the screen. Would it be very hard to create a variable for each of those numbers?

1 Like

#5
rowcol(point)

sublimetext.com/docs/2/api_reference.html

1 Like

#6

ST2 you could have more than one cursor so you couldn’t have only one variable for col and row, you must have at least a list.
Cursors are in view.sel() and you could use view.rowcol to find the position. This is how for the first cursor:

view.rowcol(view.sel()[0].begin())

IMHO, you have to fully understand what view.sel() is and what sublime.Region and sublime.RegionSet are before trying to dig deeper in the API.
Look at the doc and the existing Python sources.

Good luck.

1 Like

#7

[quote=“C0D312”]rowcol(point)
sublimetext.com/docs/2/api_reference.html[/quote]

THIS! Thank you so much. You just made my life so much easier.

1 Like

#8

[quote=“bizoo”]ST2 you could have more than one cursor so you couldn’t have only one variable for col and row, you must have at least a list.
Cursors are in view.sel() and you could use view.rowcol to find the position. This is how for the first cursor:

view.rowcol(view.sel()[0].begin())

IMHO, you have to fully understand what view.sel() is and what sublime.Region and sublime.RegionSet are before trying to dig deeper in the API.
Look at the doc and the existing Python sources.

Good luck.[/quote]

This helps immensely as well. Thank you!

1 Like

#9

[quote=“bizoo”]ST2 you could have more than one cursor so you couldn’t have only one variable for col and row, you must have at least a list.
Cursors are in view.sel() and you could use view.rowcol to find the position. This is how for the first cursor:

view.rowcol(view.sel()[0].begin())

[/quote]

Could I just ask if the “first cursor” is the most recent one you’ve manipulated or if it’s the first one that ever existed? I think for my purposes I won’t ever have more than one region so it shouldn’t matter but it’s not very easy to determine from the docs and I’m curious. Thanks!

1 Like

#10

Could I just ask if the “first cursor” is the most recent one you’ve manipulated or if it’s the first one that ever existed? I think for my purposes I won’t ever have more than one region so it shouldn’t matter but it’s not very easy to determine from the docs and I’m curious. Thanks![/quote]

I think that the RegionSet are always ordered first Region to last Region regarding their position in the view.
But honestly I never thought about it and never tried.
If you want to check if there is only one cursor, which is fairly common in plugins, simply do:

[code]if len(self.view.sel()) == 1:

Do whatever you want with self.view.sel()[0][/code]

1 Like

#11

You’ve been so helpful, I’ve got a bunch of stuff working how I want it now!

One further question: If I want to extract text that I’ve selected in the document, would I still use view.sel() somehow? I’m assuming it would be some function in the view class anyway. I’ve been looking through the plugins and the forums/docs and I can’t seem to find an example. (I think this should just about finish up all the questions I have :stuck_out_tongue:)

Thank you all once again for your help with this!

1 Like

#12
view.substr(view.sel()[0])
1 Like

#13

Much obliged :smile:

1 Like