Context: I am trying to write a small tool in Golang, which spawns a vim
editor and tries to record all the keystrokes input by the user.
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "test.txt")
rdr := bufio.NewReader(os.Stdin)
cmd.Stdin = rdr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Println("err", err)
return
}
}
The above piece of code works as expected, but I want to record the keystrokes into a temporary buffer, for this I am using an io.TeeReader
which writes the os.Stdin
contents into a bytes.Buffer
. This seems to break the tty, vim starts behaving weirdly and doesn't differentiate between the insert mode and command mode. Also The Read
method returned by the io.TeeReader
hangs up and doesn't seem to return after vim
is exited.
Example code:
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "test.txt")
vimBuff := bytes.Buffer{}
rdr := io.TeeReader(os.Stdin, &vimBuff)
cmd.Stdin = rdr
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
fmt.Println("err", err)
return
}
}