afurlan's blog

/^random (nerd|geek)? posts$/

tag: shell

Clearing the screen with IPython

Monday, November 16, 2009 - No comments

Some time ago I changed my default shell to use IPython instead of Bash. The IPython is a really great shell and IMHO has great advantages but for those who, like me, are completely addicted to the clear command, the IPython's clear is a huge disadvantage.

In IPython, the clear command clear varios data like input, output and directory histories but it doesn't clear the screen! You can take a look at the documentation of default function for the clear command below and see how it works:

afurlan@merlin:~$ from IPython.Extensions.clearcmd import clear_f
afurlan@merlin:~$ print clear_f.__doc__
 Clear various data (e.g. stored history data)

    %clear out - clear output history
    %clear in  - clear input history
    %clear shadow_compress - Compresses shadow history (to speed up ipython)
    %clear shadow_nuke - permanently erase all entries in shadow history
    %clear dhist - clear dir history

afurlan@merlin:~$

So I created a new function to extend the first one adding the option to clear the screen when no args were passed. If you'd like to do the same, add the following lines in your ~/.ipython/ipy_user_conf.py:

def new_clear_f(*args, **kwargs):
    ''' Extend the default clear function adding the option to
    clear the screen when no args were passed.'''
    if not args[1]:
            ip.system('clear')
    else:
            from IPython.Extensions.clearcmd import clear_f
            clear_f(*args, **kwargs)
            ip.expose_magic('clear', new_clear_f)
ip.expose_magic('clear', new_clear_f)

And now it all seems clear again. :)

As always: if you found some english bug, warn me and I'll be glad to fix it. :)

Using IPython as your default shell

Thursday, October 29, 2009 - Five comments

If you are a shell addicted person and like Python, then the IPython was made just for you! The IPython provides a very usefull feature (that may looks a bit strange at first) of mixing Python and Shell commands in a easy and intuitive way. I don't pretend to say why you should use IPython instead of your current shell but I think that, if you know Python, you should prefer to write Python code instead of Bash code.

I'll assume that you already have the IPython installed but if you don't, take a look at IPython's documentation to see how to install it. I'm running Debian and my setup is focused in my machine, so it should work for the most of the others linux distros but if you are running a different system, I'm sorry for you you may have to adapt some commands and/or paths. :)

Configuring the IPython's prompt

The first time you run IPython, it displays a warning about the creation of your personal configuration directory. One of the most important parts of this warning is about the new configuration file: ~/.ipython/ipy_user_conf.py. So let's configure this file and the IPython's prompt to make it looks exactly like the Debian's prompt. Edit your configuration file and change the following settings as below:

...
import ipy_profile_sh
...
o.prompt_in1 = r'\C_Normal\u@\H:\Y2$ '
o.prompt_in2 = r'\C_Normal... '
...

The first option, import ipy_profile_sh, avoid you to have to escape all the Bash commands. The second and third options, o.prompt_in1 and o.prompt_in2, change the IPython's prompt. And now start the IPython as follow:

afurlan@merlin:~$ ipython -nosep -nobanner -noconfirm_exit
afurlan@merlin:~$
afurlan@merlin:~$ 
afurlan@merlin:~$ print 'hello python!'
hello python!
afurlan@merlin:~$ echo 'hello bash!'
hello bash!

Now you're able to run Python commands:

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     print line
              ...    
              ...    
afurlan.org

blog.afurlan.org

And a mixed of Python and Bash commands:

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     echo "$line"
              ...    
              ...    
afurlan.org

blog.afurlan.org

afurlan@merlin:~$ for line in open('domains.txt'):
              ...     echo "$line.strip()"
              ...    
              ...    
afurlan.org
blog.afurlan.org

Setting the IPython as your default shell

Actually I don't like to set the IPython as my default shell... I use to run screen so I configure it to open five IPython sessions by default but if you really like to set it as your default shell, you can add the following line at the end of your ~/.bashrc file:

...
exec ipython -nosep -nobanner -noconfirm_exit

If you're a screen user too, you can add the following lines at your ~/.screenrc:

...
# open 5 ipython sessions by default
screen -t p 1 ipython -nosep -nobanner -noconfirm_exit
screen -t p 2 ipython -nosep -nobanner -noconfirm_exit
screen -t p 3 ipython -nosep -nobanner -noconfirm_exit
screen -t p 4 ipython -nosep -nobanner -noconfirm_exit
screen -t p 5 ipython -nosep -nobanner -noconfirm_exit
...

Or, if you like, you can also get my current .screenrc file.

And, as always: if you found some english bug, warn me and I'll be glad to fix it. :)