Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Open editor to create/edit stdin before feeding to a command



On Dec 1, 10:51pm, martin f krafft wrote:
}
} I recently discovered edit-command-line[0] and that made me think
} that zsh should really get a feature which would allow the user to
} use an editor to feed data to a command's stdin, basically
} a simplified here-document, e.g. with a syntax mockup:
} 
}    # opens editor on tmpfile, feeds file to wc stdin on close
} ~$ wc <<!

Unfortunately there's very little syntax you can use following "<<"
that would not potentially be in conflict with an existing here-
document.  In particular I've seen a number of scripts that use

something <<!
misc. text
!

(and so on with other punctuation).

However, you can get Awfully Darn Close (TM) to this already with a
couple of simple tricks.

For example:

    function edcat {
      emulate -L zsh
      if (( $# )); then
        ${VISUAL:-${EDITOR:-vi}} $* </dev/tty >&/dev/tty && cat $*
      else
        edcat =(:)
      fi
    }
    alias -g '@!'='<<(edcat)'

And then:

    $ wc @!

}    # puts cat output into tmpfile, opens editor, feeds tmpfile to wc
}    # on close
} ~$ cat |! wc

This one is trickier, requiring an explicit subshell to force the editor
to run synchronously.  However, it can use the same "edcat" function.

    alias -g @@='|( exec 3<&0 && edcat =(cat <&3) )|'

    $ print lookee here @@ wc

A lot of interesting things can be done with global aliases and sequences
of characters that are unlikely to need to appear unquoted otherwise.



Messages sorted by: Reverse Date, Date, Thread, Author