by xmdx · 6y ago · 0 comments
For example if I set the following alias:
alias hello = '~/Docuemnts'
It would be nice if I could then execute hello/world to take me to ~/Documents/world but it requires hardcoding.Guess I could play around with functions and arguments to solve this.
I keep all my code in ~/code
$ c # cd to ~code
$ c pr<tab> # tab complete to c projectname
$ c projectname # cd to ~/code/projectname
This is pretty simple in zsh
# c to cd to ~/code
c() { cd ${CODE_DIRECTORY}/$1; }
# tab complete c against code
_c() { _files -W ${CODE_DIRECTORY} -/; }
compdef _c c
(Note: CODE_DIRECTORY is just an env var for ~/code)
I use this all the time and I use h to do the same for my home directory
https://github.com/gpspake/dotfiles/blob/master/zsh/gpspake/...
hello=~/Documents
Then: cd ~hello/world
Or omitting `cd` (requires `setopt auto_cd`): ~hello/world
And with `setopt cdable_vars`: hello/word
This also works with tab completion: hello/<TAB> hash -d hello=~/Documents
~hello/world
The shortened names also show up in your PROMPT if it is using %~ to display cwd.Also, apparently you can add this to zsh with a plugin: https://github.com/momo-lab/zsh-abbrev-alias
$ hash -d hello=$HOME/Documents
$ cd ~hello/world && pwd
/home/user/Documents/world