Otis Wright
Otis Wright

Reputation: 2110

Trouble writing fish if statement

So I have the following if statement (originally from my .zshrc):

if [ -n "${NVIM_LISTEN_ADDRESS+x}" ]; then
  alias h='nvr -o'
  alias v='nvr -O'
  alias t='nvr --remote-tab'
fi

Now I am trying to port this across to my config.fish, I understand that it is to do with the [ on the if but I can't quite figure out the syntax.

Upvotes: 1

Views: 474

Answers (1)

ridiculous_fish
ridiculous_fish

Reputation: 18551

The [ is OK. The two parts that are different in fish are the +x expansion modifier, and the loop syntax.

In zsh, "${NVIM_LISTEN_ADDRESS+x}" evaluates to x if $NVIM_LISTEN_ADDRESS is set, and empty otherwise; then that is compared to see if it's empty. This seems to be a roundabout way to just check if the variable is set.

Regarding loops, fish blocks always end with end, and there is no need for then or do.

So this is the most direct port to fish:

if [ -n "$NVIM_LISTEN_ADDRESS" ]
    alias h='nvr -o'
    alias v='nvr -O'
    alias t='nvr --remote-tab'
end

And this is more idiomatic:

if set -q NVIM_LISTEN_ADDRESS
    abbr h 'nvr -o'
    abbr v 'nvr -O'
    abbr t 'nvr --remote-tab'
end

which uses the fish abbreviation feature that expands in place.

Upvotes: 2

Related Questions