Question

I've looked at all the similar questions on Stack Overflow, and tried a bunch of stuff in their documentation. My present attempt is below.

What I'm trying to accomplish is differential behavior based on the computer user. I have a work computer and a home computer, and I want the work computer to go to a different directory when I enter "code" into the command line than the one I want when I enter it at home. I tried this (my work username is afk, home is sfk):

function code
    set result (dirh)
    if [contains "sfk" $result]
        cd ~/rails
    else if [contains "afk" $result]
        cd ~/code
    else
        echo $result
    end
end

Currently, this is griping that "Unknown command contains Users/sfk/.config/fish/" (My PWD is Users/sfk/.config/fish/ when I'm entering this). I think maybe contains is just for lists, but the docs aren't particularly clear about this? Any idea how I might do this?

EDIT

Just tried this, and while it no longer errors, it ends up in the else case, which it shouldn't, cause my user IS sfk, which IS included in the dirh string:

function code
    set result (dirh)
    if test (contains "sfk" $result)
        cd ~/rails
    else if test (contains "afk" $result)
        cd ~/code
    else
        echo $result
    end
end
Was it helpful?

Solution

As said above in the comments, that is the best solution for this specific use case. But to answer the question in case someone wants to do something else.

You are right about contains, that it does an exact match on list items. You could use the switch function instead. Which supports wild-card matching.

function code
    set -l result (dirh)

    switch $result
        case '*sfk*'
            cd ~/rails
        case '*afk*'
            cd ~/code
        case '*'
            echo $result
    end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top