Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use zsh's global aliases inside argument

Tags:

zsh

I use global aliases for some of my remote hosts like alias -g MY_HOST="server.waytolongfoobar.com" to be able to ssh into them using ssh MY_HOST. But if I want to scp something to the host, I have to use something awkward like scp file.tar.gz 'echo MY_HOST':~/dir. Is there a nicer way to use global aliases inside arguments?

Maybe I use global aliases for the wrong purpose? I think I could actually use exported variables like scp file $MY_HOST:~/dir. What's the benefit of global aliases over variables anyway (beside not having to type the $).

like image 429
Christian Avatar asked Sep 19 '25 09:09

Christian


2 Answers

You can define host aliases directly in ssh without using the shell. Add the following to your .ssh/config file:

host MY_HOST
    HostName server.waytolongfoobar.com

Now, any place where ssh or scp would take a host name, it will treat MY_HOST as a synonym for server.waytolongfoobar.com.


As to your question regarding the utility of aliases, they are expanded before any part of the command line is parsed, allowing you to include shell syntax:

% alias -g PAGE="| less"
% cat .zshrc PAGE         # expands to cat .zshrc | less
% alias -g ignore="> /dev/null"
% cat .zshrc ignore
like image 168
chepner Avatar answered Sep 23 '25 12:09

chepner


In your case, there is no good way to use an alias because it has to be an independent (space separated) token on the command line. Aliases can actually be less flexible than variables for that exact reason. The purpose of an alias is to save you typing. Global aliases are intended for files or other standalone expressions that you use very frequently. A host name is generally not standalone.

like image 25
Mad Physicist Avatar answered Sep 23 '25 12:09

Mad Physicist