Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by

This is more of an answer rather than a question.

I use zoxide to quickly jump to most recent directories which matches a substring. For example, if often cd into MyApp directory whose full path is ~/Work/Projects/Apps/MyApp, then (assuming I have visited that directory using cd a couple of times) I can just use zd mya or maybe shorter substring to jump to the directory. Below I have shown how I integrate it to vifm and within cd version of fzf command in vifm

" Choose directory with zoxide query, where user enter a search term after `Zcd` like `:Zcd substring` and cd into the first match
command! Zcd : set noquickview
              \| let $Z_PICK = term('zoxide query "%a"')
              \| if $Z_PICK != ''
              \|     let $dir_name = fnameescape($Z_PICK)
              \|     cd $dir_name
              \|     execute '!zoxide add %%d %%i'
              \| endif

" Choose directory with zoxide interactive and cd into it
command! ZcdI : set noquickview
               \| let $Z_PICK = term('zoxide query -l | fzf --height 20 2>/dev/tty')
               \| if $Z_PICK != ''
               \|     let $dir_name = fnameescape($Z_PICK)
               \|     cd $dir_name
               \|     execute '!zoxide add %%d %%i'
               \| endif

You can even add execute '!zoxide add %%d %%i' to the fzf cd command as below

" Choose directory and cd into it
command! Fzfcd : set noquickview
                \| let $FZF_PICK = term('fd --type d --hidden --follow | fzf-tmux 2>/dev/tty')
                \| if $FZF_PICK != ''
                \|     execute 'cd' fnameescape($FZF_PICK)
                \|     execute '!zoxide add %%d %%i'
                \| endif

1 Answer

0 votes
by
selected ago by
 
Best answer

This is more of an answer rather than a question.

Then I'll do a bit of a review and ask a question :)

let $dir_name = fnameescape($Z_PICK)
cd $dir_name

This breaks entering into directories that require escaping. Either do cd $Z_PICK or execute 'cd' fnameescape($Z_PICK) (this variant can use a global variable like g:z_pick instead to not affect environment). Unlike in a shell, environment variables are expanded after parameter splitting.

I use zoxide to quickly jump to most recent directories which matches a substring.

What if it guessed the directory incorrectly, just do another try? Doesn't this mess up history so that cd - and $OLDPWD point to the path opened by mistake?

by

This breaks entering into directories that require escaping. Either do cd $Z_PICK or execute 'cd' fnameescape($Z_PICK) (this variant can use a global variable like g:z_pick instead to not affect environment)

If I use execute 'cd' fnameescape($Z_PICK), is execute '!zoxide add %%d %%i' still needed after that? Or will it have the effect of using it in the interactive shell.

What if it guessed the directory incorrectly, just do another try? Doesn't this mess up history so that cd - and $OLDPWD point to the path opened by mistake?

Yes. But I have zoxide aliased to zd. I use cd + shell completions if I need to use cd. So when I don't want to fzf alt+c or fzf history search, I just use something like zd "substring". With shell autosuggestions, it becomes much easier.

There's also zoxide interactive with fzf which I have aliased to zdi where I can choose the most recent dirs from fzf unlike alt+c in fzf.

by

If I use execute 'cd' fnameescape($Z_PICK), is execute '!zoxide add %%d %%i' still needed after that? Or will it have the effect of using it in the interactive shell.

Internal :cd has no connection to a shell (unlike in Midnight Commander which runs it in background), so yes. However, you could instead invoke zoxide on every directory change implicitly like this:

autocmd DirEnter * !zoxide add %d %i
If you would like to make a bug report or feature request consider using GitHub, SourceForge or e-mail. Posting such things here is acceptable, but this is not a perfect place for them.
...