英文:
Can not call `vim` within go code
问题
我试图在Go程序中调用vim
,代码类似于这样:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
err := exec.Command("vim", "a.txt").Run()
if err != nil {
fmt.Println(err)
}
os.Exit(0)
}
我运行了go run mycode.go
然后得到了:
exit status 1
我尝试了几种方法来成功实现这个,例如用Start()
、Output()
等替换Run()
,但似乎都不起作用。最后,我想做的是调用vim
并停止当前的Go程序。我只想看到vim
出现,仅此而已。
英文:
I try to call vim
within go program, which code similar to this:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
err := exec.Command("vim", "a.txt").Run()
if err != nil {
fmt.Println(err)
}
os.Exit(0)
}
I ran go run mycode.go
then got:
exit status 1
I have tried several ways to succeed this e.g. replace Run()
by Start()
, Output()
, ...
, but it seems not work. Finally, What I try to do is I try to call vim
and stop my current go program. I just want to see vim
appear, that's all.
答案1
得分: 7
为了使vim能够渲染其界面,您需要将标准输入/输出流附加到进程中:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "a.txt")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
os.Exit(0)
}
不附加流类似于从shell中运行以下命令:
vim < /dev/null > /dev/null 2> /dev/null
英文:
In order for vim to render its interface, you need to attach the standard input/output streams to the process:
package main
import (
"fmt"
"os"
"os/exec"
)
func main() {
cmd := exec.Command("vim", "a.txt")
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
fmt.Println(err)
}
os.Exit(0)
}
Not attaching the streams is similar to running the following command from your shell:
vim < /dev/null > /dev/null 2> /dev/null
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论