如何将`ranger-cd`函数移植到fish shell

non*_*one 3 fish

我一直在尝试将ranger-cd用于游侠文件管理器的函数移植到fish shell,如下所示:

function ranger-cd {
    tempfile='/tmp/chosendir'
    /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
    test -f "$tempfile" &&
    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
        cd -- "$(cat "$tempfile")"
    fi
    rm -f -- "$tempfile"
}

# This binds Ctrl-O to ranger-cd:
bind '"\C-o":"ranger-cd\C-m"'
Run Code Online (Sandbox Code Playgroud)

(此函数为游侠文件管理器提供临时文件,以存储最后访问的目录,以便我们可以在游侠退出后更改到该目录.)

这是我到目前为止所做的:

function ranger-cd
    set tempfile '/tmp/chosendir'
    /usr/bin/ranger --choosedir=$tempfile (pwd)
    test -f $tempfile and
    if cat $tempfile != echo -n (pwd)
        cd (cat $tempfile)
    end
    rm -f $tempfile
end

function fish_user_key_bindings
        bind \co ranger-cd
end
Run Code Online (Sandbox Code Playgroud)

当我使用这个功能时,我得到:

test: unexpected argument at index 2: 'and'
     1  /home/gokcecat: !=: No such file or directory
cat: echo: No such file or directory
cat: /home/gokce: Is a directory
Run Code Online (Sandbox Code Playgroud)

我猜测上面的代码中仍然存在多个错误.有没有人有这方面的工作解决方案?

chr*_*bia 7

我的回答是基于gzfrancisco的.但是,我修复了"索引2处的'-a"问题,并且我还确保在退出游侠后打印一个新提示.

我把以下内容放入~/.config/fish/config.fish:

function ranger-cd                                                               

  set tempfile '/tmp/chosendir'                                                  
  /usr/bin/ranger --choosedir=$tempfile (pwd)                                    

  if test -f $tempfile                                                           
      if [ (cat $tempfile) != (pwd) ]                                            
        cd (cat $tempfile)                                                       
      end                                                                        
  end                                                                            

  rm -f $tempfile                                                                

end                                                                              

function fish_user_key_bindings                                                  
    bind \co 'ranger-cd ; fish_prompt'                                           
end
Run Code Online (Sandbox Code Playgroud)