How to start vim from go?

go, macos, process, system, vim

Solution

Pass on `stdin` and `stdout` from the calling program which, provided it was run from a terminal (likely for a command line program) will start vim for you and return control when the user has finished editing the file.

package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    cmd := exec.Command("vim", "test.txt")
    cmd.Stdin = os.Stdin
    cmd.Stdout = os.Stdout
    err := cmd.Run()
    fmt.Println(err)
}

Problem

I've got a command line tool written in Golang and I need to start vim from it. However it's not working, and there's not any error or much else to work with. I've reduced the code to just this: ``` package main import ( "fmt" "os/exec" ) func main() { cmd := exec.Command("vim", "test.txt") err := cmd.Run() fmt.Println(err) } ``` When I run this, I can see the vim process for a 2-3 seconds but the application doesn't actually open. Then the program simply exits (and the vim process closes) with an "exit status 1". I've also tried this to capture stderr: ``` package main import ( "bytes" "fmt" "os/exec" ) func main() { cmd := exec.Command("vim", "test.txt") var stderr bytes.Buffer cmd.Stderr = &stderr err := cmd.Run() fmt.Println(err) fmt.Println(stderr) } ``` But in this case, the program gets stuck indefinitely. Any idea what could be the issue?

Original source