开发者

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

开发者 https://www.devze.com 2023-04-13 09:55 出处:网络
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 th

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 ma开发者_开发百科ke this less painful ?


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.

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

(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!!


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.

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


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]:


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消