Home Fish shell Quickly Navigate Through Directory History In Fish Shell

Quickly Navigate Through Directory History In Fish Shell

By sk
Published: Last Updated on 2.3K views

Do you often work with large number of directories? I have a small tip for you. This brief guide explains how to quickly navigate through directory history in Fish shell using cdh, nextd and prevd commands in Linux.

Say hello to cdh, nextd, and prevd commands

A while ago, we discussed about pushd, popd and dirs commands which allows us to quickly navigate through a stack of directories. Unlike the normal cd command, the pushd, popd and dirs commands helps you to easily move back and forth between directories, without having to type the full path. These trio commands comes in handy when you are working with large number of directories and sub-directories. Today, we will learn three other similar commands namely cdh, nextd and prevd.

As the name says, the cdh command allows you to change to the recently visited directories, the nextd command allows you to move forward through directory and the prevd command allows you to move backward through directory history.

This set of three commands are available only in Fish shell and they are often used to navigate through the recently visited directories easily as well as quickly.

The cdh, nextd and prevd commands are quite useful if you are often dealing with deep directory structure. You can quickly go forward or backward without having to the type the actual path of the directories. These triplet makes your CLI navigation better and faster!

Enter into Fish shell if you haven't already:

$ fish

Now let us see some examples for each command. Let us first start with cdh command.

cdh command examples for faster CLI navigation

Navigate through a few directories.

> cd ~/Documents/
> cd ~/Downloads/
> cd ~/Music/
> cd ~/Pictures/
> cd ~/Public/

Note: Here, > indicates the fish shell prompt. I included it to clarify that I am working in a fish shell session.

Finally, let us go back to the $HOME directory.

> cd

To view all recently visited directories, just run cdh without any flags like below:

> cdh

Sample output:

 f  6)  ~
 e  5)  ~/Documents
 d  4)  ~/Downloads
 c  3)  ~/Music
 b  2)  ~/Pictures
 a  1)  ~/Public
Select directory by letter or number: 

As you can see in the above output, the cdh command presents the list of recently visited directories. You can now select one of the entries by letter or number to change into the respective directory. For example, I am going to cd into the ~/Downloads directory, so I entered number 4.

[...]
Select directory by letter or number: 4
sk@ostechnix ~/Downloads> pwd
/home/sk/Downloads
Change to recently visited directory with cdh command in fish shell in Linux
Change to recently visited directory with cdh command in fish shell in Linux

Again, run the cdh command to list all recently visited directories and enter the number or letter shown by the directory to enter into immediately. No need to type full path!

prevd command examples for faster CLI navigation

The prevd command helps you to move backward through recently visited directories.

Check the current working directory:

> pwd
/home/sk

My present working directory is now /home/sk.

Now, go to any directory:

> cd Downloads/Ostechnix/
sk@ostechnix ~/D/Ostechnix> pwd
/home/sk/Downloads/Ostechnix

Now the working directory is /home/sk/Downloads/Ostechnix.

You can move back to the $HOME directory i.e. move backward to the recently visited directory:

> prevd

Check the current working directory:

> pwd
/home/sk
Move backward through directory history with prevd command in fish shell
Move backward through directory history with prevd command in fish shell

If the -l or --list flag is specified, the current directory history is also displayed.

> prevd -l

Sample output:

10) /home/sk
 9) /home/sk/Documents
 8) /home/sk/Downloads
 7) /home/sk/Music
 6) /home/sk/Pictures
 5) /home/sk/Public
 4) /home/sk
 3) /home/sk/Downloads
 2) /home/sk/Pictures
 1) /home/sk
    /home/sk/Downloads
 1) /home/sk
 2) /home/sk/Downloads/Ostechnix

nextd command examples for faster CLI navigation

The nextd command is just opposite to the prevd command. The nextd command is used to move forward through the history of recently visited directories.

Go to any directory:

> cd Downloads/Ostechnix/

My current working directory now is /home/sk/Downloads/Ostechnix.

> pwd
/home/sk/Downloads/Ostechnix

Go back to where you came from with prevd command:

> prevd

You can now move forward to the previously visited directory (i.e. /home/sk/Downloads/Ostechnix) using command:

> nextd

I am now back to the /home/sk/Downloads/Ostechnix directory. Quite easy, isn't it?

Move forward through directory history with nextd command in fish shell
Move forward through directory history with nextd command in fish shell

If the -l or --list flag is specified, the current directory history is also displayed.

> nextd -l

To list all previously visited directories, simply run cdh command and then choose a directory to visit.

Please note that the cd command limits directory history to the 25 most recently visited directories. The history is stored in the $dirprev and $dirnext variables which cdh, nextd and prevd commands manipulates.

More details can be found in the man pages of the respective command:

man cdh
man nextd
man prevd

The cdh, nextd and prevd can be useful when you are working in a stack of directories and nested directories in fish shell.

Related read:

You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More