0

I don't understand why these two different ways to use the same command lead to different behaviors.

I've installed fzf and fd and have the following lines in .bashrc

function updir(){
  cd ..
}
function overdir(){
  cd `fd -t d -d 1 | fzf`
}
function downdir(){
  cd `fd -t d -d 8 | fzf`
}

bind -x '"\C-i":"updir"'
bind -x '"\C-j":"overdir"'
bind -x '"\C-l":"downdir"'

export PS1="[\u@\h:\W]$ "

If I type overdir in the shell I can type a pattern and go to a directory as expected, and the current working directory updates in the prompt, like so

[davidlittle@deus1:~]$ overdir
[davidlittle@deus1:googledrive]$ overdir
[davidlittle@deus1:Preferences]$

Note that inbetween these prompts an fzf window is revealed, I select a directory, and then the view returns to what is shown above. However, if I do the same thing using the bound shortcut "C-j" instead of , now I get something different:

[davidlittle@deus1:Preferences]$
[davidlittle@deus1:Preferences]$ ls
%Users%davidlittle%googledrive%Preferences%vscode%settings.json keybindings.json                                                settings.json
[davidlittle@deus1:vscode]$

As you can see the directory listed in the prompt is Preferences on line 2. But this was after hitting C-j and selecting the vscode directory. Once I call ls the correct directory is shown in the prompt.

The expected behavior is that line 2 of the second example should display vscode not Preferences.

What's going wrong???

  • what happens if you hit enter key by itself, instead of the ls command? – jsotola Nov 1 '18 at 22:27
  • what happens if you add ls as the second command into overdir()? – jsotola Nov 1 '18 at 22:29
  • what is the result of C-i? ..... how about the result of multiple presses of C-i? – jsotola Nov 1 '18 at 22:30
0

Thanks to @jsotola's questions, and some experimentation, I managed to solve this problem myself.

First, the -x isn't necessary, and seemed to be causing problems (when I hit enter, as @jsotola asked, that changes $PS1, so I could probably also include the key combination for enter inside the binding, but I'm not sure how to do that and I think I prefer seeing what the last command was); second, the control commands were working poorly (since they map to other key combinations depending on the terminal) so I switched to using the meta key. Here are the final, working bindings:

function updir(){
  cd ..
  exa --long --group-directories-first
}
function indir(){
  cd "`fd -t d -d 1 | fzf`"
  exa --long --group-directories-first
}
function deepdir(){
  cd "`fd -t d -d 8 | fzf`"
  exa --long --group-directories-first
}


bind '"\ek":"updir\n"'
bind '"\ej":"indir\n"'
bind '"\el":"deepdir\n"'

Note that these new bindings also include a command to display the directory using exa. Turns bash into a pretty clean file explorer with fuzzy matching!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.