0

Using this post and the documentation, I was trying to create a fish function for a PR on gokechan/lf package to get lfcd working in fish.

This is the bash function that I am trying to convert:

lfcd () {
tmp="$(mktemp)"
lf -last-dir-path="$tmp" "$@"
if [ -f "$tmp" ]; then
    dir="$(cat "$tmp")"
    rm -f "$tmp"
    if [ -d "$dir" ]; then
        if [ "$dir" != "$(pwd)" ]; then
            cd "$dir"
        fi
    fi
fi
}

And this is the fish function I've adapted from the function above:

function lfcd
set tmp "(mktemp)"
lf -last-dir-path="$tmp" "$argv"
if [ -f "$tmp" ];
    set dir "(cat "$tmp")"
    rm -f "$tmp"
    if [ -d "$dir" ];
        if [ "$dir" != "(pwd)" ];
            cd "$dir"
        end
    end
end
end

So far the function runs lf but it does not open the last selected directory on exit.

I have 0 experience creating fish functions or translating bash to fish. This is my introductory fish project, so any help or guidance will be much appreciated!

1 Answers1

1

Most of the time, it's not strictly necessary to quote variables in fish: word splitting is not a feature of fish.

You could write:

function lfcd
    set tmp (mktemp)
    lf -last-dir-path=$tmp $argv
    if [ -f $tmp ]
        set dir (cat $tmp)
        rm -f $tmp
        [ -d $dir ]; and [ $dir != (pwd) ]; and cd $dir
    end
end
glenn jackman
  • 238,783
  • 38
  • 220
  • 352