5

I was wondering if there was an easier way to pick directories to cd to within one's current directory.

For example, is there a way to just get a numbered list of the current directory and be able to do cd 1, cd 2, cd 3, etc. Is there some other way to easily cd between sub directories in one's current directory?

1
  • 1
    You might want to look at Midnight Commander, which is a console-based "GUI" that displays directory listings and lets you change among them.
    – pjc50
    Commented Sep 28, 2015 at 10:00

4 Answers 4

7

If you set the autocd option in your ~/.bashrc you can change directories just by typing the name. Combined with Tab completion, you have only to type the first, unique characters and then hit Tab to move to your desired directory.

From the manual:

If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells.

Add the option to your ~/.bashrc with:

shopt -s autocd 

If you use Zsh, set the option with:

setopt autocd 
1
  • I was still using $CDPATH of old ...
    – Archemar
    Commented Sep 28, 2015 at 19:14
3

Add a function to your ~/.bashrc

mycd() { local x; shopt -s {null,dot}glob; select x in ../ */; do cd "$x"; break; done; }

usage: mycd

2
  • You might also want to use the dotglob option to allow hidden names.
    – chepner
    Commented Sep 28, 2015 at 17:31
  • @chepner: Good idea and thank you for quoting. I've updated my answer.
    – Cyrus
    Commented Sep 28, 2015 at 17:53
2

Use completion! By default, this requires you to type a unique prefix of the directory you want to switch to, then press Tab. More precisely, pressing Tab completes the first unique prefix.

You can tweak completion through some readline options. In particular, set show-all-if-ambiguous to immediately display a list if there are multiple possible completions, or show-all-if-unmodified to display a list only if the current prefix cannot be completed further due to ambiguity. These options go into the file ~/.inputrc, for example:

set show-all-if-ambiguous on

You can configure bash to make repeated presses of Tab rotate through the possible completions. To do that, bind the key to menu-complete instead of the default complete. In your ~/.inputrc:

"\t": menu-complete

Depending on your installation, completion after cd may either offer only directories, or all files. If it offers all files, you need to enable context-sensitive completion. Make sure that the bash-completion package is installed (it's available in most distributions) and that you have the line

. /etc/bash_completion

in your ~/.bashrc (or in /etc/bash.bashrc if your distribution has that).

Zsh is an alternative shell that has many nice features for interactive use that bash lacks, including the possibility to choose completions in a menu. You may want to give it a try.

1

You can also use a single-character placeholder for each directory that you migrate to by assigning the current directory file path to an environment variable such as ...

    a=`pwd`
    b=`pwd`

etc.. Then you can jump to each directory with:

    cd $a
    cd $b

Be sure to use 'back-ticks' which mean to execute the command between the ticks and the equal sign assigns the output to the variable.

You can also assign the paths to single-character variables by writing them out also, such as:

    c="/home/user/projects/c++/headers/docs"

then you can change to that directory using...

    cd $c

You must log in to answer this question.

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