-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathcopy_line.sh
executable file
·111 lines (98 loc) · 3.08 KB
/
copy_line.sh
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/usr/bin/env bash
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
HELPERS_DIR="$CURRENT_DIR"
TMUX_COPY_MODE=""
REMOTE_SHELL_WAIT_TIME="0.4"
# shellcheck source=scripts/helpers.sh
source "${HELPERS_DIR}/helpers.sh"
# sets a TMUX_COPY_MODE that is used as a global variable
get_tmux_copy_mode() {
TMUX_COPY_MODE="$(tmux show-option -gwv mode-keys)"
}
# The command when on ssh with latency. To make it work in this case too,
# sleep is added.
add_sleep_for_remote_shells() {
local pane_command
pane_command="$(tmux display-message -p '#{pane_current_command}')"
if [[ $pane_command =~ (ssh|mosh) ]]; then
sleep "$REMOTE_SHELL_WAIT_TIME"
fi
}
go_to_the_beginning_of_current_line() {
if [ "$(shell_mode)" == "emacs" ]; then
tmux send-keys 'C-a'
else
tmux send-keys 'Escape' '0'
fi
}
enter_tmux_copy_mode() {
tmux copy-mode
}
start_tmux_selection() {
if tmux_is_at_least 2.4; then
tmux send -X begin-selection
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
# vi copy mode
tmux send-keys 'Space'
else
# emacs copy mode
tmux send-keys 'C-Space'
fi
}
# works when command spans accross multiple lines
end_of_line_in_copy_mode() {
if tmux_is_at_least 2.4; then
tmux send -X -N 150 'cursor-down' # 'down' key. 'vi' mode is faster so we're
# jumping more lines than emacs.
tmux send -X 'end-of-line' # End of line (just in case we are already at the last line).
tmux send -X 'previous-word' # Beginning of the previous word.
tmux send -X 'next-word-end' # End of next word.
elif [ "$TMUX_COPY_MODE" == "vi" ]; then
# vi copy mode
# This sequence of keys consistently selects multiple lines
tmux send-keys '150' # Go to the bottom of scrollback buffer by using
tmux send-keys 'j' # 'down' key. 'vi' mode is faster so we're
# jumping more lines than emacs.
tmux send-keys '$' # End of line (just in case we are already at the last line).
tmux send-keys 'b' # Beginning of the previous word.
tmux send-keys 'e' # End of next word.
else
# emacs copy mode
for ((c = 1; c <= '30'; c++)); do # go to the bottom of scrollback buffer
tmux send-key 'C-n'
done
tmux send-keys 'C-e'
tmux send-keys 'M-b'
tmux send-keys 'M-f'
fi
}
yank_to_clipboard() {
if tmux_is_at_least 2.4; then
# shellcheck disable=SC2119
tmux send -X copy-pipe-and-cancel "$(clipboard_copy_command)"
else
tmux send-keys "$(yank_wo_newline_key)"
fi
}
go_to_the_end_of_current_line() {
if [ "$(shell_mode)" == "emacs" ]; then
tmux send-keys 'C-e'
else
tmux send-keys '$' 'a'
fi
}
yank_current_line() {
go_to_the_beginning_of_current_line
add_sleep_for_remote_shells
enter_tmux_copy_mode
start_tmux_selection
end_of_line_in_copy_mode
yank_to_clipboard
go_to_the_end_of_current_line
display_message 'Line copied to clipboard!'
}
main() {
get_tmux_copy_mode
yank_current_line
}
main