2

I have the following plugin that puts a time stamp at the top of the document on line 1 but I'd like it to insert the string on a different line, like line 6. At first I thought the insert method was 0 indexed but that doesn't seem to be the case. How would I tell the insert method which line to insert the signature string at?

import sublime, sublime_plugin
import datetime, getpass

class SignatureCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        signature = "[%s]\n" % (datetime.datetime.now().strftime("%A, %B %d %I:%M %p"))
        self.view.insert(edit, 0, signature)

Thanks for your help :)

Update: thanks to Enteleform for the wonderful answer, I added a line_num variable for added clarity :)

import sublime, sublime_plugin
import datetime, getpass

class SignatureOnSpecificLineCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        line_num = 6 # line number that signature will go on
        signature = "[%s]\n" % (datetime.datetime.now().strftime("%A, %B %d %I:%M %p"))
        line6_column0 = self.view.text_point(line_num - 1, 0)
        self.view.insert(edit, line6_column0, signature)

1 Answer 1

3

view.insert() takes a point as it's location argument.

Points are essentially sequential character positions within a document.


For example, in the following document:

Hello
World

a caret at the end of World would be at point 11

  • 5 characters in Hello
  • 1 NewLine character after Hello
  • 5 characters in World

In order to calculate the point of a particular row & column, use:

view.text_point(row, column)


Example:

import sublime, sublime_plugin
import datetime, getpass

class SignatureCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        signature = "[%s]\n" % (datetime.datetime.now().strftime("%A, %B %d %I:%M %p"))
        line = 6
        point = self.view.text_point(line - 1, 0)
        self.view.insert(edit, point, signature)

Note:

rows start at 0 and thus are offset from the displayed lines in SublimeText by -1, which is why I included line - 1 in view.text_point()

Sign up to request clarification or add additional context in comments.

2 Comments

Cool! Thanks so much :) this was exactly what I was looking for. I also went ahead and added a line_num variable that can be adjusted later.
@mbigras : Good call, I updated the answer to reflect that since it offers improved usability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.