Change to Custom Directory Alias With Completions in Zsh (2021)
6 points by Aks
6 points by Aks
Maybe I’m confused, but I think you can do this more easily. You don’t need a function to bookmark directories. Instead, you can use CDPATH
or even better hash -d
which gives you TAB-completion for free.
~ % hash -d dots=Documents/git-repos/dotfiles
~ % nvim ~dots/[TAB]
base16.yml gitignore_global pack-vim.sh*
bash_aliases go-extras/ port.awk
bash_completion go-kickstart.sh* profile
bash_functions home-setup.sh README.md
bash_path hushlogin run-help-debian
bashrc inputrc run-help-macos
bin/ IR_Black_Mine.terminal stylua.toml
bin-linker.sh* launchagents/ test-run-help.zsh
config/ LICENSE.txt TODO.md
dot_copier.sh* macOS.sh* vim/
dot_linker.sh* mail-skel.sh Vim-big.terminal
drive.icns mpdconf Vim-small.terminal
etc_bashrc nvim-kickstart.bash* Vim.terminal
etc_profile nvim-link.sh* zshenv
foo/ old-zshrc zshrc
gitconfig pack-nvim.sh*
zsh even lets you define a hook to generate these bookmarks on the fly ( see Dynamic Named Directories in zshexpn(1)).
There’s a lot cool things you can do with this.
My favorite is a bookmark ~[git]
that resolves to the root of the repository I’m in. I use this so much that it stopped me from switching to fish because I couldn’t figure out an equivalent that worked as well.
I’m curious about your code for the ~[git]
bookmark and also about why you find it so valuable. Here’s what I came up with.
zsh_directory_name() {
emulate -L zsh
if [[ $1 = n ]] && [[ $2 = git ]]; then
local git_root
git_root=$(git rev-parse --show-toplevel 2>/dev/null)
if [[ -n $git_root ]]; then
typeset -ga reply
reply=("$git_root")
return 0
fi
fi
return 1
}
This gives me cd ~[git]
to go to the root of a git repo and nvim ~[git]/<Tab>
to quickly get tab completion for files in the root of the repo from any depth. (I’m ignoring the c
and d
options because I don’t currently have a use for them.)
In any case, I wonder how you define the function and how you use it.
I didn’t know about dynamic named directories. Thanks. (zsh reminds me a lot of vim/neovim: there’s always more to learn.)
My “problem” with that was that I did not want to type ~ first.
But it’s still great tip.
My “problem” with that was that I did not want to type ~ first.
Fair enough. I can see how that is annoying. CDPATH
does not require you to type ~
, and I believe that it gives you tab completion for free (or freely with zsh’s existing completion). An advantage of hash -d
for me is that I get tab completion for all commands rather than just for cd
, which is what CDPATH
gives me. For that, I’m willing to type ~
, but I completely understand if you prefer another method.
Found this super useful for my usecase, so wanted to share this post for anyone else needing similar functions.
Curious if people have any other helper zsh/bash/fish functions here!
You could probably also use
cd d/g/d
to go into Documents/git-repos/dotfiles
using a custom matcher-list:
autoload -Uz compinit
compinit
zstyle ':completion:*' matcher-list \
'' \
'm:{a-z\-}={A-Z\_}' \
'r:[^[:alpha:]]||[[:alpha:]]=** r:|=* m:{a-z\-}={A-Z\_}' \
'r:|?=** m:{a-z\-}={A-Z\_}'
Explanation of each line:
Source: I think this is from StackOverflow when it was good back in the days ;-)