Skip to content

Instantly share code, notes, and snippets.

@trungnt13
Last active May 3, 2023 19:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trungnt13/4466aa135026c6e21786ea0964f46171 to your computer and use it in GitHub Desktop.
Save trungnt13/4466aa135026c6e21786ea0964f46171 to your computer and use it in GitHub Desktop.
Tmux autocomplete and configuration
#!/bin/bash
########## Check for tmux and bash-completion installed
if [[ "$(uname)" == "Darwin" ]]; then
if ! command -v brew >/dev/null 2>&1; then
echo "Homebrew is not installed. Please install Homebrew and try again."
exit 1
else
# Install tmux if not installed
if ! command -v tmux >/dev/null 2>&1; then
echo "Installing tmux..."
brew install tmux
fi
# Install bash-completion if not installed
if ! brew ls --versions bash-completion >/dev/null 2>&1; then
echo "Installing bash-completion..."
brew install bash-completion
fi
fi
elif [[ "$(uname)" == "Linux" ]]; then
# Add a check for the user's permissions and provide an error message if they do not have the necessary permissions
if ! sudo -v >/dev/null 2>&1; then
echo "You do not have the necessary permissions to install packages. Please run the script as a user with sudo privileges."
exit 1
fi
# Install tmux if not installed (Linux)
if ! command -v tmux >/dev/null 2>&1; then
echo "Installing tmux..."
sudo apt-get install -y tmux
fi
# Install bash-completion if not installed (Linux)
if ! type _init_completion &>/dev/null; then
echo "Installing bash-completion..."
sudo apt-get install -y bash-completion
fi
else
echo "Unsupported operating system '$(uname)'. Please install tmux and bash-completion manually."
exit 1
fi
########## Determine the current shell and install tmux autocomplete
current_shell="$(basename "$SHELL")"
# Bash
if [[ "$current_shell" == "bash" ]]; then
echo "Setting up tmux autocomplete for Bash..."
if [[ "$(uname)" == "Darwin" && ! $(grep -q "/etc/profile.d/bash_completion.sh" ~/.bash_profile) ]]; then
echo "[MacOS] Setup tmux autocomplete in ~/.bash_profile"
# Append code block to .bash_profile
cat << EOF >> ~/.bash_profile
# <<< bash-completion <<<
if type brew &>/dev/null
then
HOMEBREW_PREFIX="\$(brew --prefix)"
if [ -f \${HOMEBREW_PREFIX}/etc/bash_completion ]; then
. \${HOMEBREW_PREFIX}/etc/bash_completion
fi
if [[ -r "\${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh" ]]
then
source "\${HOMEBREW_PREFIX}/etc/profile.d/bash_completion.sh"
else
for COMPLETION in "\${HOMEBREW_PREFIX}/etc/bash_completion.d/"*
do
[[ -r "\${COMPLETION}" ]] && source "\${COMPLETION}"
done
fi
fi
# <<< bash-completion <<<
EOF
fi
# Check if tmux_completion is already installed, Download the tmux completion script for Bash
if [ ! -f ~/.tmux_completion ]; then
curl -o ~/.tmux_completion https://raw.githubusercontent.com/imomaliev/tmux-bash-completion/master/completions/tmux
fi
# Get the path to the bash profile
if [[ "$(uname)" == "Darwin" ]]; then
BASH_PROFILE="${HOME}/.bash_profile"
elif [[ "$(uname)" == "Linux" ]]; then
BASH_PROFILE="${HOME}/.bashrc"
else
echo "Unsupported operating system '$(uname)'. Please install tmux and bash-completion manually."
exit 1
fi
if [ ! -f "${BASH_PROFILE}" ]; then
echo "${BASH_PROFILE} does not exist. Stop the installation!"
exit 1
fi
# Add the source line to ~/.bashrc or ~/.bash_profile
if ! grep -q "source ~/.tmux_completion" "${BASH_PROFILE}"; then
echo "Adding tmux autocomplete to ${BASH_PROFILE}"
echo "source ~/.tmux_completion" >> "${BASH_PROFILE}"
# Apply the changes
source "${BASH_PROFILE}"
else
echo "tmux autocomplete is already enabled in ${BASH_PROFILE}"
fi
else
echo "Tmux autocomplete is not supported for $current_shell."
exit 1
fi
########## Customize tmux configuration
# Define the settings to be added
# copy-pipe command stores selected text in tmux buffer same to copy-selection,
# plus pipes selected text to the given command pbcopy. So we get text stored in two places:
# the tmux copy buffer and the system clipboard.
settings_to_add='
set -g history-limit 50000
## Change background and border of active pane
set-option -g pane-border-style fg=default,bg=default
set-option -g pane-active-border-style fg=red,bg=default
set-option -g default-terminal "xterm-256color"
# set-option -g default-terminal "screen-256color"
set -g window-style "bg=black"
# set -g window-active-style "bg=grey20"
set -g window-active-style "bg=#000040"
### copy-mode
# Set custom key bindings for copy mode
bind-key -T copy-mode 'i' send-keys -X cursor-up
bind-key -T copy-mode 'k' send-keys -X cursor-down
bind-key -T copy-mode 'j' send-keys -X cursor-left
bind-key -T copy-mode 'l' send-keys -X cursor-right
bind-key -T copy-mode 'u' send-keys -X start-of-line
bind-key -T copy-mode 'o' send-keys -X end-of-line
bind-key -T copy-mode '.' send-keys -X page-up
bind-key -T copy-mode ',' send-keys -X page-down
# selection and clipboard
bind-key -T copy-mode '[' send-keys -X begin-selection
bind-key -T copy-mode 'Space' send-keys -X begin-selection
bind-key -T copy-mode ']' send-keys -X clear-selection
# bind-key -T copy-mode Enter send-keys -X copy-selection-and-cancel
if-shell "uname | grep -q Linux" {
bind-key -T copy-mode Enter send-keys -X copy-pipe-and-cancel "xclip -in -selection clipboard"
}
# macOS configuration
if-shell "uname | grep -q Darwin" {
bind-key -T copy-mode Enter send-keys -X copy-pipe-and-cancel "pbcopy"
}
'
# Check if the tmux.conf file exists, if not, create it
tmux_conf="${HOME}/.tmux.conf"
if [ ! -f "${tmux_conf}" ]; then
touch "${tmux_conf}"
fi
# Check if the settings are already in the tmux.conf file
if ! grep -qF -- "${settings_to_add}" "${tmux_conf}"; then
# If not, append the settings to the file
echo "${settings_to_add}" >> "${tmux_conf}"
echo "Settings added to tmux.conf"
else
echo "Settings already exist in tmux.conf"
fi
########## reload tmux
tmux source-file "$tmux_conf"
@trungnt13
Copy link
Author

trungnt13 commented Apr 13, 2023

/bin/bash -c "$(curl -fsSL https://gist.githubusercontent.com/trungnt13/4466aa135026c6e21786ea0964f46171/raw/3a90efe5fd023e747ecb200f561ae9267531d93d/tmux_config.sh)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment