Skip to content

Remove universal variables #7379

Open
Open

Description

@IlanCosman

There was some discussion of removing the universal variable functionality in #7317. Some good initial ideas were brought up there.

Activity

  1. folke commented on Oct 28, 2020

    @folke
    Contributor

    Loading universal variables from fish_variables is currently much faster than specifying them as globals in your config.fish. See also my proposal #7432

  2. krobelus commented on Jun 19, 2021

    @krobelus
    Contributor

    A lightweight first iteration of this could look like:

    1. Whenever a universal variable is set, also set it to the global scope
    2. Whenever a global variable is set, if it's also defined in the universal scope, then also update it in the universal scope (persisting it)

    This removes support for shadowing of universals with globals, which is probably not used very much[citation needed].

    The next obvious step would be to remove the universal scope, since at this point it's just used as flag whether the variable is persisted.

    In the end we probably want a different interface for universal variables

  3. added this to the fish-future milestone on Jul 28, 2021
  4. planet36 commented on Jul 29, 2021

    @planet36

    Is the universal variable feature going to be removed from fish?

  5. faho commented on Jul 30, 2021

    @faho
    Member

    @planet36 That is why the issue is called "Remove universal variables", yes. It means that we would like to remove universal variables, in their current form.

    They would be replaced with something that doesn't have their drawbacks, see #7317 (comment).

  6. planet36 commented on Jul 31, 2021

    @planet36

    Here's a problem I had that I solved using universal variables. (If there's a better way, please let me know.)

    In my config.fish I set environment variable CXXFLAGS in a block like this.

    if status is-login
        set --export CXXFLAGS -O2 -pipe -Wall -Wextra
    end
    

    The intent is to set my environment variables once upon login, not every invocation of fish.

    Using $CXXFLAGS works as expected at first.

    count $CXXFLAGS

    4

    g++ $CXXFLAGS test.cpp
    success

    But when I open a nested shell, the variable expansion (or maybe it's the splitting) is different.

    open a nested shell
    fish

    count $CXXFLAGS

    1

    g++ $CXXFLAGS test.cpp

    cc1plus: error: argument to '-O' should be non-negative integer, 'g', 's' or 'fast'

    I understand the following work-around, but I'd rather not type it out often.

    count (string split -- ' ' $CXXFLAGS)

    4

    In bash, the environment variable is expanded/split as expected.

    open a nested shell
    bash

    g++ $CXXFLAGS test.cpp
    success

    When I make CXXFLAGS both universal and exported, it always expands as expected, even in nested shells.

    if status is-login
        set --export --universal CXXFLAGS -O2 -pipe -Wall -Wextra
    end
    

    Repeat the example above and notice that count $CXXFLAGS prints the same value.

    Maybe my confusion is a point in favor of removing uvars, idk.

  7. krobelus commented on Jul 31, 2021

    @krobelus
    Contributor

    if status is-login
    set --export CXXFLAGS -O2 -pipe -Wall -Wextra
    end

    You can remove the if and set it as global variable in every new shell.

    Or keep using the universal one; the replacement should be as powerful.
    I would only export it if really necessary.

  8. faho commented on Jul 31, 2021

    @faho
    Member

    @planet36 What is happening here is two things:

    1. Fish can't reconstruct lists from variables it imports (unix doesn't know lists, so it collapses down to one string)
    2. You're using "is-login" uselessly

    Really, if you just want to "set a variable", you should probably do that in all shells, or at least interactive ones. There is no need to only do it sometimes, in some shells that your environment gets to specifically pick.

    You're also missing a --global - "exported" is not a scope.

    One issue using a universal variable here can cause is that it makes overriding it sometimes awkward. Say you want to pick different CXXFLAGS for a thing. So you go there in your shell, set CXXFLAGS --my-cool-flag. You want to quickly check something, so you open another shell and run a few commands there, you close it and go back to your old shell. You had set the variable, right? Well, yeah, and then your config set it back!

    (@krobelus this is also why I'm against merging universal and global scopes, btw)

  9. floam commented on Nov 5, 2021

    @floam
    Member

    I'm not convinced. "I don't use them and here's why" is immaterial.

  10. floam commented on Nov 5, 2021

    @floam
    Member

    Users have never needed a config file with fish. Hell, I haven't either. Universal variables are a fish killer feature.

  11. krobelus commented on Nov 6, 2021

    @krobelus
    Contributor

    uvars are often misunderstood by users (resulting in huge $PATH variables).
    We are trying to replace them with something simpler that keeps the good features.

    IOW, image

  12. floam commented on Nov 6, 2021

    @floam
    Member

    🧐 … Well that's reassuring, Mr Ulbricht.

    I had just noticed the documentation has been changed to encourage folks to make config files with set -g commands, generally discouraging set -U, references touting uvars as a feature have been removed, and I noticed #7379 Remove universal variables (this is it) and #7317 Remove default universal variables, and the issues were left open and milestoned fish-future. A PR was nearly merged that switched all uvars that were set on first launch to globals.

    So I thought folks were thinking to do away with universal variables and maybe replace them with some mechanism that just saves/maintains init scripts, honestly.

  13. krobelus commented on Nov 6, 2021

    @krobelus
    Contributor

    I thought someone was thinking to do away with universal variables and replace them with some mechanism that just saves maintains an init script, honestly.

    yeah that's a possible solution

  14. floam commented on Nov 6, 2021

    @floam
  15. added 2 commits that reference this issue on Nov 17, 2021
    e6044dd
    dd4e822
  16. Bit0r commented on Dec 8, 2024

    @Bit0r

    When the user uses set -U var value, the set -g var value is automatically written to ~/.config/fish/conf.d/universal.fish, how about the idea?

  17. Darkshadow2 commented on Jan 15, 2026

    @Darkshadow2
    Contributor

    I was curious what this might be like, so I decided to create some functions that implement it. I thought I'd share in case anyone else would like to try it out. It works really well, I don't really notice a difference between this and using universal variables.

    It does cheat a little and use a universal variable to communicate changes between multiple sessions, but other than this deals only with global variables.

    I want to stress this is just my personal take on how this might work. The final implementation could be similar or very different. I'm not working on implementing this.

    How to use

    Copy the below functions to a new file in ~/.config/fish/conf.d/ or $XDG_CONFIG_HOME/fish/conf.d/ (whichever is appropriate for your system). My suggested filename is persist_variables.fish, but you can name it however you want (but do read the known issues). You should create a new file for this as it will reload from whatever file it is placed in from time to time. Once you've created the file, source it to load it.

    Use the persist_variable function to persist variables. It accepts multiple variable names as arguments, so you can add more than one at a time. It also accepts the option -r/--remove-universal to convert a universal variable into a global one in addition to persisting it. If you do not use -r/--remove-universal, the universal variable will be left alone. I recommend using the flag to convert universal variables.

    Use the unpersist_variable function to stop a variable from being persisted. It accepts multiple variable names as arguments, so you can stop persisting more than one at a time. It also accepts the option -c/--create-universal to convert the variable into a universal one. Very big note with unpersist_variable: it does not delete/remove variables passed to it. It only stops a variable from being persisted.

    Both functions have completions.

    Known issues / quirks

    • The functions below should load after the persist file itself. If the functions load before the persist file is, the event handler will fire for every variable in the persist file as it is loaded, which will have the event handler recreating the persist file multiple times. This will not cause issues, but will create unnecessary file IO. The persist file is saved to ~/.config/fish/conf.d/0000-persist.fish or $XDG_CONFIG_HOME/fish/conf.d/0000-persist.fish, so as long as whatever you name the file the persist functions are in something that will not load before that, it will be fine.
    • If you delete/remove a variable (set -e somevar), the variable will be removed from all sessions, but it will still be considered persisted. If at some point later you do set -g somevar someval, it will be persisted again. If you want a value to be both removed and not persisted, first do set -e somevar and then unpersist_variable somevar.
    • If you have a persisted variable that can change in an automated way (with an event handler, for instance), this will cause all sessions to update the persist file. This will not cause problems, but can create a lot of file IO.
    • The -c/--create-universal option of unpersist_variable removes the global variable, and then one second later creates the universal variable. This is done to avoid multiple sessions from writing the persist file when the universal variable is created. It does mean that for one second, the variable does not exist.
    • If you create a local variable or universal variable with the same name as a variable that is persisted, the event handler will fire and update the persist file. However, the event handler cannot see the value of the local/universal variable, so uses the value of the global one.
    • There are variables that have been set to not be persisted. Most of these are read only variables, but there are a few, like PATH, that I've added because they could cause fish or things running to break or be unpredictable.

    If anyone runs into an issue using this, do please let me know.

    $\textsf{\color{blue}{persist\_variables.fish}}$

    if set -gxq __persist_no_load
        return
    end
    
    if not set -gq __persisted_variables
        set -g __persisted_variables
    end
    
    if not set -Uq __persisted_var_notifier
        set -U __persisted_var_notifier $fish_pid
    end
    set -l __persist_file $__fish_config_dir/conf.d/0000-persist.fish
    
    if not functions -q __update_persisted
        set -l __no_persist _ __fish_bin_dir __fish_cache_dir __fish_cd_direction __fish_config_dir \
            __fish_data_dir __fish_help_dir __fish_initialized __fish_man_dir __fish_sysconf_dir \
            __fish_uname __fish_vendor_completionsdirs __fish_vendor_confdirs __fish_vendor_functionsdirs \
            __persist_file __persisted_variables __persisted_var_notifier CMD_DURATION COLUMNS dirnext \
            dirprev EUID fish_complete_path fish_features fish_function_path fish_kill_signal \
            fish_killring fish_pid fish_terminal_color_theme FISH_VERSION history HOME hostname IFS \
            LINES PATH pipestatus PWD SHLVL status status_generation TERM USER version
    
        function __update_persisted --inherit-variable __persist_file \
            --inherit-variable __no_persist \
            --on-variable __persisted_variables
            if set -q __persisted_variables
                for i in $__no_persist
                    if contains -- $i $__persisted_variables
                        set -l idx (contains -i -- $i $__persisted_variables)
                        set -e __persisted_variables[$idx]
                        echo "$(set_color $fish_color_error)Will not persist '$i'$(set_color normal)" >&2
                    end
                end
                source (status filename) # We need to rebuild the persist function
                __persist_variables
            else
                echo "`persisted_variables` variable was erased. In case this was an accident,"
                echo "this is not being persisted (otherwise $(set_color -i)all persisted variables" \
                    "would be removed$(set_color normal))."
                echo
                echo "If it was an accident, type $(echo "source $__persist_file" | fish_indent --ansi) to restore it"
                echo "If you do wish to remove all persisted variables, type" \
                    (echo "set -g __persisted_variables" | fish_indent --ansi)
            end
        end
    
        function __persist_tmp_file -a filename
            set -l tmp_file (__fish_mktemp_relative $filename)
            if not string length -q -- $tmp_file
                set -l tmpdir (path filter -d -r -w $TMPDIR /tmp)
                if set -q tmpdir[1]
                    set tmp_file $tmpdir[1]/$filename-$fish_pid.fish
                else
                    set tmp_file $__fish_config_dir/$filename-$fish_pid.fish
                end
            end
            echo $tmp_file
        end
    
        function persist_variable --inherit-variable __no_persist --description "Adds variables to be persisted"
            if not string length -q -- $argv
                return
            end
            argparse --name=persist_variable r/remove-universal -- $argv || return 1
            set -l np $__persisted_variables
            set -l added_variable false
            for variable in $argv
                if not string match -qr -- "^(?:$(string join -- '|' $__no_persist))\$" $variable
                    if not contains -- $variable $__persisted_variables
                        # If the variable is universal, we create a global
                        if set -Uq $variable
                            if not set -gq $variable
                                if set -Uxq $variable && set -Uq --path $variable
                                    set -gx --path --no-event $variable $$variable
                                else if set -Uxq $variable
                                    set -gx --no-event $variable $$variable
                                else if set -Uq --path $variable
                                    set -g --path --no-event $variable $$variable
                                else
                                    set -g --no-event $variable $$variable
                                end
                            end
                            if string length -q -- $_flag_r
                                set -Ue --no-event $variable
                            end
                        end
                        set added_variable true
                        set -a np $variable
                    else
                        echo "Variable '$variable' is already persisted" >&2
                    end
                else
                    echo "$(set_color $fish_color_error)Will not persist '$variable'$(set_color normal)"
                end
            end
            if $added_variable
                # This sorts the variables
                set __persisted_variables (path sort $np)
            end
        end
        function __persist_completions --inherit-variable __no_persist --description='Completions for persist_variable'
            set -l vars (set -n | string match -rv -- "^(?:$(string join -- '|' $__no_persist))\$" | \
                string match -rv -- '^(?:___|XDG_)' | string match -rv -- "^(?:$(string join -- '|' (set -ln))\$)" | \
                string match -rv -- "^(?:$(string join -- '|' $__persisted_variables))\$")
            for var in $vars
                if set -Uq $var
                    printf '%s\tUniversal\n' $var
                else
                    printf '%s\tGlobal\n' $var
                end
            end
        end
        complete -c persist_variable -f -s r -l remove-universal -d "Remove universal variables"
        complete -c persist_variable -f -a "(__persist_completions)"
    
        function unpersist_variable --description "Stops variables from being persisted"
            if not string length -q -- $argv
                return
            end
            set -l removed_variable false
            set -l unp $__persisted_variables
            argparse --name=unpersist_variable c/create-universal -- $argv || return 1
            for variable in $argv
                set -l idx (contains -i -- $variable $unp)
                if set -q idx[1]
                    set removed_variable true
                    set -e unp[$idx]
                    if string length -q -- $_flag_c
                        if not set -q __persist_to_universal
                            set -g __persist_to_universal
                        end
                        if not set -q __unpersist_file
                            set -g __unpersist_file (__persist_tmp_file unpersist_variables)
                        end
                        set -a __persist_to_universal $variable
                        # This is to stop multiple sessions from creating a lot of disk IO
                        if set -xq $variable && set -q --path $variable
                            echo "set --universal --export --path --no-event $variable" \
                                (string escape --style=script -- $$variable) >>$__unpersist_file
                        else if set -xq $variable
                            echo "set --universal --export --no-event $variable" \
                                (string escape --style=script -- $$variable) >>$__unpersist_file
                        else if set -q --path $variable
                            echo "set --universal --path --no-event $variable" \
                                (string escape --style=script -- $$variable) >>$__unpersist_file
                        else
                            echo "set --universal --no-event $variable" \
                                (string escape --style=script -- $$variable) >>$__unpersist_file
                        end
                        set -ge --no-event $variable
                    end
                else
                    echo "$(set_color $fish_color_error)$variable is not persisted$(set_color normal)" >&2
                end
            end
            if $removed_variable
                set __persisted_variables $unp
            end
            if string length -q -- $__unpersist_file
                # Launch another instance of fish to load the universals after one second
                set -lx __persist_no_load true
                fish -c "sleep 1; source $__unpersist_file && rm $__unpersist_file" &; disown
                set -e __unpersist_file
            end
        end
        function __unpersist_completions --description='Completions for unpersist_variable'
            printf '%s\n' $__persisted_variables
        end
        complete -c unpersist_variable -f -s c -l create-universal -d "Create universal variables"
        complete -c unpersist_variable -f -a "(__unpersist_completions)"
    
        function __persisted_variable_notifier --inherit-variable __persist_file --on-variable __persisted_var_notifier
            functions -e __persist_variables
            source $__persist_file
            source (status filename) # Rebuild the __persist_variables function
        end
    end
    
    functions -q __persist_variables && functions -e __persist_variables
    
    begin
        # If persisted_variables is empty, do not listen for any variables
        if set -q __persisted_variables[1]
            echo "function __persist_variables --on-variable" \
                (string join -- ' --on-variable ' $__persisted_variables)
        else
            echo "function __persist_variables"
        end
        echo "    set -l ____persist_file \$(__persist_tmp_file persist)"
        echo "    echo '# Persisted variables autogenerated by __persist_variables' >\$____persist_file"
        echo "    echo '# DO NOT EDIT THIS FILE, changes can be overwritten' >>\$____persist_file"
        echo "    echo '# Use the persist_variable or unpersist_variable functions instead' >>\$____persist_file"
        echo "    echo >>\$____persist_file"
        echo "    echo \"set --global --no-event __persisted_variables\" (string escape --" \
            "\$__persisted_variables) >>\$____persist_file"
        echo "    for ____variable in $__persisted_variables"
        echo "        if not set -q \$____variable"
        echo "            echo \"set --global --query \$____variable && set --global --erase" \
            "\$____variable\" >>\$____persist_file"
        echo "            continue"
        echo "        end"
        echo "        set -l ____path"
        echo "        set -l ____export"
        echo "        if set -xq \$____variable"
        echo "            set ____export --export"
        echo "        end"
        echo "        if set --path -q \$____variable"
        echo "            set ____path --path"
        echo "        end"
        echo "        echo \"set --global\" \$____export \$____path \$____variable" \
            "(string escape -- \$\$____variable) >>\$____persist_file"
        echo "    end"
        echo "    if set -q __persist_to_universal"
        echo "        # Remove global variables that were unpersisted and set back to universal variables"
        echo "        printf '\n# Removed persisted variables that are now universal variables:\n\n' >>\$____persist_file"
        echo "        for ____variable in \$__persist_to_universal"
        echo "            echo \"set --global --query \$____variable && set --global --erase --no-event" \
            "\$____variable\" >>\$____persist_file"
        echo "        end"
        echo "        set -e __persist_to_universal"
        echo "    end"
        echo "    if command -q flock"
        echo "        set -lx __persist_no_load true"
        echo "        command flock -x $__persist_file -c \"cat \$____persist_file > $__persist_file\" && rm \$____persist_file"
        echo "    else"
        echo "        cp \$____persist_file $__persist_file && rm \$____persist_file"
        echo "    end"
        echo "    set --no-event __persisted_var_notifier \$(random)"
        echo end
    end | source
    
    if not test -f $__persist_file
        __persist_variables
    end

    Edit 2026/01/20: Fixed an issue that could cause a hang when launching a sub shell

  18. andradei commented on Jan 27, 2026

    @andradei

    Managing persistent variables is a good idea. But I think this juggling of state and other things is very cumbersome to implement, maintain, and use.

    Persistent variables should be override-able by tools and scripts if they are exported (don't know how that would be achieved in a simple way, though), this way tools can manipulate them as needed. Maybe they could also have a comment on top showing what process last change them to help with debugging, I'm not sure.

    In case it's decided that persistent variables will never be exported, all exported ones would be global and set in config.fish or conf.d/ and loaded at shell start up like other shells.

  19. andradei commented on Jan 27, 2026

    @andradei

    Or... limit exported state to globals only (set --export would imply global scope always) and all universal vars would be manipulated by fish internally and by user in XGD_CONFIG_HOME/.config/fish/persitent.fish and CLI.

  20. kaddkaka commented on Apr 7, 2026

    @kaddkaka

    Today I was confused for over an hour because I had accidentally gotten a path added to the universal variables file. I had no idea that file existed and don't remember running a command that would have added it. (As a new fish user I probably - against better knowing - ran an interactive fish_add_path command and didn't know the consequences)

    What happened: upon startup /bin got added to the start of my path from the universal variable file. This meant that when I activated a nix-environment with nix-shell my_env.nix, I didn't see the binary from my nix configuration, because a new shell was spawned, prepending /bin, shadowing my nix path and effectively making my nix environment un-discoverable.

    I would have been better of if the PATH env var setting was not universally saved.

    While waiting for universal variables to be removed. Is there any setting or workaround I can do to disable/block universal variables from being added?

  21. jon-munoz commented on Apr 17, 2026

    @jon-munoz

    I was also confused by this for over an hour trying to remove paths to binaries in a git work tree that were conflicting with the paths to another git work tree. I'm new to fish and probably misused the fish_add_path command as well.

    I ended up just deleting the universal PATH variable with set --erase --universal PATH.

    I then oscillated between running set -gx PATH and set -gx PATH (string match -v /path/to/unwanted/dir/bin $PATH) and restarting my computer to verify the results with set --show PATH.

    If I misunderstood my own problem/solution, or if there's a better way to avoid this foot-gun I'm all ears.

  22. ridiculousfish commented on Apr 17, 2026

    @ridiculousfish
    Member

    @jon-munoz thanks for sharing. You certainly aren't the first to trip over PATH handling, in any shell.

    To be sure, $PATH is not a universal variable. If you try to make it one, you'll get a warning. It's just an ordinary global exported variable, same as any other shell.

    If you look in a typical .bashrc or .zshrc you'll probably see a bunch of lines like:

    export PATH="/home/me/bin:$PATH"
    export PATH="/opt/foo/bin:$PATH"

    This works fine in fish if you prefer to manage your $PATH like that.

    What fish_add_path does is automate this by storing those additional path entries in a universal variable named $fish_user_paths. It's as if you ran in bash:

    user_paths="/home/me/bin:/opt/foo/bin"
    export PATH="$user_paths:$PATH
    

    So if you're making a change to PATH that you want to be local to a session - not persistent - you either would modify $PATH directly like you would in zsh (or you can use fish_add_path with the --path option).

    The important point is that $PATH works basically the same in fish as in other shells and you can use the same techniques to manage it, if you prefer. It's not universal, and you are free to ignore universal variables entirely if you prefer to manage it manually!

    That doesn't get your time back but hopefully it helps clear things up for you or the next reader.

  23. krobelus commented on Apr 19, 2026

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions