How can I set environment variables in fish?
First, the fish config file is ~/.config/fish/config.fish. Editing the file you named won't have any effect. Second, fish does not have any default for, nor does it set, the EDITOR or VISUAL variables. So whatever is setting it to /usr/bin/nano is a customization unique to your system.
If you set -Ux EDITOR vim
and do not set it in config.fish it should be set to vim even if it is already set when fish starts. Run that set command then do set -U | grep EDITOR
and env | grep EDITOR
to see that it is set as a universal variable and exported. Now type fish
to start a sub-shell. Run the previous two commands and you should see that it is still set to the same value. Now type set -U EDITOR nano
in the sub-shell followed by exit
. In the earlier shell you should now see that EDITOR is set to nano.
Personally I don't like to use uvars for this since at the moment they are per-machine. I just do set -gX EDITOR (type -p vim)
in my ~/.config/fish/config.fish. This ensures that if I start fish on a new machine on which I've installed my ~/.config/fish directory I get my expected defaults.
The other reason not to use a uvar in this way is that the resolution order is local scope, global scope, universal scope. Since environment vars imported when fish starts running are placed in the global scope they will shadow the uvar you defined. Do not export universal variables. It is unlikely to produce the desired results. Simply set -gx
the var in your config.fish script.
P.S., When asking questions of this nature you should always include pertinent facts such as the OS you're using and the fish version.
Jonathan
Updated on June 16, 2022Comments
-
Jonathan almost 2 years
I'm new to the fish shell, and just trying to set my $EDITOR variable so that's it's persistent across sessions and reboots. Here's what I've tried so far:
- Running
set -gx EDITOR vim
from the command line. - Running
set -Ux EDITOR vim
from the command line. - Running those commands, prefixed by
set -e EDITOR
to unset any previous value. - Adding the above commands to my
~/.config/fish/config.fish
file (it complainsset: Warning: universal scope selected, but a global variable “EDITOR” exists.
) - Uninstalling
oh-my-fish
and removing all fish configs to start from scratch.
No matter what I do, the EDITOR variable always ends up being
/usr/bin/nano
whenever I open a new terminal, start a new session, or reboot. What's even more strange is that in~/.config/fish/fishd.my-hostname
, I seeSET_EXPORT EDITOR vim
, and nothing about nano. Is this some kind of fish default? If so, how can I set this correctly?Edit: I'm running Fish 2.6.0 on Antergos Linux.
- Running