1

To add something to $PATH in fish, I use

fish_add_path -a ~/foo/bar

Then fish adds ~/foo/bar to my ~/.config/fish/fish_variables:

SETUVAR fish_user_paths:/Users/john/foo/bar

Then, is it correct to say that to remove /Users/john/foo/bar from $PATH, I have two options:

  • to edit the fish_variables file

  • to use the following function:

    // https://github.com/fish-shell/fish-shell/issues/8604#issuecomment-1169638533
    function remove_path
      if set -l index (contains -i "$argv" $fish_user_paths)
        set -e fish_user_paths[$index]
        echo "Removed $argv from the path"
      end
    end
    

And the second part of the question: How to properly use that function? I saved it as remove_from_path.fish, executed as remove_from_path.fish "/Users/john/foo/bar", but it doesn't seem to remove /Users/john/foo/bar from $PATH. What I'm doing wrong?

1 Answer 1

3

I saved it as remove_from_path.fish, executed as remove_from_path.fish "/Users/john/foo/bar"

If you save it as a script file, fish will run that file, define the function, and then not do anything.

You would typically save this as a fish function - either put it in your ~/.config/fish/config.fish, or save it as a standalone file in ~/.config/fish/functions/remove_path.fish (the name must match the function name!), and then run it as just

remove_path /Users/john/foo/bar

without the ".fish". With that, fish knows the function and calls it.

Or, if you want to use the script file, you would call the function inside:

function remove_path
    # code here
end

# Now call the function we just defined:
remove_path $argv

This will start a second fish instance, which will then remove the path. This works because universal variables are shared across all fish instances, but is a bit silly.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.