terminal icon indicating copy to clipboard operation
terminal copied to clipboard

ability to pipe scrollback buffer to a pager or terminal editor like kitty

Open chiefjester opened this issue 5 years ago β€’ 8 comments

Ability to pipe scrollback buffer to a pager or terminal editor

This is basically what kitty does with ctrl-shift-h. By default it just uses less, but you can use any pager or terminal editor just as long as it understands ANSI escape codes. eg scrollback_pager nvim -c 'set ft=man' -.

I believe this is a more powerful way to mark, select, copy than introducing mark mode (#5804) and selection spec (#2840). Because it leverages all the features a terminal editor has as oppose to have arbitrary bindings that only exist in Microsoft Terminal.

Don't get me wrong, while I know I just said that this is more powerful, I'm not saying this could replace, but rather be a powerful complement to those PRs.

I guess the right approach is just dumping the whole buffer to stdin and piping it to whatever command we set in JSON configuration.

With Microsoft Terminal already supporting splits and tabs, this could totally replace tmux for me. And I do think this is a superior approach since terminal multiplexers tend to just duplicate all the escape sequences you type, which is wasteful. Having the Terminal handle your splits, and scrollback buffer is what most people use terminal multiplexer is for.

Some might say, "but what about sessions?". Tmux is an odd program since it is both a session manager and a terminal multiplexer. This solves the latter by completing the trifecta - splits, tabs, scrollback buffer.

This should complement the history size configuration. Since having a large amount of scrollback buffer could lead to slowdowns. But using vim for years, I never found a file that it couldn't handle, (I'm talking about opening gigabytes worth of log files).

Proposed technical implementation details (optional)

  1. Default binding: ctrl-shift-h
  2. Pipes the whole buffer to stdin to a default program like less 2a. (optional) We could remove all the ANSI Escape codes by default, that way you just pipe it directly to your terminal like vim -M +MANPAGER -, I have to use sed to remove them when using it in kitty.
  3. Ability to change the scrollback pager program via scrollbackPager in JSON Config.
  4. closing the scrollback pager would bring you back to where you invoked to command

chiefjester avatar Sep 26 '20 19:09 chiefjester

This sounds like a sibling of #642

skyline75489 avatar Sep 28 '20 03:09 skyline75489

@skyline75489 Oh for #642 definitely! However with one big upside on this. If we go on this route, then we can skip the saving part and not build a UI out for it. Because we can already leverage that to the external pager/editor to do saving for us. Something that most terminal users know generally.

This is a pretty powerful paradigm, to be honest. If you spend most of your time working on terminals, leveraging a TUI for your selection, marking, saving is going to feel second nature. It basically gives you superpowers. πŸ₯³

chiefjester avatar Sep 28 '20 18:09 chiefjester

I'm going to leave this open as an extension idea. It's definitely weird, and I'd hope that maybe someday the Terminals itself has all the features that you're looking for, so you don't need to rely on a 3rd party pager just to interact with the history in the Terminal 😝

zadjii-msft avatar Sep 29 '20 03:09 zadjii-msft

I see your point and I think it’s a fantastic idea. Do you happen to know other terminals that have this feature? This would require a spec before actual implementation. More reference implementation would help us understand how to accomplish it with breaking compatibility with others.

θŽ·ε– Outlook for iOShttps://aka.ms/o0ukef

skyline75489 avatar Sep 29 '20 03:09 skyline75489

Hi @skyline75489 πŸ‘‹

Do you happen to know other terminals that have this feature?

Do you mean besides kitty? (I mentioned it in the first post) So far from what I used, alacritty uses a similar feature, which is it's vi-mode. It seems to be supporting basic vi movements -> h,j,k,l page up, page down, half-page up, have page down, word movement, end movement, etc. and yank.

  1. How Kitty is doing it:
  2. And here's alacritty's implementation:

I've listed a proposed implementation:

  1. Default binding: ctrl-shift-h
  2. Pipes the whole buffer to stdin to a default program like less, or vi

But now that I think of it, we don't even need to handle exiting the pager/program, because we're just passing the buffer via stdin and opening it normally. So just these two. :v:

@zadjii-msft I hope you don't dismiss the idea just because you feel weird. Maybe you don't get how this makes Microsoft Terminal very flexible. But I hope I can do justice. Here goes.

Terminal users are mostly keyboard-driven individuals. The selection #2840 and mark mode #5804 is currently being built, was opened more than a year ago. I hope I don't hit a nerve, but I believe the one of the reason is still being "built" up to know is you have to build controls - scrolling, marking, selection, etc, which are all "basic" functions of a pager/editor, plus it has search too! :ok_hand:

The whole idea of this proposal is to leverage existing pager/editor. So you get that scrolling, marking, selection, saving, searching out of the box, and for free :moneybag:. This is extending what's already there. :partying_face:

This is similar to supporting tmux's command mode, I can't see the issue, but I believe this is planned support too? But rather than supporting one external program like tmux, this is opening the possibility use a pager/editor that supports passing text via stdin, which is basically all pagers/editors? (maybe) or a big chunk of them :smiley:

So as it currently stands, there's no way to do scrolling, yanking, saving the buffer to a text file, marking text via keyboard shortcuts. I think this proposal makes it a step closer because this drastically simplifies these features by leveraging existing external programs. Something that most terminal users are accustomed to and second nature to them.

Things we can defer or skip if the proposal gets built:

  1. #5804 - mark mode
  2. #2840 - selection
  3. #642 - saving history

And on top of that we get all the powerful features an editor has, regex, macros, searching, etc. All given to us for free. I don't know what more bang of the buck feature proposal out there that takes this many features in one go. So again, I like to iterate, this proposal simplifies things, not complicate them. :v:

chiefjester avatar Sep 30 '20 22:09 chiefjester

I think this is a super cool idea. It doesn't preclude the need for #5804, #2840 and #642, however; I rather consider it a nice extension to buffer management. Windows doesn't ship with a good pager by default (MORE.COM is not a good or useful pager), so we'd be saddling users with a default key combination that opens a hardcopy in notepad or even wordpad.

I totally see the value. I just also think it's additive, not a replacement. :smile:

DHowett avatar Sep 30 '20 23:09 DHowett

@DHowett , I don't mean to alienate none WSL Window users, but now that Microsoft is pushing hard for WSL with a full team, it safe to say, most of the terminal users would be using WSL? However, for Dos users, there a winget vim?

Given those rebuttal any chance you consider moving this out of Icebox? πŸ‘€

chiefjester avatar Oct 01 '20 13:10 chiefjester

It is possible to do with "multipleActions".

[!NOTE] hx -> Helix Editor. Other editors should work as well.

PowerShell

{
 "actions": [
   {
     "id": "User.ScrollbackEditor",
     "command": {
       "action": "multipleActions",
       "actions": [
         { "action": "selectAll" },
         { "action": "copy" },
         { "action": "scrollToBottom" },
         {
           "action": "wt",
           "commandline": "--window 0 pwsh -c \"Get-Clipboard | hx\""
         }
       ]
     }
   }
 ],
 "keybindings": [{ "id": "User.ScrollbackEditor", "keys": "alt+v" }]
}

WSL

[!NOTE] I have used win32yank.exe for accessing clipboard. So make sure it's in your WSL distro's path. And replace NixOS with your WSL profile. If it has space in the name make sure to escape it with quotes.

{
  "actions": [
    {
      "id": "User.ScrollbackEditorWSL",
      "command": {
        "action": "multipleActions",
        "actions": [
          { "action": "selectAll" },
          { "action": "copy" },
          { "action": "scrollToBottom" },
          {
            "action": "wt",
            "commandline": "--window 0 --profile NixOS bash -l -c \"win32yank.exe -o | hx\""
          }
        ]
      }
    }
  ],
  "keybindings": [{ "id": "User.ScrollbackEditorWSL", "keys": "alt+v" }]
}

SymphonySimper avatar Nov 25 '25 09:11 SymphonySimper