6

This is a follow-up to this question: I'm trying to create a keyboard shortcut for the terminal file manager ranger, in order to jump from the zsh prompt into the file manager with a single keystroke. I was following the linked answer, adding this to my .zshrc:

run_ranger () { echo; ranger; zle redisplay }
zle -N run_ranger
bindkey '^f' run_ranger

The key binding itself works, however ranger fails to start with Error: Must run ranger from terminal. I had a look at the ranger code (Python) and it performs the common check sys.stdin.isatty() to verify that is has a TTY stdin. How can I modify the zle widget so that stdin is properly set?

1
  • 5
    Add < $TTY to the ranger command
    – llua
    Commented Oct 13, 2018 at 22:38

2 Answers 2

8

@llua's comment was indeed the trick to solve the stdin issue, thanks!

My use case required yet another deviation from the linked question. I'm using ranger to change the working directory (using this trick). In this case the zle redisplay has to be replaced by zle reset-prompt to properly change the prompt (see this question). The full solution becomes:

run_ranger () {
    echo
    ranger --choosedir=$HOME/.rangerdir < $TTY
    LASTDIR=`cat $HOME/.rangerdir`
    cd "$LASTDIR"
    zle reset-prompt
}
zle -N run_ranger
bindkey '^f' run_ranger
1
  • 2
    $(< ~/.rangerdir) should avoid the fork and exec and read and writes of cat
    – thrig
    Commented Oct 14, 2018 at 1:37
0

This may not be applicable to everyone. But I'm using tmux and I just resort to the following:

tmux send-keys "ranger" Enter

It uses tmux to send the word ranger and then press Enter. Ugly I know, but it works.

You must log in to answer this question.

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