Skip to content

Add support to show and hide line numbers.#389

Closed
jerubball wants to merge 5 commits intoasottile:mainfrom
jerubball:main
Closed

Add support to show and hide line numbers.#389
jerubball wants to merge 5 commits intoasottile:mainfrom
jerubball:main

Conversation

@jerubball
Copy link
Copy Markdown

Adding support to show line number using command line option or keyboard shortcut.

I use babi for my Windows machines, where installing nano would take more than 5 minutes of my time.
Because I am used to seeing line number in nano and VS code, I decided spend 5+ hours of my time to add this feature.

I did see the issue #239, but still I want to give a shot with this pull request.
By default, line number will not be shown, and it will only come to effect when user requests it via command line option or keyboard shortcut.
After all, I am too lazy to press ^C each time to know where I am.


Like GNU nano, I have added -l and --linenumbers command-line flag to enable line number.
Also, M-# (Alt + Shift + 3) can be pressed to toggle the display in-editor.
The behavior of displayed number is also made to match that of nano.


On some notes on what changes are made,

The main fix was done in File.draw() method in babi/file.py.
Line number is printed first, then rest of line buffer is printed.

To have cursor to appear in the correct place (shifted right), Screen.layout.file dimension needed to be updated.
Apparently, no parts of code heavily relied on Screen.layout.file.x being zero, and changing layout.file.x and layout.file.width worked surprisingly well, so I utilized this to keep track of screen offset of file layout area.

This would mean that layout needed to be updated time to time as edits are happening, therefore Screen.update_layout() method is created to expose Screen._layout_from_current_screen().

And finally, keyboard shortcut was added to babi/screen.py, and command line option was added to babi/main.py.


These fixes were good enough to make everything work well.
I confirmed that all existing features including horizontal scrolling, highlight, search, replace, undo, redo, cut, uncut, switching to prev/next files are working properly as intended in both on and off state.

Please review, and see if you like it.

Regardless, thank you for your work on babi editor. It made my life so easier when I ssh into Windows machines.

@asottile
Copy link
Copy Markdown
Owner

thanks for the patch but yeah I do not want line numbers

@asottile asottile closed this Dec 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants