2019-02-26 09:08:57 +00:00
|
|
|
#!/bin/sh
|
|
|
|
#
|
|
|
|
# Attach to a tmux session, or create it if it doesnt exist
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
if [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
|
2019-07-27 12:29:47 +00:00
|
|
|
cat <<-EOF
|
2019-07-10 10:19:02 +00:00
|
|
|
Usage: $0 [FILE] [NAME]
|
|
|
|
Attach to an existing tmux session, or bootstrap a new session.
|
|
|
|
|
|
|
|
If no session exists which fits the criteria, this will create a new one and
|
|
|
|
load it up obeying the commands contained in the FILE argument passed in. It
|
|
|
|
must contain commands which are tmux-command compatible since they will be
|
2019-07-10 11:21:44 +00:00
|
|
|
passed to tmux unmodified. If a session exists and a valid configuration FILE is
|
|
|
|
passed, it will attach itself to the session and execute the session setup inside.
|
2019-07-10 10:19:02 +00:00
|
|
|
|
|
|
|
It will load the default tmux.conf in the following order:
|
|
|
|
- XDG_CONFIG_HOME/tmux/tmux.conf (usually ~/.config/tmux/tmux.conf)
|
|
|
|
- ~/.tmux.conf
|
|
|
|
- /etc/tmux.conf
|
|
|
|
Afterwards, it will apply the contents of the FILE argument passed into the command.
|
|
|
|
|
|
|
|
If no FILE argument is passed it will create an empty session, or attach itself to
|
|
|
|
the session specified.
|
|
|
|
|
|
|
|
If no NAME argument is passed it will automatically create a session name from the
|
|
|
|
FILE argument passed.
|
2019-07-10 11:21:44 +00:00
|
|
|
|
|
|
|
If creating a new session without passing in a FILE argument, and the current directory
|
|
|
|
contains a .tmux.session file, it will automatically use that to set up the session.
|
|
|
|
|
|
|
|
By default, it also looks for valid session files in XDG_CONFIG_HOME/tmux/sessions/
|
|
|
|
This path can be changed by setting the TM_SESSION_PATH environment variable.
|
2019-07-10 10:19:02 +00:00
|
|
|
EOF
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Make tmux obey the XDG specification on startup
|
|
|
|
if [ -e "$XDG_CONFIG_HOME/tmux/tmux.conf" ]; then
|
|
|
|
start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
|
2019-02-26 09:08:57 +00:00
|
|
|
else
|
2019-07-10 10:19:02 +00:00
|
|
|
start=tmux
|
2019-02-26 09:08:57 +00:00
|
|
|
fi
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
_file_exists() {
|
|
|
|
if [ -f "$1" ]; then
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
fi
|
|
|
|
}
|
2019-02-26 09:08:57 +00:00
|
|
|
|
|
|
|
_not_in_tmux() {
|
|
|
|
[ -z "$TMUX" ]
|
|
|
|
}
|
|
|
|
|
|
|
|
_session_exists() {
|
2019-07-27 12:29:47 +00:00
|
|
|
if tmux has-session -t "$session_name" >/dev/null 2>&1; then
|
2019-07-10 10:19:02 +00:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
fi
|
2019-02-26 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_create_detached_session() {
|
2019-07-10 10:19:02 +00:00
|
|
|
($start new-session -d -s "$session_name")
|
|
|
|
}
|
|
|
|
|
2019-07-10 11:21:44 +00:00
|
|
|
_load_env_session_file() {
|
2019-07-10 11:48:44 +00:00
|
|
|
if [ -f ./.tmux.session ]; then
|
2019-07-27 12:29:47 +00:00
|
|
|
xargs -L1 tmux <./.tmux.session
|
2019-07-10 11:48:44 +00:00
|
|
|
fi
|
2019-07-10 11:21:44 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
_load_session_file() {
|
2019-07-10 11:48:44 +00:00
|
|
|
if [ -f "$session_file" ]; then
|
2019-07-27 12:29:47 +00:00
|
|
|
xargs -L1 tmux <"$session_file"
|
2019-07-10 11:48:44 +00:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
_set_session_file_path() {
|
|
|
|
# Prefer local dir if file exists there, fall back to root dir
|
|
|
|
if _file_exists "${1}"; then
|
|
|
|
session_file="${1}"
|
|
|
|
elif _file_exists "${session_dir}${1}"; then
|
|
|
|
session_file="${session_dir}${1}"
|
|
|
|
fi
|
2019-02-26 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
_set_session_vars() {
|
2019-07-10 11:48:44 +00:00
|
|
|
session_dir=${TM_SESSION_DIR:-"$XDG_CONFIG_HOME/tmux/sessions/"}
|
|
|
|
|
|
|
|
# set up session name and config file - if passed in as arguments
|
2019-07-10 10:19:02 +00:00
|
|
|
if [ "$#" -eq 2 ]; then
|
2019-07-10 11:48:44 +00:00
|
|
|
if ! _file_exists "$1" && ! _file_exists "${session_dir}${1}"; then
|
2019-07-27 12:29:47 +00:00
|
|
|
echo >&2 "can not source session file: '${1}' does not exist."
|
2019-07-10 10:19:02 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
2019-07-10 11:48:44 +00:00
|
|
|
_set_session_file_path "$1"
|
2019-07-10 10:19:02 +00:00
|
|
|
session_name=${2}
|
|
|
|
elif [ "$#" -eq 1 ]; then
|
2019-07-10 11:48:44 +00:00
|
|
|
_set_session_file_path "$1"
|
|
|
|
if ! _file_exists "$session_file"; then
|
2019-07-10 10:19:02 +00:00
|
|
|
session_name=${1}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# set default session name if none was passed
|
|
|
|
if [ -z "$session_name" ]; then
|
2019-07-27 12:29:47 +00:00
|
|
|
# only if we have a filename should we append it to the session name
|
|
|
|
if [ -n "$session_file" ]; then
|
|
|
|
fname=/$(basename "$session_file" .session)
|
|
|
|
fi
|
|
|
|
# default to parent directory name / config file name for the session
|
|
|
|
session_name=$(basename "$PWD" | sed 's/[^a-zA-Z0-9\-]//g' | tr . -)"$fname"
|
2019-07-10 10:19:02 +00:00
|
|
|
fi
|
2019-02-26 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_attach_or_create() {
|
2019-07-10 10:19:02 +00:00
|
|
|
# first create a new session if one doesn't exist
|
|
|
|
if ! _session_exists; then
|
|
|
|
_create_detached_session
|
2019-07-10 11:21:44 +00:00
|
|
|
# look for .tmux.session file if no file has been explicitly passed in
|
2020-02-04 09:00:05 +00:00
|
|
|
# session files always take precedence over environment files when specifically passed in
|
2019-07-10 11:21:44 +00:00
|
|
|
if ! _file_exists "$session_file"; then
|
|
|
|
_load_env_session_file
|
|
|
|
fi
|
2019-07-10 10:19:02 +00:00
|
|
|
fi
|
|
|
|
|
2019-07-10 11:09:17 +00:00
|
|
|
_load_session_file
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
# then attach or switch to the session
|
2019-02-26 09:08:57 +00:00
|
|
|
if _not_in_tmux; then
|
2019-07-10 10:19:02 +00:00
|
|
|
$start attach -t "$session_name"
|
2019-02-26 09:08:57 +00:00
|
|
|
else
|
|
|
|
$start switch-client -t "$session_name"
|
|
|
|
fi
|
2019-07-10 10:19:02 +00:00
|
|
|
|
|
|
|
_unset_functions
|
|
|
|
}
|
|
|
|
|
|
|
|
_unset_functions() {
|
|
|
|
unset _set_session_vars
|
2019-07-10 11:48:44 +00:00
|
|
|
unset _set_session_file_path
|
2019-07-10 10:19:02 +00:00
|
|
|
unset _attach_or_create
|
|
|
|
unset _load_session_file
|
|
|
|
unset _create_detached_session
|
|
|
|
unset _session_exists
|
|
|
|
unset _not_in_tmux
|
|
|
|
unset _file_exists
|
2019-02-26 09:08:57 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:19:02 +00:00
|
|
|
_set_session_vars "$@"
|
2019-02-26 09:08:57 +00:00
|
|
|
_attach_or_create
|
2019-07-10 10:19:02 +00:00
|
|
|
exit 0
|