I'd like to configure bash to page the output of all commands. Essentially, I want bash to behave as if every command I enter ended with '| less'. Is this possible?
Asked
Active
Viewed 3,278 times
2 Answers
9
You could do this:
$ bind '"\C-j": "|less\C-m"'
Or put this in your ~/.inputrc
:
"\C-j": "|less\C-m"
Then when you want to do ls -l|less
you'd type ls -l
and press control-J
instead of <enter>
.
I would not recommend swapping the j and m in the bind command (or in the .inputrc file). Every time you'd press <enter>
you'd get |less
added which could be pretty annoying.
![](../../users/profiles/1293.webp)
Dennis Williamson
- 60,515
- 14
- 113
- 148
-
1Potentially confusing as hell, but undeniably a neat trick. – womble Jun 25 '09 at 23:30
-
Dennis, That rocks! ZSH equivalent is: bindkey -s "\C-j" "|less\C-m" – Kyle Brandt Jun 26 '09 at 11:41
-
@dennis-williamson, did you know how can I fix this binding, for queued commands? For example enter in bash "sleep 10", and don't wait when "sleep 10" finished, "ls" + enter, and for "ls" command "|less" will be added. – azat Apr 25 '13 at 20:36
-
@azat: It works for me. I tried by using the `bind` command shown above (or the `.inputrc` binding), typing `sleep 10` and immediately typing `ls -l` (don't press enter) and then press Control-J (the enter is built in to the binding as `\C-m`). – Dennis Williamson Apr 26 '13 at 14:02
-
@dennis-williamson, I'm probably wrong explained, I mean that if you run "sleep 5" press enter, after it, don't wait when "sleep 10" finished, "ls -l", and "less" will be added, while it shoudn't – azat Apr 26 '13 at 15:26
-
1@azat: Oh! To fix that bind the sequence to something other than Control-J and use that keystroke to add the `less` and carriage return to commands that you want. The reason for this problem is that Control-J is normally bound to `accept-line` as is Control-M (enter). – Dennis Williamson Apr 26 '13 at 15:40
-
@dennis-williamson, Thanks! It works. – azat Apr 26 '13 at 21:36
1
I'm not sure if this is possible, but you can definitely make it easier on yourself by doing something like this in your .bashrc:
alias p='less'
ll |p
then becomes a paged listing.
EDIT: As a note though this still wont work for something that is going to STDERR. You would need to something like: errorcommand 2>&1 |p
![](../../users/profiles/3607.webp)
moshen
- 1,534
- 1
- 9
- 13