Skip to content

Instantly share code, notes, and snippets.

@farhad-taran
Last active February 25, 2021 10:04
Show Gist options
  • Save farhad-taran/2ebb154035c4787dc3a407ce5bfbf8f2 to your computer and use it in GitHub Desktop.
Save farhad-taran/2ebb154035c4787dc3a407ce5bfbf8f2 to your computer and use it in GitHub Desktop.
How to automatically navigate to current open folder when opening a new bash session

you can add the following script to your .zshrc and this will cause the newly opened bash session to automatically go to the path of the currently opened file explorer.

cdf() {
      target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
        if [[ $target == *"Desktop"* ]]; then
	    return
	elif [[ $target != "" ]]; then
            cd "$target"; pwd
        fi
}

cdf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment