-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathi3-vim-nav.go
53 lines (46 loc) · 974 Bytes
/
i3-vim-nav.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"github.com/proxypoke/i3ipc"
"github.com/vbrown608/xdo-go"
)
func main() {
dir := string(os.Args[1])
if match, _ := regexp.MatchString(`h|j|k|l`, dir); !match {
fmt.Println("must have an argument h j k or l")
return
}
if windowIsVim() {
keycmd := exec.Command("xdotool", "key", "--clearmodifiers", "Escape+control+"+dir)
out, _ := keycmd.Output()
if len(out) > 0 {
fmt.Println(out)
}
} else {
conn, err := i3ipc.GetIPCSocket()
if err != nil {
fmt.Println("could not connect to i3")
return
}
m := make(map[string]string)
m["h"] = "left"
m["j"] = "down"
m["k"] = "up"
m["l"] = "right"
conn.Command("focus " + m[dir])
}
}
func windowIsVim() bool {
xdot := xdo.NewXdo()
window, err := xdot.GetActiveWindow()
if err != nil {
return false
}
name := strings.ToLower(window.GetName())
r, _ := regexp.Compile(`\bn?v(im)?$`)
return r.MatchString(name)
}