I set up bat v0.7.0 to use a small wrapper-script for less:
~/.local/bin/bat-pager
#!/bin/bash
less --tabs 4 -RF "$@"
Using it works for any file large enough to need a pager, and I can use the scrollwheel on my mouse. So far so good.
But when I bat a short file, I see a brief flash of its contents, and then bat just exits with no output.
export BAT_PAGER=bat-pager
bat some.xml
Unsetting the BAT_PAGER environment variable means everything works as expected — except for the scrolling.
Ubuntu 18.04
Gnome Terminal with Bash
Can you please make sure that this is not an issue with less by calling just bat-pager some.xml?
The reason why we usually pass -X/--no-init in addition is - as far as I remember - that there are some weird issues when using -F alone without (-X).
Ah, -F means that less exits if the file fits on the current terminal screen. So totally expected.
Why is the use of -F documented in bat's README?
Is there a mode to let bat use a pager only when it needs one? Or is this why the wrapper script includes -F?
~With -X it all works as expected.~
With just -R less works (as BAT_PAGER as well), but bat will show short files that fit on screen with less.
Adding -F means that short files stop working, because the pager exits (as instructed by -F):
-F or --quit-if-one-screen
Causes less to automatically exit if the entire file can be displayed on the first screen.
Is bat supposed to detect less exiting and not use it in that case? Or does BAT_PAGER mean that all files (including those that need no pager) are passed to less? In the latter case, why does the documentation mention -F?
I have lost track of what the actual problem is here?
I have lost track of what the actual problem is here?
I'm not sure why you're phrasing that as a question, I'm afraid I can't answer that for you.
The issue is as reported in the first post. Following the documentation for using BAT_PAGER with a wrapper script results in bat not showing the contents of any file short enough to fit on screen.
Removing the -F flag stops less from exiting when files that are short enough to fit on screen are passed to it, but it also means that bat will show every file in a pager, instead of only those that need a pager.
Why is the -F flag mentioned in the documentation?
Is bat supposed to detect less exiting (i.e., short files that fit on screen) and not use it in that case?
Does the use of BAT_PAGER imply that all files (including short files that fit on screen that need no pager) are passed to less?
The issue is as reported in the first post. Following the documentation for using
BAT_PAGERwith a wrapper script results inbatnot showing the contents of any file short enough to fit on screen.
I can not reproduce this. Can you please make sure that this is not an issue with less by calling just bat-pager some.xml? The way -F is supposed to work (in my understanding) is that less will immediately quit the pager-mode, but still print the entire contents to the console:
> echo hello | less --tabs 4 -RF
hello
Why is the
-Fflag mentioned in the documentation?
It's just an example because I think the -F option is quite practical. You can choose to leave it out.
Is
batsupposed to detect less exiting (i.e., short files that fit on screen) and not use it in that case?
No. If bats paging-option is not explicitly disabled (via --paging=never or export BAT_PAGER=""), it will always print all its output to the pipe that connects it with the pager program. bat will never check if the file contents is large enough to fit on one screen.
Does the use of
BAT_PAGERimply that all files (including short files that fit on screen that need no pager) are passed toless?
Yes.
Related: #246
The way -F is supposed to work (in my understanding) is that less will immediately quit the pager-mode, but still print the entire contents to the console
I have tested this on Ubuntu 16.04 and 18.04, and Raspbian. From man less:
-F or --quit-if-one-screen
Causes less to automatically exit if the entire file can be dis‐
played on the first screen.
less exits, and does not display anything. This is the behaviour shown by less versions 481 and 487 at least. I expect this is the behaviour on all Linux distributions.
If -F is optional, I would omit it from the documentation, or at least mention that it will only work in some places.
Let's try to clarify things once and for all:
less option -F/--quit-if-one-screen is supposed to show the file contents on the console if the file is too small. However, older versions of less need the -X/--no-init argument in addition. Otherwise, they will clear the screen and output nothing at all. This is a bug/misbehavior. Newer versions of less (I tried 530) will work without passing -X.less which breaks mouse-scrolling if you pass the -X/--no-init option (see e.g. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=565699#10).So it looks like there are three sensible options:
--quit-if-one-screen and --no-init to less. This will ensure that the "quit if one screen" behavior works correctly for all versions of less. However, it will break mouse scrolling. This option is currently chosen by bat (as well as git).-S in addition).--quit-if-one-screen if you have a recent version of less. This will make both mouse scrolling and the "quit if one screen" feature work.Note that the --RAW-CONTROL-CHARS option always needs to be passed to less. Otherwise, colors will not work.
I hope that things are now well explained in this README section: https://github.com/sharkdp/bat#using-a-different-pager
Most helpful comment
I hope that things are now well explained in this README section: https://github.com/sharkdp/bat#using-a-different-pager