Z shell中的cd -1,-2,-3等

inv*_*ino 22 shell zsh autocomplete zsh-completion

如何设置Z shell,以便键入cd - 为您提供以前访问过的路径列表,然后cd -1,-2,-3等会将您带到目录?

cla*_*ron 58

如果您有setopt AUTO_PUSHD,.zshrc那么您cd将自动执行pushd您更改为的每个目录.以ZyX为例:

$ setopt AUTO_PUSHD
$ mkdir -p 1/2/3/4
$ cd 1
$ cd 2
$ cd 3
$ cd 4
Run Code Online (Sandbox Code Playgroud)

您可以使用以下命令查看目录列表dirs:

$ dirs -v
0    ~/1/2/3/4
1    ~/1/2/3
2    ~/1/2
3    ~/1
4    ~
Run Code Online (Sandbox Code Playgroud)

为了能够选项卡完成列表,你可以使用+-参数cd(<TAB>意思是你点击tab键):

$ cd +<TAB>
1 -- ~/1/2/3
2 -- ~/1/2
3 -- ~/1
4 -- ~
Run Code Online (Sandbox Code Playgroud)

或者相反:

$ cd -<TAB>
0 -- ~
1 -- ~/1
2 -- ~/1/2
3 -- ~/1/2/3
Run Code Online (Sandbox Code Playgroud)

然后只需选择要转到该目录的号码:

$ cd +2
$ pwd
~/1/2
Run Code Online (Sandbox Code Playgroud)

制表符完整目录

我总是忘记执行以下操作的神奇序列,所以我更新了答案以解释这一部分.

+-只需要你的目录,你不能完成标签堆栈中的路径(即cd -2/<TAB>给你什么).要使其工作,您可以使用波浪号(\n ~).

制作一些目录3以使这个例子更好.

$ mkdir 3/foo 3/bar 3/baz
Run Code Online (Sandbox Code Playgroud)

然后找到堆栈中的目录.

$ cd ~+<TAB>
1 -- ~/1/2/3/4
2 -- ~/1/2/3
3 -- ~/1
4 -- ~
Run Code Online (Sandbox Code Playgroud)

然后在条目上使用制表符完成.

$ cd ~+2/<TAB>
4/    bar/  baz/  foo/
Run Code Online (Sandbox Code Playgroud)

  • 请注意,您似乎可以在任何命令中使用它!例如`vi~ + 4/it.txt'会编辑文件`〜/ 1/it.txt' (4认同)

ZyX*_*ZyX 6

如果你使用pushd而不是cd,那么你可以列出目录dirs和cd到旧目录popd.您还可以设置autopush选项以获得更好的cd行为pushd -q.这是一个例子:

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)
Run Code Online (Sandbox Code Playgroud)

请注意,pushd不会从目录堆栈中删除任何内容,它只会旋转它.有关man zshbuiltins详细信息,请参阅