zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
47.98k stars 2.83k forks source link

Support for reading from stdin #4770

Open tuladhar opened 1 year ago

tuladhar commented 1 year ago

Check for existing issues

Describe the feature

Have - flag in zed CLI which allows reading from stdin:

Example:

ps auxf | zed -

Currently, zed - just opens the editor with - as filename.

If applicable, add mockups / screenshots to help present your vision of the feature

No response

indeyets commented 5 months ago

strictly speaking, - flag is not a requirement. I know that TextMate's mate cli command detects stdin automagically and does the right thing without additional flags

dzuelke commented 2 months ago

I use this so frequently, e.g. with git diff | mate or similar, this would be great to have.

Unfortunately, zed can somehow not open anonymous FDs, so this workaround:

$ pzed() { cat <(cat); }        
$ echo "foo" | pzed     
foo

... does not work:

$ pzed() { zed <(cat); }
$ echo "foo" | pzed     
error opening PathLikeWithPosition { path_like: "/dev/fd/11", row: None, column: None }: failed to read path after update

The only way I've found is to copy to a temp file first, like so:

$ pzed() { local fn=$(mktemp); cat >"$fn"; zed "$fn"; }

This can be turned into a script that dynamically detects stdin and adds it as an argument (works both in Bash and Zsh):

zed() {
  local args=("$@")
  if [[ ! -t 0 ]]; then
    local tmp=$(mktemp)
    cat >"$tmp"
    args+=("$tmp")
  fi
  command zed "${args[@]}"
}

But it's not ideal with the tempfile creation etc.

funivan commented 1 month ago

I have the same issue. My workaround:

alias to_zed='(cat > /tmp/zed.tmp.txt && zed /tmp/zed.tmp.txt)'
dzuelke commented 1 week ago

Just a note that the failed to read path after update error when trying to read from a /dev/fd/… file originates in the worktree crate: https://github.com/zed-industries/zed/blob/c83d007138587d832c5ce3a13c8cb99913c016be/crates/worktree/src/worktree.rs#L1717