I am using the awesome zsh framework oh-my-zsh . Every day I hit gc
which is an alias for git commit
I hit <Tab><Tab>
and it gives me the correct files to commit. I am really interested to know how that is implemented, so I can implement my own idea of navigating to sub-directories using an alias.
I mean navigating into ~/workspace/a-repo
using this alias -w a<Tab><Tab>
which completes to a-repo
and gives me some suggestions which are folders inside ~/workspace
. I made this alias already but I need to know how to add the correct autocompletion/suggestion to it.
Here is my alias:
-w() { cd ~/workspace/"$*" }
In the case of gc
(which I assume is defined as alias gc='git commit -v'
in the git plugin of oh-my-zsh ) zsh
internally substitutes the alias ( gc
) with the actual command ( git commit -v
) before looking for trying for completions. This works for every alias (unless the shell option COMPLETE_ALIASES
is set, which would allow to set separate completions for aliases).
As for what you want to do: Seing that -w
is actually a function and not an alias, you indeed would have to write your own completion. But there is a much simpler way to go about it: Static named directories .
Named directories are usually just the home directories of the users on the system. The most commonly known is probably ~
for the home directory of the current user. Other users directories are named ~USERNAME
. You can also define your own static named directories . There are two ways to do this:
hash -d
to explicitly define a name, similar to an alias: hash -d w=~/workspace
/
(this also means you cannot use ~
as shortcut for your home directory) w="${HOME}/workspace"
In both cases you now can use ~w
to reference your workspace, in the second case you can also use $w
(for example for use in quoted strings). Typing cd ~w/a-repo
will get you to ~/workspace/a-repo
. Tab-completion works like it would with any other path, so pressing ~w/a<Tab>
will present you ~w/a-repo
as completion.
Additionally, if you set the shell option AUTO_CD ( setopt AUTO_CD
), you can switch into a directory just by typing the path (or directory name) without the need for cd
.
% w="/tmp"
% cd ~w
% pwd
/tmp
% mkdir 1 2 3
% setopt AUTO_CD
% ~w/<TAB>
1 2 3
% ~w/1
% pwd
/tmp/1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.