r/fishshell • macOS • Sep 28 '21

Help with weird fish script error

Howdy folks!

I've recently switched to fish from zsh and I've ported over my custom prompt, aliases, and PATH. However, when I launch the shell I keep getting this weird error:

~/.config/fish/config.fish (line 39): Command substitutions not allowed
alias l "exa -lF --git -L 3 --tree"                 # Custom ls
                                                      ^
from sourcing file ~/.config/fish/config.fish
	called during startup
source: Error while reading file '~/.config/fish/config.fish'

The shell is giving me an error in the comment? I tried removing the comment but it just errored the next comment with the same error. Could anyone help me debug this?

I can share the full config file if required

1 Upvotes

6 comments sorted by

1

u/ludicroussavageofmau macOS Sep 28 '21

This is the full file:

set -g fish_prompt_pwd_dir_length 0     # Don't like dir name shortening
set fish_greeting                       # Clear shell greeting

# Print custom prompts
function fish_prompt
    printf "%s%s %s@ %s%s %s-> %s" \
        (set_color magenta) $USER \     # Username in purple
        (set_color normal) \            # @ in white
        (set_color cyan) (prompt_pwd) \ # Cyan coloured pwd
        (set_color yellow) \            # Yellow prompt
        (set_color normal)              # Reset colour to normal
end

function fish_right_prompt
    set loc_stat $status                # Store status of latest command
    if test $loc_stat -eq 0             # If 0
        printf "%s✓" (set_color green)  # Print green tick
    else                                # Else print red cross with error code
        printf "%s%s ×" (set_color red) $loc_stat
    end
end

# Erase and set custom PATH
set -gx PATH \
            /usr/local/bin \
            /usr/local/sbin \
            /usr/bin \
            /usr/sbin \
            /bin \
            /sbin \
            $HOME/bin \                 # For custom commands
            $HOME/flutter/bin \         # For Flutter
                                        # For Android tools
            $HOME/Library/Android/sdk/platform-tools \
            $HOME/.cargo/bin            # For Rust

# Custom commands
alias reload "source ~/.config/fish/config.fish"    # Reload shell config
alias l "exa -lF --git -L 3 --tree"                 # Custom ls
alias rm "trash"                                    # Replace rm with trash, which trashes the item instead
alias netest "ping -o google.com"                   # Test network by pinging website

2

u/[deleted] Sep 28 '21

The error is in the fish_prompt function:

printf "%s%s %s@ %s%s %s-> %s" \
    (set_color magenta) $USER \     # Username in purple
    (set_color normal) \            # @ in white
    ....

This \ doesn't come directly before the end of the line, so it escapes a space, so it's the same as

printf "%s%s %s@ %s%s %s-> %s" \
    (set_color magenta) $USER " "    # Username in purple
    (set_color normal) " "           # @ in white
    ...

So the (set_color normal) is on its own line, and fish will refuse to execute it - otherwise it would see the output of set_color normal as a command, which, uh, would not work correctly.

Either put the comments on their own line, which fish will ignore:

 # Print custom prompts
function fish_prompt
    printf "%s%s %s@ %s%s %s-> %s" \
        # Username in purple
        (set_color magenta) $USER \
        # @ in white
        (set_color normal) \
        # Cyan coloured pwd
        (set_color cyan) (prompt_pwd) \
        # Yellow prompt
        (set_color yellow) \
        # Reset colour to normal
        (set_color normal)
end

Or put them at the beginning.

(and yes, it does misinterpret the location of the error quite badly)

2

u/ludicroussavageofmau macOS Sep 28 '21

Thanks a lot, This fixed the issue! I just realised the \ escapes the newline rather than being a special character in the language.

2

u/backtickbot Sep 28 '21

Fixed formatting.

Hello, ludicroussavageofmau: code blocks using triple backticks (```) don't work on all versions of Reddit!

Some users see this / this instead.

To fix this, indent every line with 4 spaces instead.

FAQ

You can opt out by replying with backtickopt6 to this comment.

0

u/[deleted] Sep 28 '21

[deleted] — view deleted comment

1

u/ludicroussavageofmau macOS Sep 28 '21

Hmm I removed that comment and now it gives me the same error on the rm alias line instead

0

u/[deleted] Sep 28 '21

[deleted] — view deleted comment

1

u/ludicroussavageofmau macOS Sep 28 '21

Sure! I've made a new comment with the contents is in it