-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathterminal-tab.sh
53 lines (48 loc) · 1.35 KB
/
terminal-tab.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
#!/bin/sh
#
# Open a new terminal emulator tab in the current working directory.
#
# Usage:
# ./terminal-tab.sh [-t terminal_emulator] [-w window_name] [-k keystroke]
#
# Requires xdotool:
# http://www.semicomplete.com/projects/xdotool/
#
# Copyright 2015, Sebastian Tschan
# https://blueimp.net
#
# Licensed under the MIT license:
# https://opensource.org/licenses/MIT
#
# Keyboard shortcut to open a new terminal emulator tab:
KEYSTROKE='ctrl+shift+t'
# Pattern to identify a terminal emulator window by name:
WINDOW_NAME="$USER@$(hostname)"
# Terminal emulator command:
TERMINAL_EMULATOR='x-terminal-emulator'
# Parse command-line options:
while getopts t:w:k: opt; do
case $opt in
t) TERMINAL_EMULATOR="$OPTARG"
;;
w) WINDOW_NAME="$OPTARG"
;;
k) KEYSTROKE="$OPTARG"
;;
?) echo "Usage: $0 [-t terminal_emulator] [-w window_name] [-k keystroke]"
exit 2
;;
esac
done
# Search for an open terminal emulator window and bring it into focus:
xdotool search --limit 1 --name "$WINDOW_NAME" windowactivate
# Check if xdotool found a matching window:
if [ $? -eq 0 ]; then
# Open a new terminal emulator tab in the current working directory:
xdotool key "$KEYSTROKE" \
type --delay 0 "cd '$(pwd | sed "s/'/'\\\''/g")';clear" && \
xdotool key Return
else
# Open a new terminal emulator window:
$TERMINAL_EMULATOR
fi