1

I often find myself piping a command's output into less because less is far superior for studying the output than the normal terminal.

While I can always append | less to every command, I was wondering if there is a more concise way of doing this requiring fewer keystrokes.

One option would be to alias less allowing me to type |L instead of |less.

Is there a smarter way? Like binding | less to a certain key combination?

I use fish but answers for zsh and bash are equally welcome, though more generic ones are preferred.

3
  • 1
    Similar: Filter by default Aug 11, 2021 at 16:20
  • I use alias p=less myself (mnemonic: p for "pager") -- p is close to the pipe on the keyboard. Aug 12, 2021 at 1:42
  • 1
    @glennjackman, FWIW, on a UK PC keyboard, The P and | (Shift+\ ) keys couldn't be further apart. Aug 12, 2021 at 8:53

2 Answers 2

7

For this specific thing: Press alt+p. Fish binds that to add &| less by default.

5
  • On my system, pressing Alt+p just outputs the character π. I'm connecting to my system via iTerm2 on macOS. Using Esc in place of Alt seems to work though.
    – Kusalananda
    Aug 12, 2021 at 10:25
  • 2
    That's because your iTerm is configured to do special output. Escape works in place of alt because alt is escape. The normal thing for terminals to do is to send escape+key when you press alt+key. Pressing escape in this manner only works up to a timeout, because fish has to determine if you meant escape on its own, and the only way to do it is to wait. Other programs have similar solutions.
    – faho
    Aug 12, 2021 at 10:27
  • @Kusalananda: You need to make option behave as meta: 1. Command + O to open the iTerm2 profiles 2. Click Edit Profiles 3. Choose left option key as +Esc or both left and right option key on the bottom right corner 4. Close the iTerm2 Profiles Source: blog.m157q.tw/posts/2014/07/09/… Aug 12, 2021 at 15:50
  • Woah :D Fish is amazing. Always got another goody somewhere by default. Aug 12, 2021 at 15:51
  • Also note that Fish will use the PAGER variable, if set, so you can use bat (bat) if you'd like. Aug 15, 2021 at 1:39
3

One way is a custom key binding. Example:

bind \el "commandline --insert '| less'"

now pressing alt-L or option-L will insert | less at your cursor.

3
  • Sounds like this would also be a good use-case for global abbreviations (#5003) when implemented. Aug 11, 2021 at 19:32
  • Great answer! What do you need to add so that it's automatically executed - meaning I don't have to hit enter? Aug 11, 2021 at 20:01
  • May be better to wrap the <command> in begin <command>; end | less (or (<command>)|less in zsh) and avoid that wrapping being added to the history. Aug 12, 2021 at 8:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .