1

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 "$@" && { [ ! -f .cd-reminder ] || cat .cd-reminder 1>&2; }
}

alias cd=reminder_cd`

2 Answers 2

3

You could add a function like this to your ~/.config/fish/config.fish:

function show-reminder --on-variable PWD
   if test -f .cd-reminder
        cat .cd-reminder
   end
end

(This avoids overriding the built-in cd function, which likes to keep directory history that you can use.)

Note that you should not add this as an autoloaded function in ~/.config/fish/functions/, because it will not be picked up as triggering on the change of $PWD until it is run manually.

2
  • How do you do the same when you open a new tab with the directory set to that directory?
    – yiwen
    Apr 16, 2015 at 18:44
  • In that case, call the function straight away in your config.fish.
    – Zanchey
    Apr 17, 2015 at 5:30
1
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
1
  • 1
    Note that this overrides the built-in cd function, which means that directory history (prevd and nextd) won't work.
    – Zanchey
    Oct 26, 2014 at 11:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .