Problem while trying to echo my manual aliases using Zsh - zsh

I'm facing an issue while trying to use alias in my zsh
If I run alias , I get a list of all my alias, the ones in .aliases file, and the ones that I run manually in zsh:
oloco#Arch: alias
my_manual_alias='yes'
cat='bat --paging=never -l log'
cp='cp -iv'
doc=/home/oloco/Documents
dot=/home/oloco/.dotfiles
dow=/home/oloco/Downloads
egrep='egrep --color=auto'
fgrep='fgrep --color=auto'
But, I I try to echo any of them, nothing is shown:
oloco#Arch: echo $my_manual_alias
Why this behaviour? I think I should get my alias this way...or maybe not?
Thank you in advance!

Related

Tab completion for alias with the same names in zsh

I am using zsh.
I have a command foo, and I use alias foo="sudo foo" as a shortcut. I want zsh to complete the command just like I typed foo.
I googled and found compdef _foo f=foo which can deal with it. When I type f something<tab>, the completion works fine. But when I try compdef _foo foo=foo and then foo something<tab>, it does not work.
Is there a way to deal with the Tab completion when I use an alias with the same name as the original command?
The problem is actually a special case happened to me.
As #blueray said, zsh is able to complete commands like sudo foo or an alias of it normally.
Thanks to #rcwnd_cz , I found that adding setopt complete_aliases solves my problem. The point is that I set an alias somewhere else that maps sudo to sudo FOO=bar. As a result, alias foo="sudo foo" is an "alias in alias", and zsh refuses to complete it. Setting setopt complete_aliases solves it perfectly.

What can I change in order that Compdef can bind my function?

I have created some console commands in PHP. I need to use autocompletion when I launch my tasks.
I used alias otra="php console.php" in my .zshrc file.
The function itself seems to work but when I type my command name 'otra', there is only folders autocompletion...which is completely irrelevant in my case.
#compdef _otra otra
function _otra {
local line
_arguments -C \
"1: :(createAction createBundle)" \
"*::arg:->args"
}
I want to only have the two words createAction and createBundle to appear when I type otra and type .
EDIT
Ok, it is what I was thinking...if I remove my alias, the completion works but I cannot remove it since otra is not a valid command...
I tried to use setopt no_complete_aliases as I see it in another Stackoverflow post but it does not work for me.
In fact, it was related to the alias but I did not have to set setopt no_complete_aliasesin my case but pretty much the contrary...
After I put setopt complete_aliases in my .zshrc file, it works :D

zsh: alias with $(command) parameter

My Goal
I'm using the following command quite frequently:
vim $(fzf)
It uses fuzzy find to search a list of files, and then open the highlighted one in vim.
My Problem
I would like to alias vim $(fzf), but when I add alias v="vim $(fzf)" to .zshrc, fzf is executed whenever I open a new shell.
My Question
How do I set zsh to execute the $(command) only when the aliased command is executed?
Simply replace
alias v="vim $(fzf)"
With
alias v='vim "$(fzf)"'

how to properly expand filenames in zsh alias

Wrote a seemingly simple alias to convert mp3 to wav but doesn't expand the files at run time. Changed it to a function after I was unable to get it working.
Was hoping to get an explanation of why it didn't work as written.
alias 2wav="for fn in *.mp3;do echo \"Converting $fn\";avconv -y -i $fn ${$(basename $fn .mp3)}.wav 2>/dev/null;done"
Do any of your filenames contain spaces? e.g. foo bar.mp3 would produce a command line of
avconv -y -i foo bar.mp3 foo bar.wav
^^^---input file, doesn't exist
^^^^^^^--output file, but since there's no input, it's useless
^^^^^^^^^^--- miscellaneous unknown/invalid arguments
You'd need quotes around the arguments:
alias ........ -y -i "$fn" "${$(basename .....}".wav
Solved (at least partially).
alias 2wav='for fn in *.mp3;do echo "Converting ${fn}";avconv -y -i ${fn} ${$(basename ${fn} .mp3)}.wav 2>/dev/null;done'
Not sure why this fixed it but I enclosed the command with single quotes and added braces around the vars and it just worked. True that spaces in file names would still be a potential problem. I'll have to experiment with that.
Solved.
alias 2wav='for fn in *.mp3;do echo "Converting ${fn}";avconv -y -i "${fn}" "${$(basename "${fn}" .mp3)}".wav 2>/dev/null;done'
Now works for filenames containing spaces. I'm guessing that the single quotes prevent the expansion from happening until runtime and that's why this works.
Any clarification is welcome...

How can I swap (as in alias) command names in zsh?

If I try
$ alias pwd=echo
$ alias echo=pwd
I get
$ pwd
/home/owen
$ echo hi
hi
It seems aliases are followed recursively. Perhaps if there was a way to stop the recursion, that would work. In this example I can use builtin, but that won't work in general, particular for -g aliases.
All you want is that your commands are expanded once.
As describe in the zsh documentation; you can prevent an alias expansion by quoting.
This should do the trick
$ alias pwd="\echo" ; alias echo="\pwd"
There are two ways to do the equivalent of using builtin for commands which aren't built-in shell commands
The first is to just use the full path for the command you get from which. For example,
> which cat
/bin/cat
> /bin/cat
will run cat, ignoring any alias you may have set up.
The other option is to use /usr/bin/env. For example,
> /usr/bin/env cat
will also run cat while ignoring any alias to cat you may have set up.

Resources