this post was submitted on 03 Nov 2023
39 points (100.0% liked)

Programming

13307 readers
1 users here now

All things programming and coding related. Subcommunity of Technology.


This community's icon was made by Aaron Schneider, under the CC-BY-NC-SA 4.0 license.

founded 1 year ago
MODERATORS
 

I accidentally discovered that both "cd ..." and "..." work, and moreover, I can add more dots to go back further! I'm using zsh on iTerm2 on macOS. I'm pretty sure this isn't a cd feature. Is this specific to zsh or iTerm2? Are there other cool features I just never knew existed??

I'm so excited about an extra dot right now.

all 28 comments
sorted by: hot top controversial new old
[–] [email protected] 23 points 9 months ago

Yes, that's a zsh feature.

[–] [email protected] 21 points 9 months ago (1 children)

Actually cd isnt a program. Your present working directory is managed entirely by the shell. If you type "type cat" in the terminal it will tell you its a program, but "type cd" says its a shell builtin. So yes, cd depends on the shell and zsh has some awesome quality of life features. This is not something you can do in bash.

[–] [email protected] 4 points 9 months ago

I totally forgot! Makes sense that the implementation is shell-specific.

[–] [email protected] 15 points 9 months ago (3 children)

And - to go back to where you came from.

[–] [email protected] 7 points 9 months ago

This is my favorite cd feature by a large margin.

[–] [email protected] 4 points 9 months ago (1 children)

Wait what??? That’s amazing! I’m learning so much.

[–] [email protected] 2 points 9 months ago* (last edited 9 months ago)

One more, install and enable fzf in your .zshrc.

It makes ctrl+R (reverse history search) a lot better and cooler

[–] [email protected] 1 points 9 months ago (1 children)
[–] [email protected] 4 points 9 months ago

in zsh just the - is enough for me though it might be a feature of oh-my-zsh config

[–] [email protected] 10 points 9 months ago (1 children)

BTW in case you didn't know, a bare cd without arguments will bring you to your home. I have alias setup to quickly navigate one or two directories up and print the path then:

alias ..='cd .. && pwd'
alias ...='cd ../.. && pwd'

Should works with every shell and I don't even need to type cd itself.

[–] [email protected] 2 points 9 months ago (1 children)

Oooh I’m stealing these aliases! Odd that I never wondered whether you could have an alias be symbols.

[–] [email protected] 7 points 9 months ago* (last edited 9 months ago) (4 children)

Each instance of . is a relative level to your current directory. ‘cd .’ changes your directory to your current directory. ‘cd ..’ (edit: on mobile this keeps changing to three periods but it should just be two) changes it to the directory above, ‘cd ….’ would change it to three directories above. This is standard in *nix (Unix and Linux) operating systems

Edit 2: this is very wrong

[–] [email protected] 41 points 9 months ago (1 children)

This is standard in *nix (Unix and Linux) operating systems

No, it very much isn't.

[–] [email protected] 7 points 9 months ago

Yeah you’re right. I wrote this before sleep and after sleep it’s hilariously wrong lol. Oh well I’ll leave it as a cautionary tale

[–] [email protected] 25 points 9 months ago (1 children)

That's definitely not standard. Maybe your distro or shell has this configured that way. The actual standard thing is that each directory has entries for . and .., as you can see in ls -a.

[–] [email protected] 6 points 9 months ago (2 children)

Yup, that's what I've always understood. Seems like this is zsh-specific, since using the default Terminal app with zsh also works. Do you know if other shells (fish, csh, etc.) support this syntactic sugar? Anything else zsh has that I should know?

[–] [email protected] 8 points 9 months ago (1 children)

Well, I'm a fish guy, so there's two things I can tell you:

  1. fish does not support this particular syntactic sugar.
  2. You can get fish-like autosuggestions in zsh via this: https://github.com/zsh-users/zsh-autosuggestions
[–] [email protected] 2 points 9 months ago

Though I use fish as well, this is some good info 👍.

[–] [email protected] 2 points 9 months ago

Eshell, the Emacs shell, supports this feature out of the box, regardless of the OS it runs on.

[–] [email protected] 18 points 9 months ago (1 children)

Not really. . and .. are the only standard directory entries that are added by the system.

Some shells may extrapolate from that by adding ... to go two directories up, but ... can just as well be the name of an actual file or directory.

[–] [email protected] 1 points 9 months ago (1 children)

I’ve always thought it was funny how *nix lets you name things in a way that makes it miserable for others lol. I think I had a directory named - because of a mkdir syntax error.

[–] [email protected] 2 points 9 months ago

I guess this is an interesting contrast to Windows, where not only certain characters (like ? or * or |) are banned, but also entire filenames that used to refer to device files in DOS (con, prn, lpt1, etc.)

[–] [email protected] 9 points 9 months ago (1 children)

Really? Doesn't seem to work when I use bash:

bash-3.2$ cd a/b/c/d/e
bash-3.2$ ...
bash: ...: command not found
bash-3.2$ cd ...
bash: cd: ...: No such file or directory
[–] [email protected] 3 points 9 months ago* (last edited 9 months ago)

I have a function called up. I do up X where X is the number of directories I want to go up.

up() {
  if [[ $# -eq 0 ]]; then
    cd ..
    return 0
  fi
  local path i
  for (( i=0; i < $1; i++ )); do
    path+=../
  done
  cd "$path"
}

EDIT: Don't know if it's just me but if you see < it should be the less than character.

[–] [email protected] 3 points 9 months ago

Awesome, I didn't know that either! TIL

[–] [email protected] 2 points 9 months ago

I use this all the time in zsh, and it’s one dot per directory up.

……… will go up 8 directories

cd is also optional in zsh, you can just type “..” without cd