@@ -26,7 +26,8 @@ impl OutputType {
27 /// Try to launch the pager. Fall back to stdout in case of errors.
27 /// Try to launch the pager. Fall back to stdout in case of errors.
28 fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
28 fn try_pager(quit_if_one_screen: bool, pager_from_config: Option<&str>) -> Result<Self> {
29− let pager_from_env = env::var("BAT_PAGER").or_else(|_| env::var("PAGER"));
30+ env::var("BAT_PAGER").or_else(|_| env::var("PAGER").map(add_default_flags_to_less));
31 let pager = pager_from_config
32 let pager = pager_from_config
32 .map(|p| p.to_string())
33 .map(|p| p.to_string())
@@ -87,6 +88,28 @@ impl OutputType {
91+// Since someone could set PAGER to less without arg -R that bat needs,
92+// ignore flags for less and change flags to -FRX.
93+// If a user wants an env variable with flags unchanged, he/she should use
95+fn add_default_flags_to_less(pager: String) -> String {
96+ if let Some(pager_name) = shell_words::split(&pager)
99+ .and_then(|flags| flags.first())
100+ .and_then(|pager_path| {
101+ let path_buf = PathBuf::from(pager_path);
102+ path_buf.file_stem().map(|os_str| os_str.to_os_string())
105+ if pager_name == OsString::from("less") {
106+ return "less -FRX".to_string();
90impl Drop for OutputType {
113impl Drop for OutputType {
91 fn drop(&mut self) {
114 fn drop(&mut self) {
92 if let OutputType::Pager(ref mut command) = *self {
115 if let OutputType::Pager(ref mut command) = *self {