Last Updated: February 25, 2016
· hauleth

`rbenv shell` and fish

Last time I switched to fish shell and have some problems with rbenv shell command. So I needed to write own wrapper that simulate function provided by rbenv init - and here it go:

set -x PATH $HOME/.rbenv/shims $PATH
rbenv rehash >/dev/null ^&1

function rbenv_shell
  set -l vers $argv[1]

  switch "$vers"
    case '--complete'
      echo '--unset'
      echo 'system'
      exec rbenv-versions --bare
    case '--unset'
      set -e RBENV_VERSION
    case ''
      if [ -z "$RBENV_VERSION" ]
        echo "rbenv: no shell-specific version configured" >&2
        return 1
        echo "$RBENV_VERSION"
    case '*'
      rbenv prefix "$vers" > /dev/null
      set -g -x RBENV_VERSION "$vers"

function rbenv
  set -l command $argv[1]
  [ (count $argv) -gt 1 ]; and set -l args $argv[2..-1]

  switch "$command"
    case shell
      rbenv_shell $args
    case '*'
      command rbenv $command $args

11 Responses
Add your response


Thanks, I spent an hour on this.

over 1 year ago ·

Minor bug: you can add [ (count $argv) -eq 0 ]; and set -l command '' at 32th line to prevent bug on invoking rbenv without an argument =P

over 1 year ago ·

@jtomaszewski There is no need to do that. In fish (like in any other shell) when you try to access to non-existent variable then you get empty value.

over 1 year ago ·

Well, yes, but even that, it triggers an error on switch command. f.e. switch $idontexist; end

over 1 year ago ·

@jtomaszewski give example when this snippet doesn't work. I cannot found that one. But if it really bother you then you can write set -l command "$argv[1]".

over 1 year ago ·

Yeah, your way is better. The example is this and only this: rbenv. Ofcourse, unless you're not lazy as me, you can always write rbenv help.
Anyway, nevermind, the problem is unimportant.

over 1 year ago ·

@jtomaszewski Fish allow setting empty variables so there is no need to create fallback to empty string, so if there is no arguments then rbenv will be called without any arguments. Everything works as expected.

over 1 year ago ·

@hauleth It is not in the creation that the error happens, but in the switch. It doesn't matter if the variable exists or is empty because the variable is expanded and the result is a switch call without arguments. If you enclose it in quotes, like you did in the rbenv_shell function then you have one argument that happens to be empty. ;D

Thanks for this. It is great. Have you considered submitting it to oh-my-fish?

over 1 year ago ·

@hauleth Ok, one more problem: You are returning 1 after --unset. Also, unless I am misunderstanding the way return and switch work on fish, empty returns don't seem necessary since they are each the last command of the function.

over 1 year ago ·

rbenv master now supports fish:

$ rbenv init fish
# Load rbenv automatically by adding
# the following to ~/.config/fish/config.fish:

. (rbenv init -|psub)
over 1 year ago ·

Thank you, this worked.

over 1 year ago ·