13

I use numerous aliases set at a given UNIX environment. For example my current fish shell under OSX have these below

  • gm -> git merge
  • dk -> docker kill

and so on.

Now the challenge is I use iTerm 2 for connecting to various remote sessions and I wish these aliases work seamlessly on all sessions.

Is there a way to push these aliases into a global context so that iTerm 2 will input the original command upon detecting these aliases.

i.e if I hit gm + enter in any iTerm window it must issue git merge to the remote session.

Max
  • 103
  • 3
nehem
  • 240
  • 1
  • 2
  • 12
  • 1
    iTerm 2 has nothing to do with your aliases. These are defined in your .`bash_profile`. *I use iTerm 2 for connecting to various remote sessions* - Do you mean you connect to various (meaning *different*) servers and/or accounts? – Allan Mar 05 '18 at 14:03
  • Which shell are you running in iTerm? – mmmmmm Mar 05 '18 at 14:18
  • @mark I'm using fish shell in my local. However the servers I connect via ssh won't be having fish shell usually. – nehem Mar 05 '18 at 22:32
  • @Allan I agree, but as the #1 terminal app, iTerms can add this as a feature so that it can add the layer of abstraction. There's no point in adding entries `bash_profile` on numerous servers and keeping them in sync. – nehem Mar 06 '18 at 23:18
  • That's not the function of the Terminal and by extension iTerm. The entire idea behind having a `profile` on a server is so that aliases you create are there regardless of the terminal you attach with. If you want "sync" your profiles, look to rsync. – Allan Mar 07 '18 at 00:12
  • I guess we're stuck from two correct perspectives, maintaining `.bashrc` `bash_profile` on multiple servers aren't for me. Because the servers are dynamic (ips keep changing), they are owned by shared accounts, not all servers would contain `fish` shell because entries for fish aliases would go under `config.fish` file. It's not my job/privilege to install fish shell to servers that I don't own. After all what I have is iTerm which can/can be made to abstract this. Hope this explains my perspective little. – nehem Mar 07 '18 at 01:06
  • Whch tool do you use to make iTerm2 connect onto another remote system? Which shell run these other systems? (Please add this info. in your OQ so as to avoid it to disappear with comments). – dan Mar 28 '19 at 15:35

2 Answers2

12

If you use oh-my-zsh, you will want to place those in ~/.zshrc

vi ~/.zshrc

.
.
.
# Example aliases
# alias zshconfig="mate ~/.zshrc"
# alias ohmyzsh="mate ~/.oh-my-zsh"
alias work="cd ~/Lucas/local-sites/"
  • a) OP is using bash and fish not zsh. b) How does this file run on remote servers? – mmmmmm Mar 28 '19 at 13:27
  • Hi @Mark, it's very common for those using iterm2, to use oh-my-zsh, they are a known combo on the mac environment. The title of the question is "Creating aliases in iterm2", not "Creating remote aliases in iterm2", and that's the keywords that generates traffic from Google to this question, so that's what people effectively are searching for. My answer might not answer all questions, but it answers for a considerable percentage of people that end up in this question, so I think it's a valid answer. One of the many possible. – Lucas Bustamante Mar 29 '19 at 13:15
  • 1
    Sounds like we need to edit the title to match the question – mmmmmm Mar 29 '19 at 14:53
  • Im using oh-my-zsh, and it works perfectly. Thanks man – Saitama Jun 07 '21 at 00:35
  • these doesn't work for ssh sessions – Hayk Safaryan Jul 23 '21 at 12:35
  • While the question is not marked clearly for the type of shell it clearly talks about the aliases must work for system wide (even for the ssh sessions). This answer needs to be removed as it’s not a real answer. – nehem Sep 30 '21 at 00:21
  • @nehem it will work if you use oh-my-zsh on remote SSH sessions, I install it on docker myself because I like the control it gives me. It might not suit your scenario, but certainly helps other people, and that's why it's allowed to have more than one answer to the same question, to cover more ground about the question being asked – Lucas Bustamante Sep 30 '21 at 20:15
  • That’s the whole point of the question. You cannot install it on ssh servers, some of the servers you won’t even have access to. This question is iTerm related and nothing to do with any shell usage. When it comes to shell fish will smoke out zsh at any given scenario. – nehem Oct 01 '21 at 00:27
2

Have you tried creating a .bashrc file with these aliases?

It should be created in your home folder ~/.bashrc

Chris
  • 631
  • 3
  • 15
  • Do you think `.bashrc` entries will be transported to remote sessions (`ssh`)? I guess not. – nehem Mar 05 '18 at 22:30
  • No you’re right, what I meant was to put them on the remote sessions (if possible) – Chris Mar 06 '18 at 15:54
  • For few obvious reasons, I wouldn't do that, 1) The servers I connect occasionally would involve shared account details, which I wouldn't pollute them. 2) The servers I often connect are so dynamic (Ec2 instances) so everytime I need to open `vi` and edit the `.bashrc`. Let me know if there are ways to overcome the second one. – nehem Mar 06 '18 at 23:15
  • @nehemiah Use a quick script that when creating instances also creates ~/.bash* files. There are may examples - search for dotfiles also see Docker setups which do this - I don't know if ec2 is similar to Docker here. – mmmmmm Mar 28 '19 at 13:29