Błażej Michalik
Błażej Michalik

Reputation: 5085

Equivalent of Bash "!!" in IPython?

I'm looking for a functionality similar to Bash' !!.

In Bash, if you type !!, it replaces it with the last command you typed. For example, if you type which deactivate, and then cat $(!!), the second command will be reformatted into cat $(which deactivate).

In IPython typing !! leaves you with an empty list, which I suspect is trying to pass me a list of outputs from two empty-string shell commands.

Is there a similar way of formatting last command into the newly typed one in IPython?

Upvotes: 2

Views: 59

Answers (1)

James
James

Reputation: 36691

You can grab any previous input in IPython by referencing the input number:

In [4]: 'hello'
Out[4]: 'hello'

In [5]: In[4]
Out[5]: "'hello'"

If you want to grab the previous input to the current line, you can use _i or In[-2] ( -1 references the current input):

In [6]: In[-2]
Out[6]: 'In[4]'

In [7]: _i
Out[7]: 'In[-2]'

Upvotes: 3

Related Questions