Home > Net >  if statement in fish_prompt
if statement in fish_prompt

Time:09-09

I'm trying to customize my fish prompt but i can't seem to put an if statement in it.
What I want : user@host ~/.config [127]>
What i tried :

function fish_prompt 
    echo -n $USER
    echo -n "@"
    echo -n $hostname
    echo -n " "
    echo -n (prompt_pwd)
    echo -n (__fish_git_prompt)
    if [ $status -ne 0 ] 
        echo -n " [$status] "
    end 
    echo -n "> "
end 

I want the $status to show only when there is an error. So far, everything works except the if statement. (i tried the same if directly in the console and it worked, the problem occurs only in the fish_prompt) .
Thanks

CodePudding user response:

echo also returns a status, so $status is updated.

So if you are interested in the commandline's $status, you need to save that before doing anything else.

Do this:

function fish_prompt
    # $status here is what the commandline returned,
    # save it so we can use it later
    set -l laststatus $status
    echo -n $USER
    echo -n "@"
    echo -n $hostname
    echo -n " "
    echo -n (prompt_pwd)
    echo -n (__fish_git_prompt)
    if [ $laststatus -ne 0 ] 
        echo -n " [$laststatus] "
    end 
    echo -n "> "
end 

This is also what fish's sample prompts do

  • Related