@@ -1,6 +1,6 @@ |
| 1 | 1 | # shtick |
| 2 | 2 | #### shell-agnostic-shell-configuration-generation |
| 3 | | -(noun) : something cl[ever] |
| 3 | +(noun) : something cl[ever] |
| 4 | 4 | (see also) : pedantic |
| 5 | 5 | |
| 6 | 6 | ### what is this? |
@@ -8,9 +8,12 @@ this is a tool I use to manage groups of shell configurations (aliases, env vars |
| 8 | 8 | |
| 9 | 9 | It's useful if you use multiple shells regularly and would like a single source of truth for your shell ..config (...within reason). |
| 10 | 10 | |
| 11 | | -- Define aliases, env vars, and shell functions in one place (~/.config/shtick/config.toml); |
| 11 | +- Define aliases, env vars, and shell functions in one place (~/.config/shtick/config.toml) |
| 12 | 12 | - Manage persistent aliases, env vars, shell functions |
| 13 | | -- Define toggleable groups of aliases, e.g. `dev`, `personal` |
| 13 | +- Define toggleable groups of aliases, e.g. dev, personal |
| 14 | +- Create, rename, and remove groups |
| 15 | +- Backup and restore configurations |
| 16 | +- Configurable behavior through settings |
| 14 | 17 | |
| 15 | 18 | ### Shell Integration |
| 16 | 19 | Shtick generates shell-specific files in `~/.config/shtick/`. To make them available in new shell sessions, add this to your shell config: |
@@ -27,77 +30,171 @@ source ~/.config/shtick/load_active.fish |
| 27 | 30 | `bash`, `zsh`, `fish`, `ksh`, `mksh`, `yash`, `dash`, `csh`, `tcsh`, `xonsh`, `elvish`, `nushell`, `powershell`, `rc`, `es`, `oil` |
| 28 | 31 | - Run `shtick shells` to see the complete list. |
| 29 | 32 | |
| 30 | | -If you decide you want to use this yourself, do all the usual cloning, then from your local run `make install` and the `shtick` package will be installed in your environment. |
| 33 | +### Installation |
| 31 | 34 | |
| 32 | | -### Commands reference |
| 35 | +If you decide you want to use this yourself, do all the usual cloning, then from your local clone run `make install` and the `shtick` package will be installed in your environment. |
| 36 | + |
| 37 | +## Commands reference |
| 38 | + |
| 39 | +#### Core |
| 33 | 40 | ``` |
| 41 | +# Add persistent items (always active) |
| 34 | 42 | shtick alias <key>=<value> # Add persistent alias |
| 35 | 43 | shtick env <key>=<value> # Add persistent env var |
| 36 | 44 | shtick function <key>=<value> # Add persistent function |
| 37 | 45 | |
| 46 | +# Add to specific groups |
| 38 | 47 | shtick add <type> <group> <key>=<value> # Add to specific group |
| 39 | 48 | shtick remove <type> <group> <key> # Remove from group |
| 40 | 49 | |
| 50 | +# Group activation |
| 41 | 51 | shtick activate <group> # Activate group |
| 42 | 52 | shtick deactivate <group> # Deactivate group |
| 43 | 53 | |
| 54 | +# Information |
| 44 | 55 | shtick status # Show status |
| 45 | | -shtick list [-l] # List items |
| 46 | | -shtick generate # Regenerate shell files |
| 47 | | -shtick source [--shell <shell>] # Output source command |
| 48 | | - |
| 56 | +shtick list [-l] # List items (use -l for detailed view) |
| 49 | 57 | shtick shells [-l] # List supported shells |
| 50 | 58 | ``` |
| 51 | 59 | |
| 52 | | -`` |
| 60 | +#### Group management |
| 61 | +``` |
| 62 | +# Create, rename, and remove groups |
| 63 | +shtick group create <name> [-d <description>] # Create new group |
| 64 | +shtick group rename <old> <new> # Rename group |
| 65 | +shtick group remove <name> [-f] # Remove group (-f to skip confirmation) |
| 66 | +``` |
| 53 | 67 | |
| 54 | | -Usage examples: |
| 68 | +#### Backup & restore |
| 69 | +``` |
| 70 | +# Backup management |
| 71 | +shtick backup create [-n <name>] # Create backup (optional custom name) |
| 72 | +shtick backup list # List available backups |
| 73 | +shtick backup restore <name> # Restore from backup |
| 55 | 74 | ``` |
| 56 | | -shtick generate [--terse] # generate shell files for all items in config |
| 57 | 75 | |
| 58 | | -shtick list # list all managed items |
| 59 | | -shtick status # see all active items |
| 60 | | -shtick activate work # activate items in work group |
| 61 | | -shtick deactivate dev # deactivate items in dev group |
| 62 | | -eval "$(shtick source)" # IMPORTANT! load changes in shell. See tip below |
| 76 | +#### Settings management |
| 77 | +``` |
| 78 | +# Configure shtick behavior |
| 79 | +shtick settings init # Create default settings file |
| 80 | +shtick settings show # Show current settings |
| 81 | +shtick settings set <key> <value> # Change a setting |
| 82 | + |
| 83 | +# Available settings: |
| 84 | +# generation.shells = [] # List of shells to generate for (empty = auto-detect) |
| 85 | +# generation.parallel = false # Enable parallel generation |
| 86 | +# behavior.auto_source_prompt = true # Prompt to source after changes |
| 87 | +# behavior.check_conflicts = true # Warn about conflicts |
| 88 | +# behavior.backup_on_save = false # Auto-backup before saving |
| 89 | +``` |
| 63 | 90 | |
| 64 | | -shtick alias ll='ls -la' # add persistent alias |
| 65 | | -shtick add alias work up='cd ../' # add alias to specified group |
| 66 | | -shtick remove alias work up # remove alias from specified group |
| 91 | +#### Other commands |
| 92 | +``` |
| 93 | +shtick generate [--terse] # Regenerate shell files |
| 94 | +shtick source [--shell <shell>] # Output source command for eval |
| 95 | +``` |
| 67 | 96 | |
| 68 | | -shtick env EDITOR='micro' # add persistent environment variable |
| 69 | | -shtick remove env project DEBUG='true' # add env var to project group |
| 70 | | -shtick remove env persistent XDG_HAM # remove persistent environment variable |
| 71 | 97 | |
| 72 | | -shtick function mkcd='mkdir -p "$1" && cd "$1"' # add persistent function |
| 73 | | -shtick add function util backup='cp "$1" "$1.backup.$(date +%Y%m%d)"' # add function to util group |
| 98 | +### Usage Examples |
| 99 | +#### Basic Workflow |
| 100 | +``` |
| 101 | +# Add some aliases |
| 102 | +shtick alias ll='ls -la' |
| 103 | +shtick alias gs='git status' |
| 104 | +shtick alias gd='git diff' |
| 105 | +``` |
| 74 | 106 | |
| 75 | | -# Recommended: add the following: |
| 76 | | -# Add alias for instant sourcing |
| 77 | | -shtick alias ss='eval "$(shtick source)"' # or your own command to source |
| 107 | +#### Add environment variables |
| 108 | +``` |
| 109 | +shtick env EDITOR='vim' |
| 110 | +shtick env PAGER='less' |
| 111 | +``` |
| 78 | 112 | |
| 79 | | -# Use it after any shtick command to 'auto' source changes |
| 80 | | -shtick alias deploy='./deploy.sh' && ss |
| 81 | | -shtick activate work && ss |
| 113 | +#### Add functions |
| 114 | +``` |
| 115 | +shtick function mkcd='mkdir -p "$1" && cd "$1"' |
| 116 | +shtick function backup='cp "$1" "$1.backup.$(date +%Y%m%d)"' |
| 82 | 117 | ``` |
| 83 | 118 | |
| 84 | | -Sample config (pulled from my config): |
| 85 | | -`~/.config/shtick/config.toml. Also see [sample](/config.sample.toml) |
| 119 | +#### Load changes in current shell |
| 120 | +`eval "$(shtick source)"` |
| 121 | + |
| 122 | +### Working with Groups |
| 123 | +#### Create a work group |
| 124 | +`shtick group create work -d "Work-related configurations"` |
| 125 | + |
| 126 | +#### Add items to the work group |
| 127 | +``` |
| 128 | +shtick add alias work deploy='./scripts/deploy.sh' |
| 129 | +shtick add env work NODE_ENV='development' |
| 130 | +shtick add function work vpn='sudo openvpn /etc/vpn/work.conf' |
| 131 | +``` |
| 132 | + |
| 133 | +#### Create a personal group |
| 134 | +`shtick group create personal` |
| 135 | + |
| 136 | +#### Add items to personal group |
| 137 | +``` |
| 138 | +shtick add alias personal myip='curl ifconfig.me' |
| 139 | +shtick add function personal note='echo "$(date): $*" >> $HOME/notes.txt' |
| 140 | +``` |
| 141 | + |
| 142 | +#### Activate work group |
| 143 | +`shtick activate work` |
| 144 | + |
| 145 | +#### Switch to personal |
| 146 | +``` |
| 147 | +shtick deactivate work |
| 148 | +shtick activate personal |
| 149 | +``` |
| 150 | + |
| 151 | +#### Or have both active |
| 152 | +``` |
| 153 | +shtick activate work |
| 154 | +shtick activate personal |
| 155 | +Backup and Restore |
| 156 | + |
| 157 | +# Create a backup before major changes |
| 158 | +shtick backup create -n "before_refactor" |
| 159 | +``` |
| 160 | + |
| 161 | +#### Make your changes... |
| 162 | +`shtick group remove old_stuff -f` |
| 163 | + |
| 164 | +#### Oops, need to restore |
| 165 | +`shtick backup restore before_refactor` |
| 166 | + |
| 167 | +### Settings Customization |
| 168 | +#### Initialize settings file |
| 169 | +`shtick settings init` |
| 170 | + |
| 171 | +#### Disable auto-source prompt |
| 172 | +`shtick settings set behavior.auto_source_prompt false` |
| 173 | + |
| 174 | +#### Generate for specific shells only |
| 175 | +`shtick settings set generation.shells '["bash", "zsh"]'` |
| 176 | + |
| 177 | +#### Enable auto-backup |
| 178 | +`shtick settings set behavior.backup_on_save true` |
| 179 | + |
| 180 | +## Sample Configuration |
| 181 | +Shtick looks for `~/.config/shtick/config.toml`. Here's a sample. See also [sample](/config.sample.toml) |
| 182 | + |
| 86 | 183 | ``` |
| 87 | 184 | # Persistent items - always active in every shell session |
| 88 | 185 | [persistent.aliases] |
| 89 | | -pls="sudo !!" |
| 186 | +pls = "sudo !!" |
| 90 | 187 | bk = "- || cd -" |
| 91 | 188 | wttr = "curl wttr.in" |
| 92 | 189 | refresh = "source ${HOME}/.zshrc" |
| 93 | 190 | goclass = "cd ${HOME}/Documents/Class/" |
| 94 | 191 | goproj = "cd ${HOME}/Documents/Project/" |
| 95 | 192 | goorg = "cd ${HOME}/Documents/GithubOrgs/" |
| 96 | | -gad="git add" |
| 97 | | -gall="git add --all" |
| 98 | | -gputt="git push origin trunk" |
| 99 | | -gcamm="git commit --all --message" |
| 100 | | -glogg="git log --online --graph --decorate --all" |
| 193 | +gad = "git add" |
| 194 | +gall = "git add --all" |
| 195 | +gputt = "git push origin trunk" |
| 196 | +gcamm = "git commit --all --message" |
| 197 | +glogg = "git log --oneline --graph --decorate --all" |
| 101 | 198 | |
| 102 | 199 | [persistent.env_vars] |
| 103 | 200 | PAGER = "less" |
@@ -107,203 +204,79 @@ BROWSER = "firefox" |
| 107 | 204 | [persistent.functions] |
| 108 | 205 | backup = "cp \"$1\" \"$1.backup.$(date +%Y%m%d_%H%M%S)\"" |
| 109 | 206 | |
| 207 | +# Development group |
| 110 | 208 | [dev.aliases] |
| 111 | 209 | mk = "make" |
| 112 | | -mkr = "make run" |
| 113 | | -mki = "make install" |
| 210 | +mkr = "make run" |
| 211 | +mki = "make install" |
| 114 | 212 | gfort = "gfortran" |
| 115 | | -ni = "npm --install" |
| 213 | +ni = "npm install" |
| 116 | 214 | serve = "python manage.py runserver" |
| 117 | 215 | pyserve = "python3 -m http.server 8000" |
| 118 | 216 | brewup = "brew update && brew upgrade && brew cleanup" |
| 119 | 217 | |
| 120 | 218 | [dev.env_vars] |
| 121 | | -JOM = "terry" |
| 219 | +DEBUG = "1" |
| 220 | +NODE_ENV = "development" |
| 122 | 221 | |
| 123 | 222 | [dev.functions] |
| 124 | 223 | newproject = "mkdir -p \"$HOME/projects/$1\" && cd \"$HOME/projects/$1\" && git init" |
| 125 | 224 | |
| 225 | +# Personal group |
| 126 | 226 | [personal.aliases] |
| 127 | 227 | myip = "curl ifconfig.me" |
| 128 | 228 | |
| 129 | 229 | [personal.env_vars] |
| 130 | | -TOM = "JERRY" |
| 230 | +PERSONAL_NOTES = "$HOME/Documents/notes" |
| 131 | 231 | |
| 132 | 232 | [personal.functions] |
| 133 | 233 | note = "echo \"$(date): $*\" >> $HOME/notes.txt" |
| 134 | | -``` |
| 135 | | - |
| 136 | | - |
| 137 | | -## Python API Usage |
| 138 | | -We also expose a public API for using shtick functionality as library functions. |
| 139 | | - |
| 140 | | -### Basic Usage |
| 141 | | -``` |
| 142 | | -from shtick import ShtickManager |
| 143 | | - |
| 144 | | -# Initialize the manager |
| 145 | | -manager = ShtickManager() |
| 146 | | - |
| 147 | | -# Add persistent aliases (always active) |
| 148 | | -manager.add_persistent_alias('ll', 'ls -la') |
| 149 | | -manager.add_persistent_alias('grep', 'grep --color=auto') |
| 150 | | - |
| 151 | | -# Add environment variables |
| 152 | | -manager.add_persistent_env('EDITOR', 'vim') |
| 153 | | -manager.add_persistent_env('BROWSER', 'firefox') |
| 154 | | - |
| 155 | | -# Check status |
| 156 | | -status = manager.get_status() |
| 157 | | -print(f"Active groups: {status['active_groups']}") |
| 158 | | -print(f"Persistent items: {status['persistent_items']}") |
| 159 | | -``` |
| 160 | | - |
| 161 | | -### Working with groups |
| 162 | | -``` |
| 163 | | -from shtick import ShtickManager |
| 164 | | - |
| 165 | | -manager = ShtickManager() |
| 166 | | - |
| 167 | | -# Create project-specific configuration |
| 168 | | -project_aliases = { |
| 169 | | - 'start': 'npm start', |
| 170 | | - 'test': 'npm test', |
| 171 | | - 'build': 'npm run build', |
| 172 | | - 'deploy': 'npm run deploy' |
| 173 | | -} |
| 174 | | - |
| 175 | | -# Add all aliases to a group |
| 176 | | -for alias, command in project_aliases.items(): |
| 177 | | - manager.add_alias(alias, command, 'frontend') |
| 178 | | - |
| 179 | | -# Add environment variables for the project |
| 180 | | -manager.add_env('NODE_ENV', 'development', 'frontend') |
| 181 | | -manager.add_env('API_URL', 'http://localhost:3000', 'frontend') |
| 182 | | - |
| 183 | | -# Activate the group |
| 184 | | -manager.activate_group('frontend') |
| 185 | | - |
| 186 | | -# Get information about what's configured |
| 187 | | -items = manager.list_items('frontend') |
| 188 | | -for item in items: |
| 189 | | - print(f"{item['type']}: {item['key']} = {item['value']}") |
| 190 | | -``` |
| 191 | | - |
| 192 | | -### Automation and scripting |
| 193 | | - |
| 194 | | -from shtick import ShtickManager |
| 195 | | -``` |
| 196 | | -def setup_development_environment(): |
| 197 | | - """Set up a complete development environment""" |
| 198 | | - manager = ShtickManager() |
| 199 | | - |
| 200 | | - # Docker aliases |
| 201 | | - docker_aliases = { |
| 202 | | - 'dps': 'docker ps', |
| 203 | | - 'dimg': 'docker images', |
| 204 | | - 'dlog': 'docker logs -f', |
| 205 | | - 'dexec': 'docker exec -it', |
| 206 | | - 'dstop': 'docker stop $(docker ps -q)', |
| 207 | | - } |
| 208 | | - |
| 209 | | - for alias, command in docker_aliases.items(): |
| 210 | | - manager.add_alias(alias, command, 'docker') |
| 211 | | - |
| 212 | | - # Git aliases |
| 213 | | - git_aliases = { |
| 214 | | - 'gst': 'git status', |
| 215 | | - 'gco': 'git checkout', |
| 216 | | - 'gcb': 'git checkout -b', |
| 217 | | - 'gpl': 'git pull', |
| 218 | | - 'gps': 'git push', |
| 219 | | - } |
| 220 | | - |
| 221 | | - for alias, command in git_aliases.items(): |
| 222 | | - manager.add_alias(alias, command, 'git') |
| 223 | | - |
| 224 | | - # Development environment variables |
| 225 | | - dev_env = { |
| 226 | | - 'DOCKER_BUILDKIT': '1', |
| 227 | | - 'COMPOSE_DOCKER_CLI_BUILD': '1', |
| 228 | | - 'NODE_ENV': 'development', |
| 229 | | - } |
| 230 | | - |
| 231 | | - for var, value in dev_env.items(): |
| 232 | | - manager.add_env(var, value, 'development') |
| 233 | | - |
| 234 | | - # Activate all development groups |
| 235 | | - groups = ['docker', 'git', 'development'] |
| 236 | | - for group in groups: |
| 237 | | - success = manager.activate_group(group) |
| 238 | | - print(f"{'✓' if success else '✗'} Activated {group} group") |
| 239 | | - |
| 240 | | - # Generate shell files |
| 241 | | - manager.generate_shell_files() |
| 242 | | - |
| 243 | | - # Show source command for immediate use |
| 244 | | - source_cmd = manager.get_source_command() |
| 245 | | - if source_cmd: |
| 246 | | - print(f"\nTo use immediately, run: {source_cmd}") |
| 247 | | - |
| 248 | | -def clean_inactive_groups(): |
| 249 | | - """Remove aliases from inactive groups""" |
| 250 | | - manager = ShtickManager() |
| 251 | | - |
| 252 | | - active_groups = manager.get_active_groups() |
| 253 | | - all_groups = manager.get_groups() |
| 254 | | - |
| 255 | | - inactive_groups = set(all_groups) - set(active_groups) - {'persistent'} |
| 256 | | - |
| 257 | | - for group in inactive_groups: |
| 258 | | - items = manager.list_items(group) |
| 259 | | - if items: |
| 260 | | - print(f"Found {len(items)} items in inactive group '{group}'") |
| 261 | | - # Could prompt user or automatically clean up |
| 262 | | - |
| 263 | | - return inactive_groups |
| 264 | | - |
| 265 | | -# Example usage |
| 266 | | -if __name__ == '__main__': |
| 267 | | - setup_development_environment() |
| 268 | | -``` |
| 269 | | - |
| 270 | | -### Error handling and validation |
| 271 | | -``` |
| 272 | | -from shtick import ShtickManager |
| 273 | | -def safe_alias_management(): |
| 274 | | - """Example with proper error handling""" |
| 275 | | - manager = ShtickManager(debug=True) # Enable debug output |
| 276 | | - |
| 277 | | - # Try to add an alias |
| 278 | | - success = manager.add_persistent_alias('test', 'echo "Hello World"') |
| 279 | | - if success: |
| 280 | | - print("✓ Alias added successfully") |
| 281 | | - else: |
| 282 | | - print("✗ Failed to add alias") |
| 283 | | - |
| 284 | | - # Check current status before making changes |
| 285 | | - status = manager.get_status() |
| 286 | | - if 'error' in status: |
| 287 | | - print(f"Configuration error: {status['error']}") |
| 288 | | - return False |
| 289 | | - |
| 290 | | - # Validate shell integration |
| 291 | | - if not status['loader_exists']: |
| 292 | | - print("Warning: Shell loader not found. Run 'shtick generate'") |
| 293 | | - |
| 294 | | - # List current items to avoid conflicts |
| 295 | | - existing_items = manager.list_items() |
| 296 | | - existing_aliases = [item['key'] for item in existing_items |
| 297 | | - if item['type'] == 'alias'] |
| 298 | | - |
| 299 | | - new_alias = 'myalias' |
| 300 | | - if new_alias in existing_aliases: |
| 301 | | - print(f"Alias '{new_alias}' already exists") |
| 302 | | - else: |
| 303 | | - manager.add_persistent_alias(new_alias, 'echo "New alias"') |
| 304 | | - |
| 305 | | - return True |
| 306 | | - |
| 307 | | -# Run the example |
| 308 | | -safe_alias_management() |
| 234 | +todo = "echo \"[ ] $*\" >> $HOME/todo.txt" |
| 235 | + |
| 236 | +# Work group (activate during work hours) |
| 237 | +[work.aliases] |
| 238 | +deploy = "./scripts/deploy.sh" |
| 239 | +staging = "ssh staging.company.com" |
| 240 | +prod = "ssh prod.company.com" |
| 241 | + |
| 242 | +[work.env_vars] |
| 243 | +AWS_PROFILE = "work" |
| 244 | +KUBECONFIG = "$HOME/.kube/work-config" |
| 245 | + |
| 246 | +[work.functions] |
| 247 | +vpn = "sudo openvpn /etc/vpn/work.conf" |
| 248 | +standup = "open https://meet.company.com/daily-standup" |
| 249 | +``` |
| 250 | + |
| 251 | +### Tips n. Trinkets |
| 252 | + |
| 253 | + |
| 254 | +#### "Instant" sourcing alias: Add this for convenience: |
| 255 | +``` |
| 256 | +shtick alias ss='eval "$(shtick source)"' |
| 257 | +# Now you can just run 'ss' after any shtick command |
| 258 | +``` |
| 259 | + |
| 260 | +#### Check for conflicts: Shtick warns you about duplicate items across groups: |
| 261 | +``` |
| 262 | +$ shtick add alias dev ll='ls -la' |
| 263 | +Warning: Item 'll' exists in groups: ['persistent'] |
| 264 | +``` |
| 265 | + |
| 266 | +#### Fuzzy removal: Remove items with partial matching: |
| 267 | +``` |
| 268 | +shtick remove alias persistent brew # Matches 'brewup' and offers selection |
| 269 | +``` |
| 270 | + |
| 271 | +#### Quick status check: See what's active at a glance: |
| 272 | +```$ shtick status |
| 273 | +Persistent (always active): 15 items |
| 274 | +Available Groups: |
| 275 | + dev: 8 items (ACTIVE) |
| 276 | + personal: 5 items (inactive) |
| 277 | + work: 12 items (ACTIVE) |
| 278 | +``` |
| 279 | +#### Backup before removing groups: |
| 280 | +``` |
| 281 | +shtick backup create -n "safe_point" && shtick group remove old_configs -f |
| 309 | 282 | ``` |