Christian
Christian

Reputation: 4249

Use zsh's global aliases inside argument

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 $).

Upvotes: 4

Views: 757

Answers (2)

chepner
chepner

Reputation: 531135

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

Upvotes: 7

Mad Physicist
Mad Physicist

Reputation: 114310

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.

Upvotes: 2

Related Questions