How to start vim from a site?

I have a command line tool written in Golang and I need to run vim from it. However, it does not work, and there is no mistake with it or anything else. I shortened the code to 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 see the vim process for 2-3 seconds, but the application does not actually open. Then the program just exits (and the vim process closes) with "exit status 1".

I also tried 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 is stuck endlessly.

Any idea what could be the problem?

+4
source share
2 answers

stdin stdout , , (, ), vim , .

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)
}
+12

VIM .

StderrPipe vim, :

2014/02/02 20:25:49 Vim: Warning: Output is not to a terminal
2014/02/02 20:25:49 Vim: Warning: Input is not from a terminal

stderr ( ):

func logger(pipe io.ReadCloser) {
    reader := bufio.NewReader(pipe)

    for {
        output, err := reader.ReadString('\n')

        if err != nil {
            log.Println(err)
            return
        }

        log.Print(string(output))
    }
}

pipe, err := cmd.StderrPipe()

go logger(pipe)
cmd.Run()

vim , , .

, goat (doc) :

tty := term.NewTTY(os.Stdin)

cmd := exec.Command("vim", "test.txt")
cmd.Stdin = t
cmd.Stdout = t

// ...
+7

Source: https://habr.com/ru/post/1524954/


All Articles