Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: allow launching editor when viewing stdin #70

Closed
khollbach opened this issue Jun 13, 2020 · 6 comments
Closed

Feature request: allow launching editor when viewing stdin #70

khollbach opened this issue Jun 13, 2020 · 6 comments

Comments

@khollbach
Copy link

Pressing v gives me an editor with the current file and line number. I love this feature, and I'd like to be able to launch an editor this way even when I'm paging through stdin.

My use-case is syntax-highlighting via piping the output of another command to less -R. This works great, but I lose ability to edit the file with v. I can almost fix this by setting the filename in an environment variable and customizing LESSEDIT="%E ?lm+%lm. ?f%f:$less_stdin_filename.". This doesn't quite work, because less stops short of invoking the editor, with the message Cannot edit standard input (press RETURN), which is reasonable enough.

A possible solution could be to introduce a commandline flag --launch-editor-stdin (or --stdin-allow-editor or something) that turns off this error message. A bit clunky maybe, but if this is a patch you'd accept, I'd be happy to work on it.

@gwsw
Copy link
Owner

gwsw commented Jun 14, 2020

I don't quite understand your LESSEDIT setting -- what is $less_stdin_name?

In any case, perhaps using a new command for this would work better, as discussed here:
#66

@khollbach
Copy link
Author

khollbach commented Jun 23, 2020

Sorry for the late reply. The solution you linked almost does what I want, except that would give me a new buffer with the file's contents; I would rather be editing the file itself so that my changes are saved.

I didn't explain my example all that well. Here's the exact use-case, where bat is basically cat with syntax-highlighting. The LESSEDIT says "if %f (current file name) exists, use that; otherwise use the environment variable less_stdin_filename".

LESSEDIT='%E ?lm+%lm. ?f%f:$less_stdin_filename.'

# E.g.: view filename.c
function view {
    less_stdin_filename="$1"
    bat "$@" | less -R
}

I would use this to view a file, and pressing v would let me edit the file. It almost works, but as I mentioned, the "Cannot edit standard input" error message is displayed before my LESSEDIT string is evaluated. Adding a commandline flag to always evaluate LESSEDIT is the only thing I could think of, but maybe there's another way to get the behaviour I want.

@gwsw
Copy link
Owner

gwsw commented Jun 24, 2020

Ok, I see what you're trying to do now. I think using LESSOPEN to preprocess the file would be a better approach. Something like

function view {
    LESSOPEN='|bat %s' less -R "$1"
}

This should allow you to use the v command to edit the original file.

@khollbach
Copy link
Author

Perfect! This does exactly what I want, I just had to add --color=always to bat. Thank you for your help :)

One minor question: is there a way to suppress the WARNING: This file was viewed via LESSOPEN (press RETURN) prompt that appears before opening the editor?

@gwsw
Copy link
Owner

gwsw commented Jul 3, 2020

The message cannot be suppressed. It's a warning that what you see in the editor may not match what you were viewing in less, because of the LESSOPEN processing.

@khollbach
Copy link
Author

Ok, no worries; thanks again for the help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants