Search code examples
sublimetext3sublime-text-plugin

Show total line count status bar sublime text 3


Is there a code to put in the settings or a plugin that will show the total number of lines along the current line and column in the status bar in Sublime Text 3?


Solution

  • The code to show the number of lines in the status bar is very simple, just get the number of lines

    line_count = view.rowcol(view.size())[0] + 1
    

    and write the to the status bar

    view.set_status("line_count", "#Lines: {0}".format(line_count))
    

    If you want to pack in a plugin you just need to write this in a function and call it on some EventListener. Create a plugin by clicking Tools >> Developer >> New Plugin... and paste:

    import time
    import sublime
    import sublime_plugin
    
    last_change = time.time()
    update_interval = 1.5  # s
    
    
    class LineCountUpdateListener(sublime_plugin.EventListener):
        def update_line_count(self, view):
            line_count = view.rowcol(view.size())[0] + 1
            view.set_status("line_count", "#Lines: {0}".format(line_count))
    
        def on_modified(self, view):
            global last_change
            current_change = time.time()
            # check if we haven't embedded the change in the last update
            if current_change > last_change + update_interval:
                last_change = current_change
                sublime.set_timeout(lambda: self.update_line_count(view),
                                    int(update_interval * 1000))
    
        on_new = update_line_count
        on_load = update_line_count
    

    This does in essentially call the command, when creating a new view, loading a file, and modifying the views content. For performance reason it has some logic to not call it on every modification.