Добавлены конфиги tmux и zsh

This commit is contained in:
eKa
2019-09-18 00:17:47 +05:00
commit 7d0a7691c6
882 changed files with 67305 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
Copyright (C) Bruno Sutic
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -0,0 +1,98 @@
# tmux-continuum
Features:
- continuous saving of tmux environment
- automatic tmux start when computer/server is turned on
- automatic restore when tmux is started
Together these features enable uninterrupted tmux usage. No matter the computer
or server restarts, if the machine is on, tmux will be there how you left it off
the last time it was used.
Tested and working on Linux, OSX and Cygwin.
#### Continuous saving
Tmux environment will be saved at the interval of 15 minutes. All the saving
happens in the background without the impact to your workflow.
This action starts automatically when the plugin is installed.
#### Automatic tmux start
Tmux is automatically started after the computer/server is turned on.
See the [instructions](docs/automatic_start.md) how to enable this for your
system.
#### Automatic restore
Last saved environment is automatically restored when tmux is started.
Put `set -g @continuum-restore 'on'` in `.tmux.conf` to enable this.
Note: automatic restore happens **exclusively** on tmux server start. No other
action (e.g. sourcing `.tmux.conf`) triggers this.
#### Dependencies
`tmux 1.9` or higher, `bash`,
[tmux-resurrect](https://github.com/tmux-plugins/tmux-resurrect) plugin.
### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended)
Please make sure you have
[tmux-resurrect](https://github.com/tmux-plugins/tmux-resurrect) installed.
Add plugin to the list of TPM plugins in `.tmux.conf`:
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'tmux-plugins/tmux-continuum'
Hit `prefix + I` to fetch the plugin and source it. The plugin will
automatically start "working" in the background, no action required.
### Manual Installation
Please make sure you have
[tmux-resurrect](https://github.com/tmux-plugins/tmux-resurrect) installed.
Clone the repo:
$ git clone https://github.com/tmux-plugins/tmux-continuum ~/clone/path
Add this line to the bottom of `.tmux.conf`:
run-shell ~/clone/path/continuum.tmux
Reload TMUX environment with: `$ tmux source-file ~/.tmux.conf`
The plugin will automatically start "working" in the background, no action
required.
### Docs
- [frequently asked questions](docs/faq.md)
- [behavior when running multiple tmux servers](docs/multiple_tmux_servers.md) -
this doc is safe to skip, but you might want to read it if you're using tmux
with `-L` or `-S` flags
- [automatically start tmux after the computer is turned on](docs/automatic_start.md)
- [continuum status in tmux status line](docs/continuum_status.md)
### Other goodies
- [tmux-copycat](https://github.com/tmux-plugins/tmux-copycat) - a plugin for
regex searches in tmux and fast match selection
- [tmux-yank](https://github.com/tmux-plugins/tmux-yank) - enables copying
highlighted text to system clipboard
- [tmux-open](https://github.com/tmux-plugins/tmux-open) - a plugin for quickly
opening highlighted file or a url
### Reporting bugs and contributing
Both contributing and bug reports are welcome. Please check out
[contributing guidelines](CONTRIBUTING.md).
### License
[MIT](LICENSE.md)

View File

@@ -0,0 +1,87 @@
#!/usr/bin/env bash
set -x
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
source "$CURRENT_DIR/scripts/variables.sh"
source "$CURRENT_DIR/scripts/shared.sh"
save_command_interpolation="#($CURRENT_DIR/scripts/continuum_save.sh)"
supported_tmux_version_ok() {
"$CURRENT_DIR/scripts/check_tmux_version.sh" "$SUPPORTED_VERSION"
}
handle_tmux_automatic_start() {
"$CURRENT_DIR/scripts/handle_tmux_automatic_start.sh"
}
another_tmux_server_running() {
if just_started_tmux_server; then
another_tmux_server_running_on_startup
else
# script loaded after tmux server start can have multiple clients attached
[ "$(number_tmux_processes_except_current_server)" -gt "$(number_current_server_client_processes)" ]
fi
}
delay_saving_environment_on_first_plugin_load() {
if [ -z "$(get_tmux_option "$last_auto_save_option" "")" ]; then
# last save option not set, this is first time plugin load
set_last_save_timestamp
fi
}
add_resurrect_save_interpolation() {
local status_right_value="$(get_tmux_option "status-right" "")"
# check interpolation not already added
if ! [[ "$status_right_value" == *"$save_command_interpolation"* ]]; then
local new_value="${save_command_interpolation}${status_right_value}"
set_tmux_option "status-right" "$new_value"
fi
}
just_started_tmux_server() {
local tmux_start_time
tmux_start_time="$(tmux display-message -p -F '#{start_time}')"
[ "$tmux_start_time" == "" ] || [ "$tmux_start_time" -gt "$(($(date +%s)-10))" ]
}
start_auto_restore_in_background() {
"$CURRENT_DIR/scripts/continuum_restore.sh" &
}
update_tmux_option() {
local option="$1"
local option_value="$(get_tmux_option "$option")"
# replace interpolation string with a script to execute
local new_option_value="${option_value/$status_interpolation_string/$status_script}"
set_tmux_option "$option" "$new_option_value"
}
main() {
if supported_tmux_version_ok; then
handle_tmux_automatic_start
# Advanced edge case handling: start auto-saving only if this is the
# only tmux server. We don't want saved files from more environments to
# overwrite each other.
if ! another_tmux_server_running; then
# give user a chance to restore previously saved session
delay_saving_environment_on_first_plugin_load
add_resurrect_save_interpolation
fi
if just_started_tmux_server; then
start_auto_restore_in_background
fi
# Put "#{continuum_status}" interpolation in status-right or
# status-left tmux option to get current tmux continuum status.
update_tmux_option "status-right"
update_tmux_option "status-left"
fi
}
main

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
VERSION="$1"
UNSUPPORTED_MSG="$2"
# this is used to get "clean" integer version number. Examples:
# `tmux 1.9` => `19`
# `1.9a` => `19`
get_digits_from_string() {
local string="$1"
local only_digits="$(echo "$string" | tr -dC '[:digit:]')"
echo "$only_digits"
}
tmux_version_int() {
local tmux_version_string=$(tmux -V)
echo "$(get_digits_from_string "$tmux_version_string")"
}
exit_if_unsupported_version() {
local current_version="$1"
local supported_version="$2"
if [ "$current_version" -lt "$supported_version" ]; then
exit 1
fi
}
main() {
local supported_version_int="$(get_digits_from_string "$VERSION")"
local current_version_int="$(tmux_version_int)"
exit_if_unsupported_version "$current_version_int" "$supported_version_int"
}
main

View File

@@ -0,0 +1,29 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
auto_restore_enabled() {
local auto_restore_value="$(get_tmux_option "$auto_restore_option" "$auto_restore_default")"
[ "$auto_restore_value" == "on" ] && [ ! -f "$auto_restore_halt_file" ]
}
fetch_and_run_tmux_resurrect_restore_script() {
# give tmux some time to start and source all the plugins
sleep 1
local resurrect_restore_script_path="$(get_tmux_option "$resurrect_restore_path_option" "")"
if [ -n "$resurrect_restore_script_path" ]; then
"$resurrect_restore_script_path"
fi
}
main() {
# Advanced edge case handling: auto restore only if this is the only tmux
# server. If another tmux server exists, it is assumed auto-restore is not wanted.
if auto_restore_enabled && ! another_tmux_server_running_on_startup; then
fetch_and_run_tmux_resurrect_restore_script
fi
}
main

View File

@@ -0,0 +1,60 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
source "$CURRENT_DIR/shared.sh"
supported_tmux_version_ok() {
"$CURRENT_DIR/check_tmux_version.sh" "$SUPPORTED_VERSION"
}
get_interval() {
get_tmux_option "$auto_save_interval_option" "$auto_save_interval_default"
}
auto_save_not_disabled() {
[ "$(get_interval)" -gt 0 ]
}
enough_time_since_last_run_passed() {
local last_saved_timestamp="$(get_tmux_option "$last_auto_save_option" "0")"
local interval_minutes="$(get_interval)"
local interval_seconds="$((interval_minutes * 60))"
local next_run="$((last_saved_timestamp + $interval_seconds))"
[ "$(current_timestamp)" -ge "$next_run" ]
}
fetch_and_run_tmux_resurrect_save_script() {
local resurrect_save_script_path="$(get_tmux_option "$resurrect_save_path_option" "")"
if [ -n "$resurrect_save_script_path" ]; then
"$resurrect_save_script_path" "quiet" >/dev/null 2>&1 &
set_last_save_timestamp
fi
}
acquire_lock() {
# Sometimes tmux starts multiple saves in parallel. We want only one
# save to be running, otherwise we can get corrupted saved state.
local lockdir_prefix="/tmp/tmux-continuum-$(current_tmux_server_pid)-lock-"
# The following implements a lock that auto-expires after 100...200s.
local lock_generation=$((`date +%s` / 100))
local lockdir1="${lockdir_prefix}${lock_generation}"
local lockdir2="${lockdir_prefix}$(($lock_generation + 1))"
if mkdir "$lockdir1"; then
trap "rmdir "$lockdir1"" EXIT
if mkdir "$lockdir2"; then
trap "rmdir "$lockdir1" "$lockdir2"" EXIT
return 0
fi
fi
return 1 # Someone else has the lock.
}
main() {
if supported_tmux_version_ok && auto_save_not_disabled && enough_time_since_last_run_passed && acquire_lock; then
fetch_and_run_tmux_resurrect_save_script
fi
}
main

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
print_status() {
local save_int="$(get_tmux_option "$auto_save_interval_option")"
local status=""
local style_wrap
if [ $save_int -gt 0 ]; then
style_wrap="$(get_tmux_option "$status_on_style_wrap_option" "")"
status="$save_int"
else
style_wrap="$(get_tmux_option "$status_off_style_wrap_option" "")"
status="off"
fi
if [ -n "$style_wrap" ]; then
status="${style_wrap/$status_wrap_string/$status}"
fi
echo "$status"
}
print_status

View File

@@ -0,0 +1,36 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
source "$CURRENT_DIR/variables.sh"
is_tmux_automatic_start_enabled() {
local auto_start_value="$(get_tmux_option "$auto_start_option" "$auto_start_default")"
[ "$auto_start_value" == "on" ]
}
is_osx() {
[ $(uname) == "Darwin" ]
}
is_systemd() {
[ $(ps -o comm= -p1) == 'systemd' ]
}
main() {
if is_tmux_automatic_start_enabled; then
if is_osx; then
"$CURRENT_DIR/handle_tmux_automatic_start/osx_enable.sh"
elif is_systemd; then
"$CURRENT_DIR/handle_tmux_automatic_start/systemd_enable.sh"
fi
else
if is_osx; then
"$CURRENT_DIR/handle_tmux_automatic_start/osx_disable.sh"
elif is_systemd; then
"$CURRENT_DIR/handle_tmux_automatic_start/systemd_disable.sh"
fi
fi
}
main

View File

@@ -0,0 +1 @@
docs/automatic_start.md

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/../variables.sh"
main() {
rm "$osx_auto_start_file_path" > /dev/null 2>&1
}
main

View File

@@ -0,0 +1,66 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/../helpers.sh"
source "$CURRENT_DIR/../variables.sh"
template() {
local tmux_start_script="$1"
local is_fullscreen="$2"
local fullscreen_tag=""
if [ "$is_fullscreen" == "true" ]; then
# newline and spacing so tag is aligned with other tags in template
fullscreen_tag=$'\n <string>fullscreen</string>'
fi
local content
read -r -d '' content <<-EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>${osx_auto_start_file_name}</string>
<key>ProgramArguments</key>
<array>
<string>${tmux_start_script}</string>$fullscreen_tag
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
EOF
echo "$content"
}
get_iterm_or_teminal_option_value() {
local options="$1"
if [[ "$options" =~ "iterm" ]]; then
echo "iterm"
else
# Terminal.app is the default console app
echo "terminal"
fi
}
get_fullscreen_option_value() {
local options="$1"
if [[ "$options" =~ "fullscreen" ]]; then
echo "true"
else
echo "false"
fi
}
main() {
local options="$(get_tmux_option "$auto_start_config_option" "$auto_start_config_default")"
local iterm_or_terminal_value="$(get_iterm_or_teminal_option_value "$options")"
local fullscreen_option_value="$(get_fullscreen_option_value "$options")"
local tmux_start_script_path="${CURRENT_DIR}/osx_${iterm_or_terminal_value}_start_tmux.sh"
local launchd_plist_file_content="$(template "$tmux_start_script_path" "$fullscreen_option_value")"
echo "$launchd_plist_file_content" > "$osx_auto_start_file_path"
}
main

View File

@@ -0,0 +1,66 @@
#!/usr/bin/env bash
# for "true full screen" call the script with "fullscreen" as the first argument
TRUE_FULL_SCREEN="$1"
start_iterm_and_run_tmux() {
osascript <<-EOF
tell application "iTerm"
activate
# open iterm window
try
set _session to current session of current terminal
on error
set _term to (make new terminal)
tell _term
launch session "Tmux"
set _session to current session
end tell
end try
# start tmux
tell _session
write text "tmux"
end tell
end tell
EOF
}
resize_window_to_full_screen() {
osascript <<-EOF
tell application "iTerm"
set winID to id of window 1
tell i term application "Finder"
set desktopSize to bounds of window of desktop
end tell
set bounds of window id winID to desktopSize
end tell
EOF
}
resize_to_true_full_screen() {
osascript <<-EOF
tell application "iTerm"
# wait for iTerm to start
delay 1
activate
# short wait for iTerm to gain focus
delay 0.1
# Command + Enter for fullscreen
tell i term application "System Events"
key code 36 using {command down}
end tell
end tell
EOF
}
main() {
start_iterm_and_run_tmux
if [ "$TRUE_FULL_SCREEN" == "fullscreen" ]; then
resize_to_true_full_screen
else
resize_window_to_full_screen
fi
}
main

View File

@@ -0,0 +1,52 @@
#!/usr/bin/env bash
# for "true full screen" call the script with "fullscreen" as the first argument
TRUE_FULL_SCREEN="$1"
start_terminal_and_run_tmux() {
osascript <<-EOF
tell application "Terminal"
if not (exists window 1) then reopen
activate
set winID to id of window 1
do script "tmux" in window id winID
end tell
EOF
}
resize_window_to_full_screen() {
osascript <<-EOF
tell application "Terminal"
set winID to id of window 1
tell application "Finder"
set desktopSize to bounds of window of desktop
end tell
set bounds of window id winID to desktopSize
end tell
EOF
}
resize_to_true_full_screen() {
osascript <<-EOF
tell application "Terminal"
# waiting for Terminal.app to start
delay 1
activate
# short wait for Terminal to gain focus
delay 0.1
tell application "System Events"
keystroke "f" using {control down, command down}
end tell
end tell
EOF
}
main() {
start_terminal_and_run_tmux
if [ "$TRUE_FULL_SCREEN" == "fullscreen" ]; then
resize_to_true_full_screen
else
resize_window_to_full_screen
fi
}
main

View File

@@ -0,0 +1,10 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/../variables.sh"
main() {
systemctl --user disable ${systemd_service_name}
}
main

View File

@@ -0,0 +1,56 @@
#!/usr/bin/env bash
CURRENT_DIR="$( dirname ${BASH_SOURCE[0]} )"
source "$CURRENT_DIR/../helpers.sh"
source "$CURRENT_DIR/../variables.sh"
template() {
local tmux_start_script="$1"
shift
local options="$@"
local content=""
read -r -d '' content <<-EOF
[Unit]
Description=tmux default session (detached)
Documentation=man:tmux(1)
[Service]
Type=forking
Environment=DISPLAY=:0
ExecStart=/usr/bin/tmux ${systemd_tmux_server_start_cmd}
ExecStop=${HOME}/.tmux/plugins/tmux-resurrect/scripts/save.sh
ExecStop=/usr/bin/tmux kill-server
KillMode=none
RestartSec=2
[Install]
WantedBy=default.target
EOF
echo "$content"
}
systemd_tmux_is_enabled() {
systemctl --user is_enabled $(basename "${systemd_unit_file_path}") >/dev/null 2>&1
}
enable_tmux_unit_on_boot() {
if ! systemd_tmux_is_enabled; then
systemctl --user enable ${systemd_service_name}
fi
}
main() {
local options="$(get_tmux_option "$auto_start_config_option" "${auto_start_config_default}")"
local systemd_tmux_server_start_cmd="$(get_tmux_option "${systemd_tmux_server_start_cmd_option}" "${systemd_tmux_server_start_cmd_default}" )"
local tmux_start_script_path="${CURRENT_DIR}/linux_start_tmux.sh"
local systemd_unit_file=$(template "${tmux_start_script_path}" "${options}")
mkdir -p "$(dirname ${systemd_unit_file_path})"
echo "$systemd_unit_file" > "${systemd_unit_file_path}"
enable_tmux_unit_on_boot
}
main

View File

@@ -0,0 +1,48 @@
get_tmux_option() {
local option="$1"
local default_value="$2"
local option_value=$(tmux show-option -gqv "$option")
if [ -z "$option_value" ]; then
echo "$default_value"
else
echo "$option_value"
fi
}
set_tmux_option() {
local option="$1"
local value="$2"
tmux set-option -gq "$option" "$value"
}
# multiple tmux server detection helpers
current_tmux_server_pid() {
echo "$TMUX" |
cut -f2 -d","
}
all_tmux_processes() {
# ignores `tmux source-file .tmux.conf` command used to reload tmux.conf
ps -Ao "command pid" |
\grep "^tmux" |
\grep -v "^tmux source"
}
number_tmux_processes_except_current_server() {
all_tmux_processes |
\grep -v " $(current_tmux_server_pid)$" |
wc -l |
sed "s/ //g"
}
number_current_server_client_processes() {
tmux list-clients |
wc -l |
sed "s/ //g"
}
another_tmux_server_running_on_startup() {
# there are 2 tmux processes (current tmux server + 1) on tmux startup
[ "$(number_tmux_processes_except_current_server)" -gt 1 ]
}

View File

@@ -0,0 +1,7 @@
current_timestamp() {
echo "$(date +%s)"
}
set_last_save_timestamp() {
set_tmux_option "$last_auto_save_option" "$(current_timestamp)"
}

View File

@@ -0,0 +1,40 @@
SUPPORTED_VERSION="1.9"
# these tmux options contain paths to tmux resurrect save and restore scripts
resurrect_save_path_option="@resurrect-save-script-path"
resurrect_restore_path_option="@resurrect-restore-script-path"
auto_save_interval_option="@continuum-save-interval"
auto_save_interval_default="15"
# time when the tmux environment was last saved (unix timestamp)
last_auto_save_option="@continuum-save-last-timestamp"
auto_restore_option="@continuum-restore"
auto_restore_default="off"
auto_restore_halt_file="${HOME}/tmux_no_auto_restore"
# tmux auto start options
auto_start_option="@continuum-boot"
auto_start_default="off"
# comma separated list of additional options for tmux auto start
auto_start_config_option="@continuum-boot-options"
auto_start_config_default=""
osx_auto_start_file_name="Tmux.Start.plist"
osx_auto_start_file_path="${HOME}/Library/LaunchAgents/${osx_auto_start_file_name}"
status_interpolation_string="\#{continuum_status}"
status_script="#($CURRENT_DIR/scripts/continuum_status.sh)"
# below options set style/color for #{continuum_status} interpolation
status_on_style_wrap_option="@continuum-status-on-wrap-style" # example value: "#[fg=green]#{value}#[fg=white]"
status_off_style_wrap_option="@continuum-status-off-wrap-style" # example value: "#[fg=yellow,bold]#{value}#[fg=white,nobold]"
status_wrap_string="\#{value}"
systemd_service_name="tmux.service"
systemd_unit_file_path="$HOME/.config/systemd/user/${systemd_service_name}"
systemd_tmux_server_start_cmd_option="@continuum-systemd-start-cmd"
systemd_tmux_server_start_cmd_default="new-session -d"