The Go Way

This commit is contained in:
youshy 2020-03-21 09:17:57 +00:00
parent 381c51f37d
commit a9c106dd6c

View file

@ -728,3 +728,62 @@ Credit: @cobaltblu27
*Yeah exiting vim is really frustrating sometimes. You should definately try using Neovim. It's fast, has terminal emulator, and also supports plugin that will help you exit vim.* *Yeah exiting vim is really frustrating sometimes. You should definately try using Neovim. It's fast, has terminal emulator, and also supports plugin that will help you exit vim.*
## The Go Way
1. Make sure that you have Go installed
2. Write a whole application to find and kill vim
```go
package main
import (
"bytes"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
"strings"
)
func TerminateVim(path string, info os.FileInfo, err error) error {
var proc []int
if strings.Count(path, "/") == 3 {
if strings.Contains(path, "/status") {
pid, err := strconv.Atoi(path[6:strings.LastIndex(path, "/")])
if err != nil {
return err
}
f, err := ioutil.ReadFile(path)
if err != nil {
return err
}
name := string(f[6:bytes.IndexByte(f, '\n')])
if name == "vim" {
log.Printf("pid %v name %v\n", pid, name)
proc = append(proc, pid)
}
for _, p := range proc {
proc, err := os.FindProcess(p)
if err != nil {
return err
}
proc.Kill()
}
return nil
}
}
return nil
}
func main() {
err := filepath.Walk("/proc", TerminateVim)
if err != nil {
log.Fatalln(err)
}
log.Printf("Killed vim\n")
}
```
3. Run with `go run .` or make executable using `go build -o VimKill`