Skip to content

Instantly share code, notes, and snippets.

@oxidizeddreams
Created November 27, 2018 14:41
Show Gist options
  • Save oxidizeddreams/de0f1349c45dbf7aeb02624548e7f40e to your computer and use it in GitHub Desktop.
Save oxidizeddreams/de0f1349c45dbf7aeb02624548e7f40e to your computer and use it in GitHub Desktop.
zsh directory stack autocompletion
# The directory stack keeps track of the various directories you've visited in a given shell session. Here is a way to make the stack persistent using zsh. The aim is to make it easier to switch between frequently visited directories. The basic idea for this was taken from a 1991 Usenet posting by Uri Guttman. Other bits come from Paul Falstad's Z Shell Guide.
# First, make a new file with the following contents:
#!/usr/bin/env ruby
dirs = []
ARGF.each { |line| dirs.push(line) }
dirs.reverse.each { |line|
puts "pushd #{line.chomp.gsub(/s/, ' ')}"
}
# Name the file savedirs.rb, save it somewhere in your path, and make it executable with chmod +x savedirs.rb.
# Next, add these lines to your ~/.zlogout file (create it if necessary):
# Save directory stack
dirs -p | savedirs.rb > ~/.dirstack
# Finally, add these lines to your ~/.zshrc file:
DIRSTACKSIZE=8
setopt autopushd pushdminus pushdsilent pushdtohome
alias dh='dirs -v'
# Restore directory stack
source ~/.dirstack
# You can use dh (directory history) to show the stack:
% dh
0 /var
1 /etc
2 /Applications
3 ~/Development/Scripts
# Use cd -3 to change to ~/Development/Scripts, for example. The directory stack will now be saved across shell sessions, with the added bonus that your shell will now open in the directory you were in last.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment