Home Download Buy Blog Forum Support

Passing line and column number?

Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 1:46 pm

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)
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby jps on Wed Apr 18, 2012 2:03 pm

For the build systems, the available variables are listed at http://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
jps
Site Admin
 
Posts: 3077
Joined: Wed Mar 19, 2008 12:33 pm

Re: Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 2:36 pm

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?
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 3:02 pm

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?
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby C0D312 on Wed Apr 18, 2012 3:03 pm

C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Passing line and column number?

Postby bizoo on Wed Apr 18, 2012 3:12 pm

askeeve wrote: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?

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:
Code: Select all
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.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 3:26 pm


THIS! Thank you so much. You just made my life so much easier.
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 3:33 pm

bizoo wrote: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:
Code: Select all
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.


This helps immensely as well. Thank you!
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby askeeve on Wed Apr 18, 2012 3:55 pm

bizoo wrote: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:
Code: Select all
view.rowcol(view.sel()[0].begin())



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!
askeeve
 
Posts: 51
Joined: Wed Apr 18, 2012 1:33 pm

Re: Passing line and column number?

Postby bizoo on Wed Apr 18, 2012 4:24 pm

askeeve wrote:
bizoo wrote: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:
Code: Select all
view.rowcol(view.sel()[0].begin())



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!

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: Select all
if len(self.view.sel()) == 1:
  # Do whatever you want with self.view.sel()[0]
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Next

Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 27 guests