3

How do you port your bash aliases to IPython version >= 0.11?

This question has already been answered for IPython < 0.11, and link for that is here:

http://ipython.scipy.org/Wiki/tips

Alex Gaudio
  • 1,894
  • 1
  • 16
  • 13
  • this looks ok so.. what's the question? – wim Sep 28 '11 at 14:29
  • 3
    Alex, it would be better to ask your question in the question and then write your answer as an answer to this question - and if no one else offers a better way to do it, then accept your answer :-) – Sean Vieira Sep 28 '11 at 19:11
  • Would you like to put this on the IPython wiki? http://wiki.ipython.org/Cookbook/Moving_config_to_IPython_0.11 – Thomas K Sep 28 '11 at 22:50
  • You can replace the chained *`.replace('=',' ').replace('"','').replace("'",'')`* with a single *`string.translate(s,table,deletechars)`* – smci Sep 30 '11 at 03:22
  • Thanks wim, Sean, Thomas and smci for your comments. I've followed them through – Alex Gaudio Oct 06 '11 at 23:43

1 Answers1

2

Here is my solution. Improvements welcome!

In your ipython config, add the following lines: (mine is here: ~/.config/ipython/profile_default/ipython_config.py)

c = get_config()

## Port bash aliases to ipython
import os, string
a = os.popen("bash -l -c 'alias'").read()
a = a.translate(string.maketrans("=", ' '), '\'"').split('alias ')
a = [tuple(x.strip().split(' ', 1)) for x in a]
c.AliasManager.user_aliases = [x for x in a if len(x) == 2]
Alex Gaudio
  • 1,894
  • 1
  • 16
  • 13
  • If you have aliases defined in another file you need to do `os.popen("bash -lc '. /the/file/where/you/have/aliases && alias'").read()`. This works for zsh as well. – Phillip Cloud Apr 24 '12 at 18:25
  • This does not work in ipython 0.13.2, the function `get_config()` doesn't exist. – qed May 09 '13 at 19:45
  • Important to mention: My ~/.bashrc was set so it doesn't execute while in non-interactive mode, and thus `"bash -lc"` returned nothing. If you have the same problem, try this `os.popen("bash -lci alias")`. The `-i` option is for interactive mode, and makes bash thing it's business as usual. – vlad-ardelean Nov 16 '14 at 11:35