'Print your history of visited directories.
%dhist       -> print full history\
%dhist n     -> print last n entries only\
%dhist n1 n2 -> print entries between n1 and n2 (n2 not included)\
This history is automatically maintained by the %cd command, and
always available as the global list variable _dh. You can use %cd -<n>
to go to directory number <n>.
Note that most of time, you should view directory history by entering
cd -<TAB>.'

@line_magic
def dhist(self, parameter_s=''):

    dh = self.shell.user_ns['_dh']
    if parameter_s:
        try:
            args = map(int, parameter_s.split())
        except:
            self.arg_err(self.dhist)
            return
        if (len(args) == 1):
            (ini, fin) = (max((len(dh) - args[0]), 0), len(dh))
        elif (len(args) == 2):
            (ini, fin) = args
            fin = min(fin, len(dh))
        else:
            self.arg_err(self.dhist)
            return
    else:
        (ini, fin) = (0, len(dh))
    print 'Directory    history    (kept    in    _dh)'
    for i in range(ini, fin):
        print ('%d:    %s' % (i, dh[i]))
