aboutsummaryrefslogtreecommitdiff
path: root/scripts/tmux_open_filename_in_vim.sh
blob: b82c8672c6426e808d2e6922b4af908d83ed4e46 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
#!/bin/bash
#
# Author: Cody Hiar
# Date: 2017-04-22
#
# Purpose: The purpose of this script is to accept a file name then find a tmux
# pane that is currently running either vim or neovim and tell that pane to open
# up the file. This script is meant to be used with a fuzzy find such as fzf so
# that you can use an external fuzzy finder but still integrate it with vim
#
# Set options:
#   e: Stop script if command fails
#   u: Stop script if unset variable is referenced
#   x: Debug, print commands as they are executed
# set -eu

# Immutable globals
readonly ARGS=( "$@" )
readonly PROGNAME=$(basename "$0")
readonly USAGE=$(cat << EOF
usage: $PROGNAME file_name

Script that will search for a tmux pane running vim and will tell vim to open a
file specified by the passed in arguement

OPTIONS:
    -h      Display help options
EOF
)

# Function for processing arguments
cmdline() {
    while getopts "h" FLAG; do
        case "$FLAG" in
            h)
                echo "$USAGE"
                exit 0
                ;;
            *)
                exit 0
                ;;
        esac
    done
}

# Main loop of program
main() {
    if [[ -z ${ARGS[0]} ]]; then
        exit
    fi
    cmdline "${ARGS[@]}"
    panes=($(tmux list-panes| awk -F: '{ print $1 }'))
    for pane in "${panes[@]}"; do
        pane_tty=$(tmux display -p -t "$pane" '#{pane_tty}')
        ps -o state= -o comm= -t "$pane_tty" \
            | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$' &> /dev/null
        if [[ "$?" == 0 ]]; then
            filename="${ARGS[0]}"
            tmux send-keys -t "$pane" ":e $filename" Enter
            tmux kill-pane
            # tmux select-pane -t "$pane"
        fi
    done
}
main