Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I use bash, but I like the zsh feature where you could type "cd -n" and go back n entires in your "directory stack", so I made my own version as a function. It's probably subtly broken in some way, since it's bash, but it works relatively well for me:

  cd() {
  	# Set the current directory to the 0th history item
  	cd_history[0]=$PWD
  	if [[ $1 == -h ]]; then
  		for i in ${!cd_history[@]}; do
  			echo $i: "${cd_history[$i]}"
  		done
  		return
  	elif [[ $1 =~ ^-[0-9]+ ]]; then
  		builtin cd "${cd_history[${1//-}]}" || # Remove the argument's dash
  		return 
  	else
  		builtin cd "$@" || return # Bail if cd fails
  	fi
	# cd_history = ["", $OLDPWD, cd_history[1:]]
  	cd_history=("" "$OLDPWD" "${cd_history[@]:1:${#cd_history[@]}}")
  }
Other than that, I doubt there's much that would be generally useful to anyone.


Funnily enough, I did the exact same thing but for zsh's two-argument cd:

  cd() { if [[ -n "$2" && "$1" != "--" ]]; then builtin cd "${PWD/"$1"/"$2"}"; else builtin cd "$@"; fi; };
It's niche functionality, but super useful when you hit its niche:

  user@machine:/usr/src/linux-kbuild-4.9/scripts/mod$ cd 4.9 4.8
  user@machine:/usr/src/linux-kbuild-4.8/scripts/mod$ 
I'll leave combining these two as an exercise for the reader.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: