I'm looking for a command that invokes readline or similar, primed with the current $PWD, to let the user edit the current directory, then cd to the edited value. E.g.

> cd ~/a/b/c/d
> pwd
> /home/alice/a/b/c/d

Then run the proposed icd command (for "interactive cd", inspired by imv in renameutils). It prompts the user as follows:

> icd
icd> /home/alice/a/b/c/d

Then the user can, e.g. press Alt-b, Alt-b, Alt-t, resulting in:

icd> /home/alice/a/c/b/d

(Alt-t transposing b and c)

Upon pressing Enter, the icd command changes the current directory to /home/alice/a/c/b/d.

Ideally icd would have some autocompletion. Maybe even visual indication of whether the current value is an existing/valid directory.

This can nearly be done in zsh by typing

> cd `pwd`

then pressing Tab. But a command like icd would save keystrokes.

Related: Interactive cd (directory browser)

  • 2
    Your title says "sh"; did you mean for something POSIX-compliant, or zsh-focused? – Jeff Schaller Jul 11 '19 at 17:45
  • zsh-focused would be fine, since I use it, but I put "sh" because I figured this would be easy enough to do-able in basic sh, making the answer more generally helpful, while also still probably compatible with zsh. I named a shell at all because, e.g. a csh-specific answer would not help me. – Robert Fleming Jul 12 '19 at 16:28
  • An sh like like dash has so few line-editing features that I'd imagine something like this would be hundreds of times more difficult there. (Not that anyone uses dash interactively, I hope) – muru Jul 13 '19 at 2:52

For bash and any other shell supporting readline you might be able to use this function

icd() { local a; read -ei "${1:-$PWD}" -p "$FUNCNAME> " a && cd "$a"; }


icd          # Starts editing with $PWD
icd /root    # Starts editing with /root
  • 1
    This is perfect, thanks! Unfortunately zsh's read doesn't support -i. But modifying your solution to use vared works for me: icd() { local a="${1:-$PWD}"; vared -p "$FUNCNAME> " a && cd "$a"; } – Robert Fleming May 18 '20 at 22:32

It can be done at least in zsh. A similar thing is built into fzf, where you can press Alt+C to cd into a directory through fuzzy selection:

Instead of fzf, one could presumably use your proposed icd tool.

I do not believe it can be done in a POSIX-compliant way.

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.