Cols and number

15 views
Skip to first unread message

Felipe Gasper

unread,
Nov 17, 2021, 10:01:00 AM11/17/21
to vim...@googlegroups.com
Hello,

When I set `cols=80` and enable line numbering (`number`), I lose the last few columns for code because MacVim uses them for the line numbers.

Is it possible to make the `cols` setting account for line numbering, so that I get 80 columns for editing code, regardless of whether line numbering is on (and how many columns the line numbers themselves need)?

Thank you!

Cheers,
-Felipe Gasper

Tony Mechelynck

unread,
Nov 18, 2021, 4:06:18 PM11/18/21
to vim_mac

Vim's 'columns' setting includes the line number (if any), whose width varies according to how many lines are in the file and on the 'numberwidth' setting. No matter which options you set, if you are running Vim in an 80-column-wide terminal, you won't be able to get 80 columns for the text plus some additional columns for the line number. Otherwise, if your screen is wide enough, and your Vim is compiled with +float, you can get the width of the linenumber as (untested)
    &l:nu?(max([&l:numberwidth,floor(log10(line('$')))+2])):0
floor(log10(n)) should be one less than the number of decimal digits needed to write the number n, and +2 because we add back not only that "one less" but also the single empty column between the line number and the text.

Add the value of the above expression to the desired textwidth (here 80), set 'columns' to the sum of both, then check if you got what you wanted (because setting 'columns' is limited by the actual available width: if I try to set it to 999 it will actually be set to only the number of characters that can be displayed onscreen).

Best regards,
Tony.

Felipe Gasper

unread,
Nov 18, 2021, 4:23:32 PM11/18/21
to vim...@googlegroups.com
Ah, cool!

Is there a vim event I could hook into for “when a line is added or removed”?

Thank you!

-FG

Tony Mechelynck

unread,
Nov 18, 2021, 5:14:19 PM11/18/21
to vim_mac

I don't see anything obvious. You might want to check the autocommands described at lines 372 and following of ":help autocmd.txt" (TextChanged, TextChangedI, etc.) but keep a "saved value" of the number of lines i.e. line('$') because if it didn't change you can return early. Or you might set a flag from a 'statusline' setting, but handle it elsewhere because 'statusline' is evaluated very often and for every (split) window.

Best regards,
Tony.
Reply all
Reply to author
Forward
0 new messages