Sublime Forum

Line count or percentage in status bar

#1

Hi,

Is there an easy way to get the bottom status bar to display the total line count or percentage of the document. Preferably, this shall be done without a plugin. An example:

Line 143 / 160

Or:

65 % [line position relative to the whole document]

Thanks in advance.

1 Like

#2

The answer is “no”, if a plugin is no option.

0 Likes

#3

Could you advise a minimalistic plugin that would do the job?

0 Likes

#4

The following should work

import sublime
import sublime_plugin


class LineCountListener(sublime_plugin.EventListener):
	"""
	This class describes a line count listener.

	Note: Set ``"show_line_column", "disabled"``
	      to disable ST's built-in line/column status
	"""

	def on_new(self, view: sublime.View):
		"""
		Update status for new view.
		"""
		self._update(view)

	def on_load(self, view: sublime.View):
		"""
		Update status after loading file.
		"""
		self._update(view)

	def on_selection_modified(self, view: sublime.View):
		"""
		Update status when caret moves.
		"""
		self._update(view)

	def _update(self, view: sublime.View):
		pt = view.sel()[0].begin()
		row, col = view.rowcol(pt)
		cols = len(view.line(pt))
		lines, _ = view.rowcol(view.size())
		view.set_status("zzz_lines", f"L: {(row + 1)}/{(lines + 1)}, C: {col + 1}/{cols + 1}")
1 Like

#5

@deathaxe thank you, the plugin works. Fantastic!

0 Likes

#6

Below is the plugin version for complete newbies, like me, who only want to see the total number of lines, not columns, and want to remove the native ST display of lines and columns. The status bar in the end will look like this:

Line 1 of 40, Column 3

Step 1. Add this in ST settings: "show_line_column": "disabled",

Step 2. Import the code below via Tools > Developer > New plugin. For Linux, the plugin shall be saved in ~/.config/sublime-text/Packages/User/*.py

Hooray to @deathaxe!

import sublime
import sublime_plugin


class LineCountListener(sublime_plugin.EventListener):
	"""
	This class describes a line count listener.

	Note: Set ``"show_line_column", "disabled"``
	      to disable ST's built-in line/column status
	"""

	def on_new(self, view: sublime.View):
		"""
		Update status for new view.
		"""
		self._update(view)

	def on_load(self, view: sublime.View):
		"""
		Update status after loading file.
		"""
		self._update(view)

	def on_selection_modified(self, view: sublime.View):
		"""
		Update status when caret moves.
		"""
		self._update(view)

	def _update(self, view: sublime.View):
		pt = view.sel()[0].begin()
		row, col = view.rowcol(pt)
		cols = len(view.line(pt))
		lines, _ = view.rowcol(view.size())
		view.set_status("zzz_lines", f"Line {(row + 1)} of {(lines + 1)}, Column {col + 1}")

		"""
		/{cols + 1}
		"""
1 Like

Hide line/column numbers in status bar
#7

My search displays the line count at the end of the page.
To get the total line count I force a display of the final page.
By reading to the end then a 2nd time to the end less 30.
having the elapsed time is important too.

0 Likes

#9

What should be deleted in this plugin to correctly remove column number display?

0 Likes

#10

This is the line that’s adding that text:

view.set_status("zzz_lines", f"Line {(row + 1)} of {(lines + 1)}, Column {col + 1}")

If you don’t want the column number part, then remove the , Column {col + 1} from the format string, and it will only show you the line count instead.

1 Like

#11

Many thanks, it works!

0 Likes

#12

What can cause errors?

Traceback (most recent call last):
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python38/sublime_plugin.py", line 967, in on_selection_modified
    run_view_callbacks('on_selection_modified', view_id)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python38/sublime_plugin.py", line 741, in run_view_callbacks
    callback(v, *args)
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python38/sublime_plugin.py", line 162, in profiler
    return event_handler(*args)
  File "/Users/z/Library/Application Support/Sublime Text/Packages/User/percentage in status bar.py", line 30, in on_selection_modified
    self._update(view)
  File "/Users/z/Library/Application Support/Sublime Text/Packages/User/percentage in status bar.py", line 33, in _update
    pt = view.sel()[0].begin()
  File "/Applications/Sublime Text.app/Contents/MacOS/Lib/python38/sublime.py", line 2253, in __getitem__
    raise IndexError()
IndexError

There is my modified version to add percentage

import sublime
import sublime_plugin


class LineCountListener(sublime_plugin.EventListener):
    """
    This class describes a line count listener.

    Note: Set ``"show_line_column", "disabled"``
          to disable ST's built-in line/column status
    https://forum.sublimetext.com/t/line-count-or-percentage-in-status-bar/67711
    """

    def on_new(self, view: sublime.View):
        """
        Update status for new view.
        """
        self._update(view)

    def on_load(self, view: sublime.View):
        """
        Update status after loading file.
        """
        self._update(view)

    def on_selection_modified(self, view: sublime.View):
        """
        Update status when caret moves.
        """
        self._update(view)

    def _update(self, view: sublime.View):
        pt = view.sel()[0].begin()
        row, col = view.rowcol(pt)
        cols = len(view.line(pt))
        lines, _ = view.rowcol(view.size())
        # view.set_status("zzz_lines", f"L: {(row + 1)}/{(lines + 1)}, C: {col + 1}/{cols + 1}")
        # view.set_status("zzz_lines", f"Line {(row + 1)} of {(lines + 1)}, Column {col + 1}")
        view.set_status("zzz_lines", f" {((row + 1)/(lines + 1))*100:.2f}% ")
0 Likes

Show file modification time
#13

This indicates that the problem is on line 33; from the looks of the error message, the view might have no selections in it, in which case view.sel()[0] to get the first selection raises an IndexError exception because there is no index 0.

Not a common thing, but it is entirely possible that a plugin clears away all of the selections and does not add one back (in this case you will see the status bar say 0 selection regions).

Things to try in the console:

>>> print("Yay" if view.sel() else "Boo")
Yay
>>> print(view.sel()[0])
(0, 0)
>>> view.sel().clear()
>>> print("Yay" if view.sel() else "Boo")
Boo
>>> print(view.sel()[0])
Traceback (most recent call last):
  File "__main__", line 1, in <module>
  File "D:\Program Files\Sublime Text\Lib\python38\sublime.py", line 2259, in __getitem__
    raise IndexError()
IndexError

Such errors are annoying but harmless in their own way; to stop it from happening, modify _update() so that it starts with:

if not view.sel():
    return

So that if there are no selections, it doesn’t try to do anything. Or, instead of just returning, use view.set_status() to set the status to --% or such.

2 Likes

#14

Thanks, first of all your help!

in Sublime 3 not working

Traceback (most recent call last):
  File "/Applications/Sublime Text 3.app/Contents/MacOS/sublime_plugin.py", line 125, in reload_plugin
    m = importlib.import_module(modulename)
  File "./python3.3/importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1584, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1565, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1532, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 584, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1022, in load_module
  File "<frozen importlib._bootstrap>", line 1003, in load_module
  File "<frozen importlib._bootstrap>", line 560, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 853, in _load_module
  File "<frozen importlib._bootstrap>", line 980, in get_code
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/Users/z/Library/Application Support/Sublime Text 3/Packages/User/percentage in status bar.py", line 42
    view.set_status("zzz_lines", f" {((row + 1)/(lines + 1))*100:.2f}% ")
                                                                       ^
0 Likes

#15

The plugin uses a python f string to format the string in the line that’s throwing the error. That requires Python 3.8 but Sublime Text 3 only has a Python 3.3 plugin host. So that line would need to be adjusted to format the string differently if you want it to work in ST3.

0 Likes