Pergunta

I have been trying to port the ranger-cd function for the ranger file manager to the fish shell. As of 2013, ranger’s ranger-cd function looks like this:

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"'

(This function gives a temporary file to ranger file manager to store the last accessed directory so that we can change to that directory after ranger quits.)

Here’s what I have done so far to port the function to fish:

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

When I use this function I get:

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

I’m guessing there are still multiple errors in the above code. Does anyone have a working solution for this?

Foi útil?

Solução

My answer is based off of gzfrancisco's. However, I fix the "'-a' at index 2" issue, and I also ensure that a new prompt is printed after exiting ranger.

I put the following in ~/.config/fish/config.fish:

function ranger-cd                                                               

  set tempfile '/tmp/chosendir'                                                  
  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 ; commandline -f repaint'                                           
end

Outras dicas

simpler solution, but it does the same. type in shell:

alias ranger 'ranger --choosedir=$HOME/.rangerdir; set RANGERDIR (cat $HOME/.rangerdir); cd $RANGERDIR'
funcsave ranger

alias is a shortcut do define a function. funcsave saves it into ~/.config/fish/functions/ranger.fish

The problem is this line.

test -f $tempfile and

You shuld remove the and because "and" is a conditional execution http://ridiculousfish.com/shell/user_doc/html/commands.html#and

function ranger-cd

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

  if test -f $tempfile and cat $tempfile != echo -n (pwd)
    cd (cat $tempfile)
  end

  rm -f $tempfile

end

EDIT: This does not work for fish unfortunately, but only for bash-compatible shells.

I recently found out that there is another way to accomplish the same end goal, by sourcing the ranger script instead:

source ranger

When ranger is then exited, it will have changed the working directory into the last visited one.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top