Hartator
Hartator

Reputation: 5145

How to rerun a series of statements in the same order in a shell?

I bet you have came across the same problem like me. I use shell(s) a lot. For python, ruby(irb), mongo... I use some shortcurt like crt+a to go at the begining, crt+e at the end, crt+k to clean to the end.

But I miss one thing. When you define a multiline block in a ruby shell or a python shell and you miswrite something in one statement, you have to rewrite almost everything and the history bring by the up arrow becomes a mess when you have more than 3 lines.

Any tips to make this less painful ?

Upvotes: 2

Views: 460

Answers (4)

Moiz Raja
Moiz Raja

Reputation: 5760

For ruby see if pry works for you: http://pry.github.com

pry is an alternative to irb and it has a pretty flexible hist command to retrieve and replay history. It gives far more control over changing multiline statements. See this gist to understand the kind of thing you can do with pry: https://gist.github.com/972057

Upvotes: 1

horseyguy
horseyguy

Reputation: 29905

Pry (A Ruby REPL) was designed to make this kind of thing less of a pain. There are a few options to deal with this situation:

(1) Use amend-line to fix the line.

enter image description here

(2) Dump the current input buffer into an editor using the edit command, the changes you make in the editor are then brought back into the Pry session after saving/quitting.

(3) Use hist --replay to replay and then correct the lines in the REPL.strong text

Of these options i typically use amend-line if it's a simple error, and edit if it's more involved.

Have fun!!

Upvotes: 3

kindall
kindall

Reputation: 184200

For Python just use IDLE, which comes with it. If you position the cursor in a multi-line statement in the terminal window and hit Enter, the whole statement is copied to the input line for editing.

Upvotes: 0

unutbu
unutbu

Reputation: 879899

Find a text editor that can send regions of code to a python, ruby or mongo session. Emacs can do this; I'm sure vim and other editors can do it as well.

This way, you can edit and save your work and still take advantage of the interactive shell.

Below is an example where random_pick is defined in a text file, the region is selected and sent to IPython by pressing Ctrl-c Ctrl-.. It shows up in IPython on the line that starts with In [5]:. The next line shows IPython now knows about random_pick.

enter image description here


For IPython (as opposed to the default Python shell), there is also %cpaste, which allows you to cut-and-paste multline-blocks of code into the shell:

In [54]: %cpaste
Pasting code; enter '--' alone on the line to stop.
:def random_pick(choices,probs):
    cutoffs=np.cumsum(probs)
    idx=cutoffs.searchsorted(random.uniform(0,cutoffs[-1]))
    return choices[idx]
--
:::::::::

In [55]:

Upvotes: 1

Related Questions