Zsh: Make a certain set of aliases take precedence in completion suggestions - zsh

I have a couple of functions set in my zsh profile which let me save aliases to directories on the fly. It's pretty simple - just adds an alias to cd to the dir to a ~/.dirs file which gets sourced.
Is it possible in zsh to make aliases which come from this file take precedence in suggested tab completions? For example, there's an alias in there called 'printeffect'.. If I type 'printe' and hit tab, the first completion suggestion is 'printenv'.. I want it to be 'printeffect'.

This should work:
an array holding your current special aliases, you only need to redefine the array when you switch dirs
typeset -U MY_DIR_ALIASES
MY_DIR_ALIASES=(foo1 foo2 foo3 foo-etc)
a custom completer function _foo (file name is _foo) completing when the cursors in a -commands- position:
_foo() {
cmds=( $MY_DIR_ALIASES )
_describe -commands- cmds
}
Or if you are saving this inside a file named _foo:
#compdef -command-
# -*-shell-script-*-
cmds=( $MY_DIR_ALIASES )
_describe -commands- cmds
making this completer function be ran before any other:
zstyle ':completion:::::' completer _foo _complete _approximate

Related

Zsh completion for global aliases

Is there a way to get tab completion for global aliases in zsh? Defined as:
% alias -g zshplugins=~/.zshplugins
% nvim zshpl[tab] would not tab complete.
I use global aliases mainly to not have to enter the path to a file nor restrict myself to a single command (e.g., alias zshplugin="nvim ~/.zshplugins")
I understand that these are also meant to be used inside arbitrary one-liners (a global alias for | grep -i, for instance) and don't make sense to suggest on every tab stroke, but If there's some workaround to include these in directory/file completion, that would be great.
You should use the shell variable but not global alias.
But if you want, add following code to your zshrc
_complete_alias() {
[[ -n $PREFIX ]] && compadd -- ${(M)${(k)galiases}:#$PREFIX*}
return 1
}
zstyle ':completion:*' completer _complete_alias _complete _ignored
Would zshpl[tab] complete at the beginning of the line (leaving out nvim)? This what I should work. An alias is not meant to be used as a shortcut for a file name. Shell variables can be used for the latter (and there is a completion on them too). I suggest that you define
zshplugins=~/.zshplugins
and then do
nvim $zshpl[tab]

Reload aliases on change automatically without closing the shell window

I have my aliases stored in ~/.zsh_aliases and sourced in ~/.zshrc:
# Access custom aliases in the shell
[ -e "${HOME}/.zsh_aliases" ] && source "${HOME}/.zsh_aliases"
However, when changing the name of an alias, I have to always close the current shell window and open a new one for the change to become active.
Can Zsh automatically reload aliases on change to make them available without having to close the shell window?
You do not actually need to close and reopen your terminal for that, just running source ~/.zsh_aliases (loads the new and changed aliases) or maybe exec zsh (replaces the current shell with a new one) would work, too.
If you really want to re-source ~/.zsh_aliases whenever it is modified, I would suggest adding the following to your ~/.zshrc:
# File containing aliases;
ALIAS_FILE="${HOME}/.zsh_aliases
reload_aliases () {
# do nothing if there is no $ALIAS_FILE
[[ -e ALIAS_FILE ]] || return 1
# check if $ALIAS_FILE has been modified since last reload
# the modifier `(:A)` resolves any symbolic links
if [[ $LAST_ALIAS_RELOAD < $(stat -c %Y ${ALIAS_FILE}(:A)) ]]; then
# remove all aliases; optional!
# only do this if all of your aliases are defined in $ALIAS_FILE
# also affects aliases defined on the command line
unalias -m '*'
# load aliases
source $ALIAS_FILE
# update date of last reload
LAST_ALIAS_RELOAD=$(date +%s)
fi
}
# make reload_aliases to be run before each prompt
autoload -Uz add-zsh-hook
add-zsh-hook precmd reload_aliases
Note, that any changes will only be available on a new prompt. That means, if you modify ~/.zsh_aliases, you need to press at least Enter once in the all terminals for the changes to take effect.
I use an alias, thusly: -
alias vialias='vi ~/.oh-my-zsh/custom/alias.zsh ; source ~/.oh-my-zsh/custom/alias.zsh'
When I run vialias, I edit my aliases, then when I leave vi, the change(s) take effect.
To simplify the accepted answer add:
source ~/.zsh_aliases
in the ~/.zshrc below the plugins section.
Then add an alias inside the ~/.zsh_aliases like so:
alias f="exec zsh"
To refresh zsh & aliases type f

Zsh expand alias to result of function as string

Really quirky title, I know.
Basically, I have this:
alias vv="xclip -selection clipboard -o"
which prints out anything in my clipboard, such as a repository location in ssh-form (git#github.username/repname.git).
Now I'd like to:
git clone vv
I tried several variations of the above, such as trying various switches on the alias, or using different expansions, but with no luck.
Any suggestions?
Global alias might do it... actually it does it:
alias -g vv="$(date)" # replace 'date' with your command of choice
Notice:
it is a global alias, so it works anywhere in the command line (not just the beginning)
$(...) will do command substitution and expand it as a variable, see man zshexpn and search for $(...). By default zsh will not break the results using white-spaces.
[...]
I initially wrote a suggestion to create a (zsh) widget to insert the clipboard into the command line with a given key combination, then I realized that you would just likely hit "Ctrl-Shift-V" or something... :-S
[...]
FYI, this is how you would do this using a zsh widget:
that inserts the clipboard content on the command line, and binding it to some key, as it would allow you to see what you are doing before hitting enter. Place the following into your $fpath, inside a file called insert-clipboard (needs to be loaded with KSH_AUTOLOAD set)
#! /bin/zsh
## Inserts the output of the command into the cmd line buffer
zmodload -i zsh/parameter
insert-clipboard() {
LBUFFER+="$(date)" # REPLACE date BY YOUR COMMAND!
}
At your .zshrc
autoload insert-clipboard # as written, it needs KSH_AUTOLOAD set....
zle -N insert-clipboard
bindkey '^Xu' insert-clipboard # pick a key combination you like...

Make zsh complete arguments from a file

zsh is great but its completion system is very diverse. And the documentation lacks good examples. Is there a template for completing for a specific application. The completion would get its match data from a file, separated by newlines?
I tried modifying an older example of mine that takes match data "live":
~ % cat .zsh/completers/_jazzup
#compdef jazz_up
_arguments "2: :(`mpc lsplaylists|sed -e 's# #\\\\ #g'`)"
I could supply cat my_file there instead of mpc invocation and so on but would there be a more elegant way to do this simple task? And that completion there is placement-specific: can you provide an example where zsh would attempt to complete at any point after the program name is recognized?
The match data will have whitespaces and so on, the completion should escape the WS. Example of that:
Foo bar
Barbaric
Get it (42)
Now if that completion would be configured for a command Say, we should get this kind of behaviour out of zsh:
$ Say Fo<TAB>
$ Say Foo\ bar
$ Say Ge<TAB>
$ Say Get\ it\ \(42\)
Simple completion needs are better addressed with _describe, it pairs an array holding completion options and a description for them (you can use multiple array/description pairs, check the manual).
(_arguments is great but too complex.)
[...]
First create a file
echo "foo\nbar\nbaz\nwith spac e s\noh:noes\noh\:yes" >! ~/simple-complete
Then create a file _simple somewhere in your $fpath:
#compdef simple
# you may wish to modify the expansion options here
# PS: 'f' is the flag making one entry per line
cmds=( ${(uf)"$(< ~/simple-complete)"} )
# main advantage here is that it is easy to understand, see alternative below
_describe 'a description of the completion options' cmds
# this is the equivalent _arguments command... too complex for what it does
## _arguments '*:foo:(${cmds})'
then
function simple() { echo $* }
autoload _simple # do not forget BEFORE the next cmd!
compdef _simple simple # binds the completion function to a command
simple [TAB]
it works. Just make sure the completion file _simple is placed somewhere in your fpath.
Notice that : in the option list is supposed to be used for separating an option from their (individual) description (oh:noes). So that won't work with _describe unless you quote it (oh\:yes). The commented out _arguments example will not use the : as a separator.
Without changing anything further in .zshrc (I already have autoload -Uz compinit
compinit) I added the following as /usr/local/share/zsh/site-functions/_drush
#compdef drush
_arguments "1: :($(/usr/local/bin/aliases-drush.php))"
Where /usr/local/bin/aliases-drush.php just prints a list of strings, each string being a potential first argument for the command drush. You could use ($(< filename)) to complete from filename.
I based this on https://unix.stackexchange.com/a/458850/9452 -- it's surprising how simple this is at the end of the day.

bind key to complete filename wherever the context is in Zsh

Sometimes I want a filename instead of what zsh guesses for me. For example, I have a PNG file without a proper .png suffix, which makes zsh think it isn't a picture and won't list it when I type Tab to complete arguments of display.
I am wondering if there is a key sequence that completes for a filename wherever the context is, like ^XC for _correct_filename, or how to configure zsh to do the thing?
You can add a key binding to do what you want:
zle -C complete complete-word complete-files
bindkey '^X\t' complete
complete-files () { compadd - $PREFIX* }
Edit: Added $PREFIX
You can add those lines to your ~/.zshrc file.
That makes the completion list all files when you press Ctrl-x Tab at each step instead of Tab. You could choose another keystroke combination that suits you.
Or to make ImageMagick completions always include all files, try editing (make a backup first) the file /usr/share/zsh/functions/Completion/Unix/_imagemagick (or similar) and change this to comment out the existing line that begins with _files and add the new one shown:
if (( $# )); then
# _files "$#" -g "*.(#i)(${~formats//:/|})(-.)"
_files "$#"
return
fi
Dennis' answer didn't expand tilde for me, so I would get stuff like complete-files: no matches found: ~/ma* when I tried to invoke it on foo ~/ma. I did find an alternate in the zsh FAQ that will expand them, though:
zle -C complete-file complete-word _generic
zstyle ':completion:complete-file::::' completer _files
bindkey '^xF' complete-file

Resources