Skip to content

Instantly share code, notes, and snippets.

@electblake
Last active January 25, 2024 17:39
Show Gist options
  • Save electblake/e150f3dc45ee0452ef3fe21fd34221e2 to your computer and use it in GitHub Desktop.
Save electblake/e150f3dc45ee0452ef3fe21fd34221e2 to your computer and use it in GitHub Desktop.
Enhance the `mosh` command in Zsh by automatically adding default arguments.
# File: custom-mosh.zsh
# Author: electblake <github.com/electblake>
# Last Modified: 2024-01-25
# Source URL: https://gist.github.com/electblake/e150f3dc45ee0452ef3fe21fd34221e2
# Description:
# Enhances the `mosh` command in Zsh by automatically adding default arguments.
# Defaults set:
# --server "/usr/local/bin/mosh-server"
# Additional defaults can be added in future updates.
#
# Usage:
# Use the `mosh` command as usual. Defaults will be applied automatically.
# Example:
# mosh user@host # Automatically uses the enhanced server connection.
#
# Installation:
# 1. Download the script using wget from the source URL:
# ```
# wget https://gist.github.com/electblake/e150f3dc45ee0452ef3fe21fd34221e2/raw/custom-mosh.zsh -O ${ZSH:-$HOME/.oh-my-zsh}/custom/custom-mosh.zsh
# ```
mosh() {
# Avoid interfering with Zsh's internal file descriptor handling
emulate -L zsh
local new_args=()
local server_arg_present=0
for arg in "$@"; do
if [[ $arg == --server* ]]; then
server_arg_present=1
fi
new_args+=("$arg")
done
if [[ $server_arg_present -eq 0 ]]; then
new_args+=(--server "/usr/local/bin/mosh-server")
fi
# Get the absolute path to the current script, including the file name
local script_path="$(realpath $0)"
# Extract the directory path
local script_dir="$(dirname $script_path)"
# Replace the home directory path with "$HOME" and include the file name
local home_path="${script_dir/#$HOME/$HOME}/custom-mosh.zsh"
# Print a small notice to inform users of the enhanced function, its location, and the script path
echo "Notice: Using an enhanced 'mosh' command with automatic server connection."
echo " Script location: $home_path"
command mosh "${new_args[@]}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment