This post has been replaced with a newer post that works with SDKMAN.
The Fish Shell is awesome but it doesn’t have a Posix-compatible shell language like Bash or ZSH. This is a problem when you’re trying to use third-party tools that expect a Posix-compatible shell. Tools like GVM, the Groovy enVironment Manager.
GVM, like it’s inspiration RVM, lets a developer switch between versions of the language toolchain. This is important because modern languages are moving targets and building an application against one version of the toolchain isn’t guaranteed to run against later versions.
GVM, in particular, wires itself into the Bash and ZSH shells. The best approach to getting GVM to work with Fish is to rewrite it for Fish. But the developers for GVM don’t seem inclined to do that. So we have to cheat.
There’s an existing bit of code in Oh My Fish that’s supposed to make GVM work with Fish but that code doesn’t actually work. So I put together an alternative that works cleanly. The first bit of code uses a Fish function to invoke gvm. It goes in the “~/.config/fish/functions” directory and must be named “~/.config/fish/functions/gvm.fish”.
This code works by running the GVM ‘init’ command and then the actual ‘gvm’ command in a Bash shell. It compares the environment before and after running those commands and applies the new or changed environment variables against the Fish environment. It does the same for the PATH environment variable.
The next bit of code goes into the “~/.config/fish/config.fish” file. This is the main configuration file for Fish.
This bit of code looks like the non-functioning code in Oh My Fish. But here it’s used correctly, to setup the initial paths for the shell. It works by adding all of the “current” directories maintained by GVM into the initial shell path.
The last bit of code is optional, but, ironically, is also the most complicated. It provides command-line completion for the ‘gvm’ command. It goes in a new file named “~/.config/fish/completions/gvm.fish”. The completion script parses the installed versions and will provide version number completion when using the ‘gvm’ command.