neovim icon indicating copy to clipboard operation
neovim copied to clipboard

":read :cmd" reads :cmd output into a buffer

Open sidesteps opened this issue 1 year ago • 15 comments
trafficstars

Problem

Vims' internal pager 'more' sucks: can't search when neovim opens output in it.

Expected behavior

ability to search when paging with neovims internal pager 'more'

sidesteps avatar Sep 14 '24 10:09 sidesteps

Duplicate/part of https://github.com/neovim/neovim/issues/22478 See also https://github.com/neovim/neovim/issues/27811

clason avatar Sep 14 '24 10:09 clason

And

For example open any help topic with :help and try searching with '/', you can't, can you?

Of course you can; :help opens a full buffer.

clason avatar Sep 14 '24 10:09 clason

See also https://github.com/neovim/neovim/issues/5054

Proposal

A short-term, high-leverage, low-cost improvement would be to add :read :<cmd> which opens any :<cmd> output into a buffer. Bram also was favorable to that idea.

Would be nice to see a PR for that... Let's use this issue to track that idea.

Implementation notes

  • One way to implement this is to re-use the internals of :help execute() : https://github.com/neovim/neovim/blob/e049c6e4c08a141c94218672e770f86f91c27a11/src/nvim/eval/funcs.c#L1592-L1594
    • Or rather than directly using those internals, the C code can invoke vimscript:
      execute(printf("put=execute('%s')", substitute(escape(<q-args>, '"')
      
  • Note also that "mods" should be supported, i.e. :split read :foo should collect the output of :foo before` splitting the window (otherwise this isn't useful).
  • A good place for tests would be test/functional/ex_cmds/ . There is currently no read_spec.lua in there, but perhaps could use test/functional/ex_cmds/write_spec.lua as a starting point.

justinmk avatar Sep 14 '24 10:09 justinmk

For the sake of completeness, we already have :read at home: :put=execute('cmd') (just missing opening a new buffer). Of course, that is hard enough to know about (and even remember) for a shortcut to be valuable. In fact, we have previously discussed adding even more sugar in the form of, say :^ (which is still free real estate, iirc) in analogy to :=.

Open question: do we want to restrict this to Lua command (which, let's be honest, is the primary use case these days)? Or keep :read for vimscript and add :^ as Lua sugar?

clason avatar Sep 14 '24 11:09 clason

The door is open for those questions, but adding :read :cmd in "vimscript" should be rather low-risk, and makes sense as a first step before the other stuff.

do we want to restrict this to Lua command

why? Ex commands for interactive command line shouldn't require (explicit use of) Lua.

justinmk avatar Sep 14 '24 11:09 justinmk

why? Ex commands for interactive command line shouldn't require (explicit use of) Lua.

Because that allows us to make the sugar sweeter, as we did for := (which is arguably the most frequent -- but of course not the only -- way to see the pager these days). The point of my comment is to make sure whatever is added also applies to :=.

clason avatar Sep 14 '24 11:09 clason

The point of my comment is to make sure whatever is added also applies to :=.

I would expect :read :=... to be supported since any :read :<cmd> should simply capture the output of whatever follows :read :.

justinmk avatar Sep 14 '24 12:09 justinmk

https://github.com/AndrewRadev/bufferize.vim does this which is just a light wrapper around :redir.

We could upstream a form of this without introducing another ex command in C.

lewis6991 avatar Sep 15 '24 08:09 lewis6991

We could upstream a form of this without introducing another ex command in C.

That's the idea. And this doesn't introduce any new Ex command, it only enhances :read in the same way that it already works for :read !....

:read is a basic feature and it absolutely doesn't make sense to introduce a new command or interface for this. :read is the interface for this kind of thing.

The impl of :read doesn't need to be in C, but that's just a detail anyway. The interface is what matters.

justinmk avatar Sep 15 '24 10:09 justinmk

Reading output of cmd-line command into a buffer is useful.

It does not solve the original issue, (which is now renamed to something completely different:))

The use case is to quickly lookup info in large, read-only info pages. Reading the output of a command into a buffer so that you can do search is not it.

Example is :highlight command. It outputs hundreds of lines that need to be searchable to be useful.

sidesteps avatar Sep 15 '24 13:09 sidesteps

If paged output of the command would open in a split window (as :help does) that would be a solution I guess.

sidesteps avatar Sep 15 '24 13:09 sidesteps

... which is exactly what is being proposed here.

clason avatar Sep 15 '24 13:09 clason

Also, I mentioned that https://github.com/neovim/neovim/issues/5054 tracks more elaborate "builtin pager" changes. That is why I renamed this issue to track the more near-term solution of enhancing :read.

justinmk avatar Sep 15 '24 14:09 justinmk

I had a go at this, but I am not sure I have the right approach. Could I get some feedback before going any further please?

https://github.com/bwalshe/neovim/blob/read_cmd_to_buffer/src/nvim/ex_docmd.c#L5643

bwalshe avatar Oct 02 '24 13:10 bwalshe

I had a go at this, but I am not sure I have the right approach. Could I get some feedback before going any further please?

https://github.com/bwalshe/neovim/blob/read_cmd_to_buffer/src/nvim/ex_docmd.c#L5643

Your best option will be to open a draft PR. People will give feedback there

tris203 avatar Oct 02 '24 17:10 tris203