Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In addition to @rburing's answer, I would add a couple more things:

First there is a %%file "cell magic" that is very convenient if you just want to save some input to a file: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-writefile (it's documented as "writefile" but there is an alias of it shortened to just "file"). Put this at the top of an existing input like:

sage: %%file foo.sage
....: <the existing code>

When you run this, instead of executing the code it will just save it to a file. You can prepend this to an existing code cell too. I do this sometimes if I want to save small snippets of code out to a file. It's not always the most convenient though.

Another useful "magic" (Sage's interface is built on IPython so all the IPython magics listed on the above-linked page are relevant) is %history. If you just run %history it will print out all the recently-entered code without the prompts. E.g.:

sage: 1 + 1
2
sage: for x in range(5):
....:     print(x)
....:
0
1
2
3
4
sage: %history
1 + 1
for x in range(5):
    print(x)
%history

(note that this includes the %history command itself as part of the history).

In addition to @rburing's answer, I would add a couple more things:

First there is a %%file "cell magic" that is very convenient if you just want to save some input to a file: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-writefile (it's documented as "writefile" but there is an alias of it shortened to just "file"). Put this at the top of an existing input like:

sage: %%file foo.sage
....: <the existing code>

When you run this, instead of executing the code it will just save it to a file. You can prepend this to an existing code cell too. I do this sometimes if I want to save small snippets of code out to a file. It's not always the most convenient though.

Another useful "magic" (Sage's interface is built on IPython so all the IPython magics listed on the above-linked page are relevant) is %history. If you just run %history it will print out all the recently-entered code without the prompts. E.g.:

sage: 1 + 1
2
sage: for x in range(5):
....:     print(x)
....:
0
1
2
3
4
sage: %history
1 + 1
for x in range(5):
    print(x)
%history

(note that this includes the %history command itself as part of the history).

history). %history also takes several optional arguments which you can read in the documentation. For example, it is possible to print just the last N lines of history.

In addition to @rburing's answer, I would add a couple more things:

First there is a %%file "cell magic" that is very convenient if you just want to save some input to a file: https://ipython.readthedocs.io/en/stable/interactive/magics.html#cellmagic-writefile (it's documented as "writefile" but there is an alias of it shortened to just "file"). Put this at the top of an existing input like:

sage: %%file foo.sage
....: <the existing code>

When you run this, instead of executing the code it will just save it to a file. You can prepend this to an existing code cell too. I do this sometimes if I want to save small snippets of code out to a file. It's not always the most convenient though.

Another useful "magic" (Sage's interface is built on IPython so all the IPython magics listed on the above-linked page are relevant) is %history. If you just run %history it will print out all the recently-entered code without the prompts. E.g.:

sage: 1 + 1
2
sage: for x in range(5):
....:     print(x)
....:
0
1
2
3
4
sage: %history
1 + 1
for x in range(5):
    print(x)
%history

(note that this includes the %history command itself as part of the history). %history also takes several optional arguments which you can read in the documentation. For example, it is possible to print just the last N lines of history.

Finally, while there is absolutely no shame in using plain ol' notepad (I use it all the time as a quick one-off clipboard for things when I'm working on Windows) a lot of these problems could be avoided by getting used to an editor intended for programming. On Windows, Notepad++ is quite venerable. The Atom editor is also popular on Windows, I hear, though I've never used it personally. I know some people have also used PyCharm for Sage.

Although most editors won't, out of the box, know what to do with the sage: and ...: prompts when copy/pasting from a Sage console, the thing about programming-specific editors is that most of them are programmable themselves to some extent, and can be taught things like how to automatically strip that off. Or even if you do it manually like using a find/replace, most code editors will have modes for different programming languages and will recognize Python and do things like automatically add indentation where appropriate and help you out with that. When I'm using an editor for programming (I mostly use vim personally, but with almost any of them) I almost never manually type indentation when writing Python code. At most usually it's necessary to press the <tab> key to add indentation when the editor can't guess that I want it, but usually it can (e.g. after writing a for x in y: statement).