1ad167dfac325a9f92e0693c70d0ab3f7c4c574b

Author
Marc Cornellà <hello@mcornella.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(init)!: allow turning off aliases for libs and plugins (#11550)

BREAKING CHANGE: the previous zstyle setting to disable `lib/directories.zsh` aliases has
been changed to the new syntax: `zstyle ':omz:lib:directories' aliases no`. See
https://github.com/ohmyzsh/ohmyzsh#skip-aliases to see other ways you can use this setting.
    
Co-authored-by: Carlo Sala <carlosalag@protonmail.com>

Diff

  1diff --git a/README.md b/README.md
  2index 4f0aeb7b4b77fd5432532fcf9d59a7e43a3995b7..556d4c8c5b474cb37fd7acd492478196ded04ebe 100644
  3--- a/README.md
  4+++ b/README.md
  5@@ -40,7 +40,9 @@ To learn more, visit [ohmyz.sh](https://ohmyz.sh), follow [@ohmyzsh](https://twi
  6     - [Manual Installation](#manual-installation)
  7   - [Installation Problems](#installation-problems)
  8   - [Custom Plugins and Themes](#custom-plugins-and-themes)
  9+  - [Skip aliases](#skip-aliases)
 10 - [Getting Updates](#getting-updates)
 11+  - [Updates verbosity](#updates-verbosity)
 12   - [Manual Updates](#manual-updates)
 13 - [Uninstalling Oh My Zsh](#uninstalling-oh-my-zsh)
 14 - [How do I contribute to Oh My Zsh?](#how-do-i-contribute-to-oh-my-zsh)
 15@@ -276,16 +278,58 @@ If you have many functions that go well together, you can put them as a `XYZ.plu
 16 
 17 If you would like to override the functionality of a plugin distributed with Oh My Zsh, create a plugin of the same name in the `custom/plugins/` directory and it will be loaded instead of the one in `plugins/`.
 18 
 19-### Remove directories aliases
 20+### Skip aliases
 21 
 22-If you want to skip ohmyzsh default
 23-[directories aliases](https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/directories.zsh) you can add the
 24-following snippet to your `zshrc`, before loading `oh-my-zsh.sh` script:
 25+<a name="remove-directories-aliases"></a>
 26 
 27-```zsh
 28+If you want to skip default Oh My Zsh aliases (those defined in `lib/*` files) or plugin aliases,
 29+you can use the settings below in your `~/.zshrc` file, **before Oh My Zsh is loaded**. Note that
 30+there are many different ways to skip aliases, depending on your needs.
 31+
 32+```sh
 33+# Skip all aliases, in lib files and enabled plugins
 34+zstyle ':omz:*' aliases no
 35+
 36+# Skip all aliases in lib files
 37+zstyle ':omz:lib:*' aliases no
 38+# Skip only aliases defined in the directories.zsh lib file
 39+zstyle ':omz:lib:directories' aliases no
 40+
 41+# Skip all plugin aliases
 42+zstyle ':omz:plugins:*' aliases no
 43+# Skip only the aliases from the git plugin
 44+zstyle ':omz:plugins:git' aliases no
 45+```
 46+
 47+You can combine these in other ways taking into account that more specific scopes takes precedence:
 48+
 49+```sh
 50+# Skip all plugin aliases, except for the git plugin
 51+zstyle ':omz:plugins:*' aliases no
 52+zstyle ':omz:plugins:git' aliases yes
 53+```
 54+
 55+A previous version of this feature was using the setting below, which has been removed:
 56+
 57+```sh
 58 zstyle ':omz:directories' aliases no
 59 ```
 60 
 61+Instead, you can now use the following:
 62+
 63+```sh
 64+zstyle ':omz:lib:directories' aliases no
 65+```
 66+
 67+#### Notice <!-- omit in toc -->
 68+
 69+> This feature is currently in a testing phase and it may be subject to change in the future.
 70+> It is also not currently compatible with plugin managers such as zpm or zinit, which don't
 71+> source the init script (`oh-my-zsh.sh`) where this feature is implemented in.
 72+
 73+> It is also not currently aware of "aliases" that are defined as functions. Example of such
 74+> are `gccd`, `ggf`, or `ggl` functions from the git plugin.
 75+
 76 ## Getting Updates
 77 
 78 By default, you will be prompted to check for updates every 2 weeks. You can choose other update modes by adding a line to your `~/.zshrc` file, **before Oh My Zsh is loaded**:
 79diff --git a/lib/directories.zsh b/lib/directories.zsh
 80index 13b680c19c8aaba130d06339e55db087a5361b5b..8927a56ad50c4f6f5b4a7bb48bef79da84d0cbf1 100644
 81--- a/lib/directories.zsh
 82+++ b/lib/directories.zsh
 83@@ -4,12 +4,6 @@ setopt auto_pushd
 84 setopt pushd_ignore_dups
 85 setopt pushdminus
 86 
 87-# add (uncommented):
 88-# zstyle ':omz:directories' aliases no
 89-# to your `zshrc` before loading `oh-my-zsh.sh`
 90-# to disable the following aliases and functions
 91-
 92-zstyle -T ':omz:directories' aliases || return 0
 93 
 94 alias -g ...='../..'
 95 alias -g ....='../../..'
 96diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh
 97index 363cfca8baaefd2bb979db97ce6d06682ce89bd0..20d2e354c02a35ab14dd2a6dec7375a624b0acc5 100644
 98--- a/oh-my-zsh.sh
 99+++ b/oh-my-zsh.sh
100@@ -146,22 +146,51 @@ if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then
101   command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock" 
102 fi
103 
104+_omz_source() {
105+  local context filepath="$1"
106+
107+  # Construct zstyle context based on path
108+  case "$filepath" in
109+  lib/*) context="lib:${filepath:t:r}" ;;          # :t = lib_name.zsh, :r = lib_name
110+  plugins/*) context="plugins:${filepath:h2:t}" ;; # :h2 = plugins/plugin_name, :t = plugin_name
111+  esac
112+
113+  local disable_aliases=0
114+  zstyle -T ":omz:${context}" aliases || disable_aliases=1
115+
116+  # Back up alias names prior to sourcing
117+  local -a aliases_pre galiases_pre
118+  if (( disable_aliases )); then
119+    aliases_pre=("${(@k)aliases}")
120+    galiases_pre=("${(@k)galiases}")
121+  fi
122+
123+  # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH
124+  if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then
125+    source "$ZSH_CUSTOM/$filepath"
126+  elif [[ -f "$ZSH/$filepath" ]]; then
127+    source "$ZSH/$filepath"
128+  fi
129+
130+  # Unset all aliases that don't appear in the backed up list of aliases
131+  if (( disable_aliases )); then
132+    local -a disabled
133+    # ${var:|array} gets the list of items in var not in array
134+    disabled=("${(@k)aliases:|aliases_pre}" "${(@k)galiases:|galiases_pre}")
135+    (( $#disabled == 0 )) || unalias "${(@)disabled}"
136+  fi
137+}
138+
139 # Load all of the config files in ~/oh-my-zsh that end in .zsh
140 # TIP: Add files you don't want in git to .gitignore
141 for config_file ("$ZSH"/lib/*.zsh); do
142-  custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}"
143-  [[ -f "$custom_config_file" ]] && config_file="$custom_config_file"
144-  source "$config_file"
145+  _omz_source "${config_file:t2}"
146 done
147 unset custom_config_file
148 
149 # Load all of the plugins that were defined in ~/.zshrc
150 for plugin ($plugins); do
151-  if [[ -f "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" ]]; then
152-    source "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh"
153-  elif [[ -f "$ZSH/plugins/$plugin/$plugin.plugin.zsh" ]]; then
154-    source "$ZSH/plugins/$plugin/$plugin.plugin.zsh"
155-  fi
156+  _omz_source "plugins/$plugin/$plugin.plugin.zsh"
157 done
158 unset plugin
159