Skip to content

Latest commit

Β 

History

History
149 lines (105 loc) Β· 3.53 KB

navigation.md

File metadata and controls

149 lines (105 loc) Β· 3.53 KB

Navigation

Navigating your filesystem is likely something you do on a regular basis. zsh knows this and has added some helpers to help you move quickly from one folder to another.

cd basics

Just to make sure we're all on the same page, here are some quick cd tips that should work in any shell.

# change directory to your $HOME with just the cd command
cd

# change directory to the previous directory with
cd -

Now let's set the cdpath variable. This variable will do automatic searching for a folder within folders specified in variable.

If you're familiar with bash's CDPATH it works the same way but in zsh instead of being a colon separated string (e.g. .:~:~/src) it's an array.

# make some temporary folders
$ mkdir -p ~/test ~/src/foo/bar

$ export cdpath=(. ~ ~/src/)

# cd to $HOME
$ cd

# cd to ~/src/foo
$ cd foo

# cd to ./bar
$ cd bar

# cd to ~/test
$ cd test

# cd back to ~/src/foo/bar
$ cd -

That's pretty powerful! Add more search directories you frequent and jump right to them.

zsh also allows more powerful directory movement. AUTO_CD will automatically change into a directory even without cd if that directory is not the name of a command.

$ ls
foo/ bar/ some.txt

$ setopt auto_cd

# cd into foo
$ foo

$ pwd
~/foo

# change directory to other parts of the filesystem with full paths
$ ls /var/log/
apache docker

$ /var/log/apache

That's cool but doesn't save a lot of time. You can also cd into unique paths with using partial words

# assume this directory exists /var/log/apache/errors/
# cd into it with
$ /v/l/a/e

$ pwd
/var/log/apache/errors/

# if /var/log/aero/errors exists you can use the minimal unique names
$ /v/l/ae/e

$ pwd
/var/log/aero/errors

You can also swap any part of your current path. If you have two directories with similar structures it makes moving between them much faster.

$ mkdir -p foo/bar/baz/{today,tomorrow,jan-01,jan-02}/{folder1,folder2}

$ tree -F foo
foo
└── bar/
    └── baz/
        β”œβ”€β”€ today/
        β”‚Β Β  β”œβ”€β”€ folder1/
        β”‚Β Β  └── folder2/
        β”œβ”€β”€ tomorrow/
        β”‚Β Β  β”œβ”€β”€ folder1/
        β”‚Β Β  └── folder2/
        β”œβ”€β”€ jan-01/
        β”‚Β Β  β”œβ”€β”€ folder1/
        β”‚Β Β  └── folder2/
        └── jan-02/
            β”œβ”€β”€ folder1/
            └── folder2/

$ f/b/b/tom/folder2

$ pwd
foo/bar/baz/tomorrow/folder2

# cd into foo/bar/baz/today/folder2
$ cd tomorrow today

$ pwd
foo/bar/baz/today/folder2

# this also works for partial names
$ cd today jan-01

$ pwd
foo/bar/baz/jan-01/folder2

$ cd 01 02

$ pwd
foo/bar/baz/jan-02/folder2

This is about as good as it's going to get without using an external tool.

External Tools

Now that we've covered some of what zsh can do built in it's up to you to take it one step further with external tools. These tools will keep track of files and directories you frequently use and allow you to interactively search for them or automatically jump to them.

Some additional homework for the reader is to install and setup one of the following

  • fasd: All in one jump and edit tool with fuzzy matching
  • autojump: Jump to directories with frequency tracking
  • z: Jump to most used directories based on 'frecency'
  • v: Like z but for editing files with vim

Previous: line movement | home | Next: file management