Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cd -1, -2, -3 etc in Z shell

Tags:

How do you set up the Z shell such that typing cd - gives you a list of previously visited paths, and cd -1, -2, -3, etc. will then take you to the directories?

like image 699
invisiblerhino Avatar asked Oct 21 '10 11:10

invisiblerhino


People also ask

What does Z shell do?

What is Zsh? Zsh, also known as the Z shell, extends functionality of the Bourne Shell (sh), offering newer features and more support for plugins and themes.

What does Z stand for in Zsh?

December 2021) The Z shell (Zsh) is a Unix shell that can be used as an interactive login shell and as a command interpreter for shell scripting. Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh. Z shell. Screenshot of a Zsh session.

Is Z shell a programming language?

“Z shell” or zsh for short, was created in 1990 by Paul Falstad. It is also a Unix shell and command language based on Bourne shell with a large number of improvements, including some features of bash. Zsh also had the ability to be used as a scripting language with the ability to use shell scripts.

Is Z shell better than Bash?

Z shell or Zsh is one of them which is invented after Bash. It has many features like Bash but some features of Zsh make it better and improved than Bash, such as spelling correction, cd automation, better theme, and plugin support, etc.


2 Answers

If you have setopt AUTO_PUSHD in your .zshrc then cd will automatically do a pushd of each directory you change to. Taking the example from ZyX:

$ setopt AUTO_PUSHD $ mkdir -p 1/2/3/4 $ cd 1 $ cd 2 $ cd 3 $ cd 4 

You can see a list of the directories using dirs:

$ dirs -v 0    ~/1/2/3/4 1    ~/1/2/3 2    ~/1/2 3    ~/1 4    ~ 

To be able to tab complete the list you can use the + and - arguments with cd (<TAB> meaning you hit the tab key):

$ cd +<TAB> 1 -- ~/1/2/3 2 -- ~/1/2 3 -- ~/1 4 -- ~ 

Or the reverse:

$ cd -<TAB> 0 -- ~ 1 -- ~/1 2 -- ~/1/2 3 -- ~/1/2/3 

Then just select the number to go to that directory:

$ cd +2 $ pwd ~/1/2 

Tab Complete Directories

I always forget the magic sequence to do the following so I updated the answer to explain this part.

The + and - will only take you to the directory, you can't tab complete the path in the stack (i.e. cd -2/<TAB> gives you nothing). To make this work, you can use a tilde (~).

Make some directories in 3 to make this example better.

$ mkdir 3/foo 3/bar 3/baz 

Then find the directory in the stack.

$ cd ~+<TAB> 1 -- ~/1/2/3/4 2 -- ~/1/2/3 3 -- ~/1 4 -- ~ 

Then use tab completion on an entry.

$ cd ~+2/<TAB> 4/    bar/  baz/  foo/ 
like image 168
claytron Avatar answered Oct 22 '22 03:10

claytron


If you use pushd instead of cd, then you can list directories with dirs and cd to old directory with popd. You can also set autopush option to get cd behave much like pushd -q. Here is an example:

setopt pushdsilent # Omit printing directory stack setopt autopush    # Make cd push directories onto stack setopt pushdminus  # Invert meanings of +N and -N arguments to pushd mkdir -p 1/2/3/4 cd 1 cd 2 cd 3 cd 4 popd     # Go to previous directory (3) and remove it from directory stack pushd -  # Go to previous directory (4) pushd -2 # Go 2 directories back the directory stack (2) 

Note that pushd does not remove anything from the directory stack, it only rotates it. See man zshbuiltins for more details.

like image 34
ZyX Avatar answered Oct 22 '22 03:10

ZyX