Last active
April 16, 2022 03:34
-
-
Save SteveBenner/4b4db441716732efe427 to your computer and use it in GitHub Desktop.
Define aliases in your shell with YAML instead of in Bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# If you've got a collection of Bash aliases that just keeps growing... Consider using YAML! | |
# | |
# This version is designed to parse aliases organized under named groups in the YAML | |
# file, so the first level of mappings are purely semantic, and are used to keep the | |
# data organized and well-documented. | |
# | |
require 'pathname' | |
require 'yaml' | |
$stdout.sync = true | |
DATA_FILE = '~/.aliases.yml' # a YAML file of your choice | |
# Use of Pathname is optional, but I think it makes for particularly clean, efficient code | |
ALIAS_GROUPS = YAML.load_file Pathname(DATA_FILE).expand_path.realpath | |
# Aliases are all merged into a single Hash according to the rules of Ruby's `Hash#merge`, | |
# which silently overwrites duplicates. It may be a good idea to enhance this code with a | |
# declarative block passed to `merge` that prevents data loss or at least warns the user. | |
ALIASES = ALIAS_GROUPS.values.reduce &:merge | |
# A Bash script is produced temporarily for storing the `alias` declarations | |
ALIAS_FILE = '/tmp/aliases.bash' | |
# This code writes all of the YAML data to a file, in the format of the Bash `alias` command | |
File.open(ALIAS_FILE, 'w') do |f| | |
ALIASES.each_pair do |alias_name, command| | |
f.puts %Q[alias #{alias_name}="#{cmd}"] | |
end | |
end | |
# Finally, the command is printed which your shell context evaluates to load the alias file | |
puts "source #{ALIAS_FILE}" | |
# Note: no cleanup is done for the generated Bash script, but as long as 'w' mode is used | |
# for file access, it can be safely overwritten indefinitely. If you try to delete the file | |
# immediately after printing the `source` command, it may not have time to be loaded. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# If you've got a collection of Bash aliases that just keeps growing... Consider using YAML! | |
# | |
require 'pathname' | |
require 'yaml' | |
$stdout.sync = true | |
DATA_FILE = '~/.aliases.yml' # a YAML file of your choice | |
# Use of Pathname is optional, but I think it makes for particularly clean, efficient code | |
ALIASES = YAML.load_file Pathname(DATA_FILE).expand_path.realpath | |
# A Bash script is produced temporarily for storing the `alias` declarations | |
ALIAS_FILE = '/tmp/aliases.bash' | |
# This code writes all of the YAML data to a file, in the format of the Bash `alias` command | |
File.open(ALIAS_FILE, 'w') do |f| | |
ALIASES.each_pair do |alias_name, command| | |
f.puts %Q[alias #{alias_name}="#{cmd}"] | |
end | |
end | |
# Finally, the command is printed which your shell context evaluates to load the alias file | |
puts "source #{ALIAS_FILE}" | |
# Note: no cleanup is done for the generated Bash script, but as long as 'w' mode is used | |
# for file access, it can be safely overwritten indefinitely. If you try to delete the file | |
# immediately after printing the `source` command, it may not have time to be loaded. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# These are some examples of how one would store command aliases in YAML | |
system: | |
ls: ls -GCF # pretty output | |
lla: ls -la # show file info, including invisible files | |
path: echo $PATH | tr \':\' \"\n\" # readable PATH output | |
qfind: find . -name # Quickly search for a file | |
git: | |
# info | |
gs: git status | |
gss: git status -s | |
gd: git diff | |
# Remotes | |
rem: git remote -v | |
rset: git remote set-url origin | |
# add/remove files | |
ga: git add | |
gaa: git add -A | |
# tags | |
gt: git tag | |
# commit | |
gc: git commit -m | |
amend: git commit --amend | |
# pull/push | |
gpull: git pull origin | |
# Branches | |
gb: git branch -v | |
gbr: git branch -r | |
gco: git checkout | |
gnew: git checkout -b | |
# submodules | |
gsm: git submodule | |
gsi: git submodule init | |
gsu: git submodule update | |
gclone: git clone -v --progress | |
navigation: | |
..: cd .. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I execute this script by loading it into my Bash profile as follows:
Alternatively, you could omit the ‘shebang’ (the first line of the script) and just use the Ruby interpreter directly
$(ruby ~/.yaml-aliases-groups.rb)
Don’t forget that the script needs execution permission!
chmod u+x ~/.yaml-aliases-groups.rb
If you like this approach, be sure to check out how I use the same technique to define Environment variables