Bash to Fish Conversion: Display custom message when CD’ing into a specific directory

Basically, I’d like to create a file .cd-reminder with an announcement/message inside a specific directory. It will be displayed every time someone ‘cd’ into that specific directory.

There is a shell script for that already and I’m currently using Fish and not familiar on how to convert it; any help appreciated!

reminder_cd() { 
    builtin cd "[email protected]" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; }
}

alias cd=reminder_cd`

Answer

function cd
    builtin cd $argv
    and test -f .cd-reminder
    and cat .cd-reminder
end

I just realized this will return a non-success exit status when the .cd-reminder file does not exist in a directory. use this instead so the function will only return non-success if you can’t cd to the given dir.

function cd
    builtin cd $argv
    and begin
        test -f .cd-reminder
        and cat .cd-reminder
        or true
    end
end

Leave a Reply

Your email address will not be published. Required fields are marked *