sharkdp / bat

A cat(1) clone with wings.
Apache License 2.0
49.1k stars 1.24k forks source link

why bat can not enter edit mode? #3051

Open qbai opened 2 months ago

qbai commented 2 months ago

when I use sudo bat a.sh then enter v, I can not enter edit mode as help document described. my env variable is echo $VISUAL nano -m -$ echo $EDITOR nano -m -$

the status bar show the message as following: can not edit standard input (press enter)

please help to see this problem, thanks a lot

keith-hall commented 2 months ago

When bat passes the text it outputs to your pager (i.e. less, which provides this edit function and corresponding error message), the pager just sees standard input, it doesn't know what underlying file to open for editing.

keith-hall commented 2 months ago

This functionality could potentially come as part of https://github.com/sharkdp/bat/issues/1053

qbai commented 2 months ago

@keith-hall thanks your feedback. and I am confused. when I run 'less -i -M -R -j20 -J a.sh' then click v key, it always enter edit mode. But when I run 'bat --pager="less -i -M -R -j20 -J" a.sh' then click v key, it failed as the above message. as you described, the error message came from less, why I can use less to edit but can not use bat to edit which called less ? is that a bug for bat that can not start less correctly? or could you tell me if you can use bat to edit one file by clicking v key in your environment?

keith-hall commented 2 months ago

When you run less directly and give it a filename, it knows what file to open in your editor. When you call bat, it is adding decorations, syntax highlighting etc and so passes the contents to less via stdin, so there is no file on disk for less to open in your editor.

is that a bug for bat that can not start less correctly?

I don't think so. You would see the same behavior with cat a.sh | less. Even if less supported passing a filename reference for editing purposes, there can be situations where bat is outputting multiple files or content piped into bat with no single underlying file to edit.

qbai commented 2 months ago

@keith-hall I know what you mean, but my question is what is meaning for bat with v shortkey. As bat's help description as following: v Edit the current file with $VISUAL or $EDITOR.

that means that when you click v key, bat should call $VISUAL or $EDITOR to edit file, just like less does. but now bat actually can not edit any file, right?

so I still think this should be a bug for bat, or be a new feature as your opinion when bat open only one file.

einfachIrgendwer0815 commented 2 months ago

The interactive output you see is provided by less and not by bat. As @keith-hall explaind, bat passes all its output to less for paging, but without file information. bat <file> is roughly the same as bat --decorations=always --color=always <file> | less -rF. bat just calls less automatically, so users don't have to specify explicitly. The scrolling, searching, shortcuts, etc. are all features of less and not bat.

1053, which I'm currently working on an implementation for, could provide such functionality even in cases with multiple files.

qbai commented 2 months ago

@einfachIrgendwer0815 ok, as one obvious issue, I suggest to add comment on bat's readme and tell users that currently bat can not transfer filename to less that less can not call $EDITOR successfully. so now bat is crippled to edit file.

einfachIrgendwer0815 commented 2 months ago

2763 is related. This comment suggests a possible workaround.