From 1ad167dfac325a9f92e0693c70d0ab3f7c4c574b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20Cornell=C3=A0?= Date: Mon, 3 Apr 2023 23:14:36 +0200 Subject: [PATCH] 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 --- README.md | 54 ++++++++++++++++++++++++++++++++++++++++----- lib/directories.zsh | 6 ----- oh-my-zsh.sh | 45 ++++++++++++++++++++++++++++++------- 3 files changed, 86 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4f0aeb7b4..556d4c8c5 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,9 @@ To learn more, visit [ohmyz.sh](https://ohmyz.sh), follow [@ohmyzsh](https://twi - [Manual Installation](#manual-installation) - [Installation Problems](#installation-problems) - [Custom Plugins and Themes](#custom-plugins-and-themes) + - [Skip aliases](#skip-aliases) - [Getting Updates](#getting-updates) + - [Updates verbosity](#updates-verbosity) - [Manual Updates](#manual-updates) - [Uninstalling Oh My Zsh](#uninstalling-oh-my-zsh) - [How do I contribute to Oh My Zsh?](#how-do-i-contribute-to-oh-my-zsh) @@ -276,16 +278,58 @@ If you have many functions that go well together, you can put them as a `XYZ.plu 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/`. -### Remove directories aliases +### Skip aliases -If you want to skip ohmyzsh default -[directories aliases](https://github.com/ohmyzsh/ohmyzsh/blob/master/lib/directories.zsh) you can add the -following snippet to your `zshrc`, before loading `oh-my-zsh.sh` script: + -```zsh +If you want to skip default Oh My Zsh aliases (those defined in `lib/*` files) or plugin aliases, +you can use the settings below in your `~/.zshrc` file, **before Oh My Zsh is loaded**. Note that +there are many different ways to skip aliases, depending on your needs. + +```sh +# Skip all aliases, in lib files and enabled plugins +zstyle ':omz:*' aliases no + +# Skip all aliases in lib files +zstyle ':omz:lib:*' aliases no +# Skip only aliases defined in the directories.zsh lib file +zstyle ':omz:lib:directories' aliases no + +# Skip all plugin aliases +zstyle ':omz:plugins:*' aliases no +# Skip only the aliases from the git plugin +zstyle ':omz:plugins:git' aliases no +``` + +You can combine these in other ways taking into account that more specific scopes takes precedence: + +```sh +# Skip all plugin aliases, except for the git plugin +zstyle ':omz:plugins:*' aliases no +zstyle ':omz:plugins:git' aliases yes +``` + +A previous version of this feature was using the setting below, which has been removed: + +```sh zstyle ':omz:directories' aliases no ``` +Instead, you can now use the following: + +```sh +zstyle ':omz:lib:directories' aliases no +``` + +#### Notice + +> This feature is currently in a testing phase and it may be subject to change in the future. +> It is also not currently compatible with plugin managers such as zpm or zinit, which don't +> source the init script (`oh-my-zsh.sh`) where this feature is implemented in. + +> It is also not currently aware of "aliases" that are defined as functions. Example of such +> are `gccd`, `ggf`, or `ggl` functions from the git plugin. + ## Getting Updates 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**: diff --git a/lib/directories.zsh b/lib/directories.zsh index 13b680c19..8927a56ad 100644 --- a/lib/directories.zsh +++ b/lib/directories.zsh @@ -4,12 +4,6 @@ setopt auto_pushd setopt pushd_ignore_dups setopt pushdminus -# add (uncommented): -# zstyle ':omz:directories' aliases no -# to your `zshrc` before loading `oh-my-zsh.sh` -# to disable the following aliases and functions - -zstyle -T ':omz:directories' aliases || return 0 alias -g ...='../..' alias -g ....='../../..' diff --git a/oh-my-zsh.sh b/oh-my-zsh.sh index 363cfca8b..20d2e354c 100644 --- a/oh-my-zsh.sh +++ b/oh-my-zsh.sh @@ -146,22 +146,51 @@ if command mkdir "${ZSH_COMPDUMP}.lock" 2>/dev/null; then command rm -rf "$ZSH_COMPDUMP.zwc.old" "${ZSH_COMPDUMP}.lock" fi +_omz_source() { + local context filepath="$1" + + # Construct zstyle context based on path + case "$filepath" in + lib/*) context="lib:${filepath:t:r}" ;; # :t = lib_name.zsh, :r = lib_name + plugins/*) context="plugins:${filepath:h2:t}" ;; # :h2 = plugins/plugin_name, :t = plugin_name + esac + + local disable_aliases=0 + zstyle -T ":omz:${context}" aliases || disable_aliases=1 + + # Back up alias names prior to sourcing + local -a aliases_pre galiases_pre + if (( disable_aliases )); then + aliases_pre=("${(@k)aliases}") + galiases_pre=("${(@k)galiases}") + fi + + # Source file from $ZSH_CUSTOM if it exists, otherwise from $ZSH + if [[ -f "$ZSH_CUSTOM/$filepath" ]]; then + source "$ZSH_CUSTOM/$filepath" + elif [[ -f "$ZSH/$filepath" ]]; then + source "$ZSH/$filepath" + fi + + # Unset all aliases that don't appear in the backed up list of aliases + if (( disable_aliases )); then + local -a disabled + # ${var:|array} gets the list of items in var not in array + disabled=("${(@k)aliases:|aliases_pre}" "${(@k)galiases:|galiases_pre}") + (( $#disabled == 0 )) || unalias "${(@)disabled}" + fi +} + # Load all of the config files in ~/oh-my-zsh that end in .zsh # TIP: Add files you don't want in git to .gitignore for config_file ("$ZSH"/lib/*.zsh); do - custom_config_file="$ZSH_CUSTOM/lib/${config_file:t}" - [[ -f "$custom_config_file" ]] && config_file="$custom_config_file" - source "$config_file" + _omz_source "${config_file:t2}" done unset custom_config_file # Load all of the plugins that were defined in ~/.zshrc for plugin ($plugins); do - if [[ -f "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" ]]; then - source "$ZSH_CUSTOM/plugins/$plugin/$plugin.plugin.zsh" - elif [[ -f "$ZSH/plugins/$plugin/$plugin.plugin.zsh" ]]; then - source "$ZSH/plugins/$plugin/$plugin.plugin.zsh" - fi + _omz_source "plugins/$plugin/$plugin.plugin.zsh" done unset plugin