Width is wrong when using less as pager with -J option
The output from bat is one column to wide when less adds a status column to the left edge.
Setup
touch file
export BAT_PAGER
Compare
BAT_PAGER="less -R"
bat file
To
BAT_PAGER="less -JR"
bat file
That is unfortunate, but I think it will be really difficult to handle this in bat. We currently rely on having the whole terminal width for our output. If less adds 2 columns for the status bar (-J) or line numbers (`-N?), .. we have less space effectively.
Several ideas/workarounds come to my mind:
bathas a hidden--terminal-widthoption that you can use to explicitly set the width of the terminal. Iflessis adding two columns, you can subtract two forbatin order to make it work:
Unfortunately, you cannot add this toBAT_PAGER="less -RJ" bat --terminal-width=$((COLUMNS - 2)) filebats config file or use an alias, since$COLUMNSneeds to be evaluated by the shell. A shell function would work, though.- You could turn of
bats grid and also turn off character-level wrapping. This way, the output wouldn't be so bad because line wraps would be handled byless:BAT_PAGER="less -RJ" bat --style="numbers,changes,header" --wrap=never file - Maybe it would be possible to wrap
lessin a shell script that reports a terminal size of$COLUMNS - 2tobat.
I figured it wouldn't be an easy problem to fix but wanted to bring it up if others encounter it. And if there maybe was a workaround, which you presented a few. Thanks for that!
What about adding a "--terminal-width-offset" option? That way the offset could be adapted to what version of less is currently in use. Maybe it's not worth the clutter in the code since there are acceptable workarounds.
What about adding a "--terminal-width-offset" option?
I thought about that, too. We could maybe just support something like --terminal-width=+5 or --terminal-width=-2? I don't think that would be very hard to implement.
Oh, forgot to leave a reply. The ability to set an offset is awesome! Thanks for a great tool and all the feedback!
This is now available in bat 0.9.
this is a simple issue but can be a bit tricky to track, took me forever to understand what was wrong here:

I solved it by adding --pager=less -+J on ~/.config/bat/config . This overrides the $LESS envvar. Since bat --generate-config-file already suggests some less(1) options, I think it could be a nice idea to have this as a default? (in fact the other options were already in my $LESS, my bat --pager now consists of less -+J.
@melissaboiko I don't quite understand. #392 proposes another solution to this problem. And I don't know what -+J does?
Funny less(1) syntax but -+J disables the -J (or --+status-column also works), overriding the $LESS environment variable .
The J option is the one causing trouble, so if somebody has -J on their $LESS, but "less -+J" on the --pager, the latter overrides the first, and less(1) doesn't mess up the columns when called from bat.
Of course, the offset option works too, and has the advantage of keeping up the less(1) status column if the person wants that inside bat.
Currently when you generate a sample config file there are these lines:
# Uncomment the following line if you are using less version >= 551 and want to
# enable mouse scrolling support in `bat` when running inside tmux. This might
# disable text selection, unless you press shift.
#--pager="less --RAW-CONTROL-CHARS --quit-if-one-screen --mouse"
I think similar comments could be helpful here, like
# Uncomment the following line if you use the less(1) -J/--status-column option
# (either on --pager or via $LESS), to allow space for the status column:
# --terminal-width=-2
#
# (Or disable the less(1) status column in bat by adding --+status-column to --pager).
It's a bit dirty coupling but conceivably we could even peek into --pager/$PAGER/$LESS and if less -J / less --status-column is detected, do terminal width -2 by default… I mean if you're into that kind of thing.
I think similar comments could be helpful here, like […]
That sounds great!
It's a bit dirty coupling but conceivably we could even peek into --pager/$PAGER/$LESS and if less -J / less --status-column is detected, do terminal width -2 by default… I mean if you're into that kind of thing.
I kind of like this :smile:. But maybe it's a bit too much magic and would come with new problems.
I also just struggled with this issue. Took quite a while to debug. When less -J... is used as the pager for bat is the behavior always wrong? Suggestion: It could be worth having bat output a warning message to stderr when it detects 1 character column misalignment and and less -J...is being used as the pager. Though not sure if that's possible.
Once I finally figured out the problem and found this issue it was pretty easy to fix it in my bat config thanks to the handy --terminal-width="-1" option mentioned here.