The Computer Oracle

Zsh, directory tab-completion with prefix

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Digital Sunset Looping

--

Chapters
00:00 Zsh, Directory Tab-Completion With Prefix
00:42 Accepted Answer Score 14
01:09 Answer 2 Score 3
01:34 Answer 3 Score 1
01:59 Answer 4 Score 0
02:23 Thank you

--

Full question
https://superuser.com/questions/381368/z...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#zsh #tabcompletion #prefix

#avk47



ACCEPTED ANSWER

Score 14


Here's an example from my own .zshrc that I keep as a copy/paste snippet:

# Try using the below template to set up zsh functions that act
# as aliases to cd and allow you to get autocomplete nicely.

project() { cd /path/to/project/$1; }
compctl -W /path/to/project/ -/ project

Just edit the /path/to/project section in both lines above then you are good to go.




ANSWER 2

Score 3


A different way of achieving a similar effect is to define aliases for directories:

setopt auto_cd
alias -d s=~/projects

Type ~s/ Tab to change to a subdirectory of ~/projects; you can use ~s in a command argument, too.




ANSWER 3

Score 1


You can add an entry directly to the named directory hash table:

hash -d s="${HOME}/projects"

Now you can simply use ~s to refer to your directory, and you can use Tab-completion in a cd command: cd ~s/Tab

If you have zsh's autocd option set, you can leave out the cd.




ANSWER 4

Score 0


You can modify CDPATH:

export CDPATH="$CDPATH:$HOME/Repositories:$HOME/Repositories/jc/Projects"

Now you can access all subdirectories within the specified directories from every other directory as if they would be the subdirectories of the current directory:

➜  ~ /tmp
➜  /tmp pwd
/tmp
➜  /tmp MyAwesomeProject 
~/Repositories/jc/Projects/MyAwesomeProject
➜  MyAwesomeProject git:(master) pwd
/Users/rafael/Repositories/jc/Projects/MyAwesomeProject
➜  MyAwesomeProject git:(master)