3

Sometimes when working in deeply nested file hierarchies (e.g. Java code) I find it annoying to traverse from one leaf to another. My usual cd-up looks like:

$ cd ../../..
# Oops, not enough
$ cd ..
# one more
$ cd ..

If I want to edit a file I can vim dir and interactively open desired file.
Is there some utility that allows to interactively change directory? Explorers/commanders (mc, vifm) are too heavy for such a task.

I have searched the internet, but my keywords seem to return nothing of relevance.

Added to clarify:

I am searching for an utility (let's call it vcd) that presents me an interactive directory tree which I can traverse and select where to cd.
From user perspective vim . calls hypothetical vcd which in the end executes vim $filename. I would like similar functionality (directory browser), just in the end to end up with CWD changed to interactively selected directory. I hope this makes my intentions more clear.

friendzis
  • 185
  • 1
  • 5

6 Answers6

3

Try fzf-fs.

Install fzf:

git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install

Re-source your ~/.bashrc: . ~/.bashrc

Clone fzf-fs: git clone https://github.com/D630/fzf-fs ~/fzf-fs

Add ~/fzf-fs to your PATH: PATH=$PATH:~/fzf-fs
Run fzf-fs --init
Define an alias: alias vcd='. fzf-fs'
Run vcd ~
You should see something like this: enter image description here

Choose directory with Enter, use fuzzy searching, type [q] to exit.

See also: fzf-fs USAGE, fzf-fs doesn't support newlines in filenames fixed for cd!

Evgeny
  • 5,476
2

While I do not know a tool that fits your description, perhaps some of the small utilities mentioned below would be helpful?

pushd and popd, described here along with other tools that can boost productivity while navigating around

fasd - offers quick access to files and directories for POSIX shells

autojump - a cd that learns

z - a variation on the above

v - z for vim

2

Something like PC Magazine's DM from back in the DOS days... I don't know of a similar program on Linux, but I quite like xd for navigating around directories; with a function defined as

xd() {
    cd "$(/usr/bin/xd "$@")"
}

you can type for example

xd /ulb

which will print a list of all directories matching /u*/l*/b* (/usr/lib/binfmt.d, /usr/local/bin...), ask you to pick one and change to it.

Stephen Kitt
  • 434,908
  • hm, xd V3.22.09 doesn't understand my locale settings and doesn't support UTF-8. So, xd ф prints no solutions for ф but xd $'\xd1' prints 1: /home/.../ф – Evgeny Jul 05 '15 at 11:04
  • 1
    Would you mind filing a bug (reportbug xd)? The Debian maintainer is xd's author so you'll be talking directly to the right person :-). – Stephen Kitt Jul 06 '15 at 17:05
  • thanks for the reminder. I should check the latest version of xd before sending a bug report:) – Evgeny Jul 06 '15 at 17:26
2

Try this third party tool: CDI - Change directory interactive

Repo: https://github.com/antonioolf/cdi

With this open source tool, just type the command cdi in the terminal to open the folder structure and it will allow you to browse the directories using the arrow keys (Right/Left to Enter/leave a folder).

When you press enter the conventional CD command will be executed internally passing the folder chosen as a parameter.

Installation

Copy paste and run in your terminal

curl -fsSL https://raw.githubusercontent.com/antonioolf/cdi/master/install.sh | bash

More info: https://github.com/antonioolf/cdi

Author: Antônio Oliveira (Me)

0

You can use the nnn file manager to interactively browse and select a directory.

And you can configure it to change into the selected directory on exit (when pressing Ctrl+G or always).

Pick the appropriate file for your shell from misc/quitcd and add the contents to your shell's rc file. You'll need to spawn a new shell for the change to take effect. You should start nnn as n (or your preferred function name/alias).

By default, when ^G is pressed, nnn writes the last working directory to

${XDG_CONFIG_HOME:-$HOME/.config}/nnn/.lastd

To cd on quit always, export the environment variable NNN_TMPFILE set to your preferred file path.

The extra key ^G to cd on quit is to avoid writing to a file every time nnn quits and allows on-demand cd on quit

1

Dario Seidl
  • 2,446
  • 1
  • 21
  • 21
0

ZSH shell provides an interactive cd. ZSH is an extension of the Linux shell.

ZSH shell will display all candidates if you hit the tab during cd and allow you to select the right candidate with tab.


Notice that the 'tab' autocompletion in ZHS behaves differently than the default 'tab' autocompletion in native Linux shells.

While both will do the autocompletion neatly when only one candidate exists.

When multiple candidates exist, the behaviour of ZSH and native shells is different.

The native shells will list all the candidates directly in the output and exit the cd command. So you have to reenter the command. (but actually, you have to hit twice the tab key to see this reaction. You will get no output when you hit only once the tab key)

The ZSH, however, will list all candidates as hints (without exit cd command) and allow you to select the preferred one with tab or arrow keys . After the candidate is selected, you are allowed to continue to type the reset of your command on the same line.

Below is a screenshot, you can see the folder 'Downloads' is selected and therefore automatically completed on the command line. And you are still allowed to finish your command. enter image description here

For installation, you can check the ZSH installation instructions

  • Welcome to the site. It would seem that you are describing tab completion which most shells implement nowadays, not just zsh. If that was not intended, please consider editing your post to make the distinctive feature of zsh you are describing more prominent. – AdminBee Nov 22 '22 at 11:55