-
Notifications
You must be signed in to change notification settings - Fork 2
/
vrim
executable file
·71 lines (61 loc) · 2.16 KB
/
vrim
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
66
67
68
69
70
71
#!/usr/bin/env bash
# Drop the "--" argument if present.
# Ranger passes "--" to indicate the end of the arguments, but vim treats it
# as a filename after the --remote family of arguments.
if [ "$1" == "--" ]; then
shift
fi
# Exit if we don't know what server/session to use
if [ -z "$VRANGER_ID" ]; then
echo "VRANGER_ID not set!" >&2
echo "vrim is not meant to be invoked directly" >&2
exit 1
fi
# Exit if we don't have a VRANGER_DETACH mapping
if [ -z "$VRANGER_DETACH" ]; then
echo "VRANGER_DETACH not set!" >&2
echo "vrim is not meant to be invoked directly" >&2
exit 1
fi
REMAP="nnoremap <silent> $VRANGER_DETACH :call system(\"tmux detach -s $VRANGER_ID\")"
if [ "$VRANGER_SENDKEYS" = "1" ]; then
REMAP="$REMAP<CR>"
START_VIM_SERVER="vim -c '$REMAP' \"$@\""
else
# note the <LT> workaround to prevent literal <CR>
# being interpreted by --remote-silent
REMAP="$REMAP<LT>CR>"
# I haven't found a way to prevent vim from opening a [No Name] buffer first.
# This deletes the first buffer.
REMOVE_BLANK_BUFFER="bd1"
START_VIM_SERVER="vim --servername $VRANGER_ID --remote-silent +'$REMOVE_BLANK_BUFFER | $REMAP' \"$@\""
fi
# Check to see if tmux session is running
if ! tmux has-session -t $VRANGER_ID >/dev/null 2>&1; then
# Start vim server and containing tmux session
REATTACH=$(which reattach-to-user-namespace 2>/dev/null)
if [ -x "$REATTACH" ]; then
START_VIM_SERVER="$REATTACH $START_VIM_SERVER"
fi
exec tmux -2 new-session -s $VRANGER_ID "$START_VIM_SERVER" \; set status off \; >/dev/null 2>&1
else
# Open requested file and attach to server
if [ "$VRANGER_SENDKEYS" == "1" ]; then
if [ "$VRANGER_USE_TABS" == "1" ]; then
tmux send-keys -t $VRANGER_ID Escape
tmux send-keys -t $VRANGER_ID -l ":tabnew $@"
tmux send-keys -t $VRANGER_ID Enter
else
tmux send-keys -t $VRANGER_ID Escape
tmux send-keys -t $VRANGER_ID -l ":e $@"
tmux send-keys -t $VRANGER_ID Enter
fi
else
if [ "$VRANGER_USE_TABS" == "1" ]; then
vim --servername $VRANGER_ID --remote-tab "$@"
else
vim --servername $VRANGER_ID --remote "$@"
fi
fi
exec tmux -2 attach -t $VRANGER_ID >/dev/null 2>&1
fi