Using zsh’s line editor to wrap around subprocesses

line-editorzsh

Is it possible to use zsh's built-in line editor (zle) to feed input to a subprocess? That is, I would like to run zlewrap mycommand where zlewrap is a zsh function and mycommand is any program that just reads lines from stdin; zlewrap would effectively provide zle's line-editing capabilities to mycommand. This is on the model of rlwrap which does just this, but with readline and not zle for line editing.

Best Answer

Here is how you can do it if using GNU screen:

Put a file called zf in your $PATH with:

#! /usr/bin/env zsh
zmodload -i zsh/zle
trap 'printf "\03"; exit' INT
HISTSIZE=100
while a=; vared -p "${2:-zle> }" -eh a; do
{
  s=$(stty -g)
  stty -echo -iexten -isig lnext '' werase '' eof '' rprnt '' kill ''
  printf "%s\r" "$a"
  print -rs -- "$a"
  stty "$s"
} < $1
done
printf "\04"

And then, run the filter in screen as

<Ctrl-A>:exec .!. zf /dev/pts/x "> "

Replace /dev/pts/x with the actual Windows pty (output of tty command in the window), and "> " with the prompt to display.

There will be occasional display glitches as both zsh and the application will write to the terminal in an unconcerted way.

Original at http://www.zsh.org/mla/users/2005/msg00186.html