Skip to content

Instantly share code, notes, and snippets.

@SteveBenner
Last active April 16, 2022 03:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveBenner/4b4db441716732efe427 to your computer and use it in GitHub Desktop.
Save SteveBenner/4b4db441716732efe427 to your computer and use it in GitHub Desktop.
Define aliases in your shell with YAML instead of in Bash
#!/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.
#!/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.
# 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 ..
@SteveBenner
Copy link
Author

I execute this script by loading it into my Bash profile as follows:

# Load aliases
$(~/.yaml-aliases-groups.rb)

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

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