Merge pull request #180 from youshy/master

Added "The Go Way"
This commit is contained in:
Luke Stephens (hakluke) 2020-04-02 01:20:37 +10:00 committed by GitHub
commit 995441cd2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -728,3 +728,64 @@ 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.*
## The Go Way
Credit: @youshy
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`