Ask Your Question
0

How to search for earlier commands SageMath terminal?

asked 2020-03-20 00:48:58 -0500

mcmug gravatar image

Hello,

I'd like to know how I can search by keywords for commands that I entered earlier in the SageMath terminal.

Thanks in advance!

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
4

answered 2020-03-20 02:22:21 -0500

Sébastien gravatar image

updated 2020-03-20 07:50:01 -0500

After writing some prefix, typing the UP-ARROW

 sage: prefix<UP-ARROW>

Allows you to search the previous entered command starting with prefix.

EDIT: To search not only prefixes, but also any occurrences of a keyword, you may show the complete history of inputs by doing:

sage: %history

or you may use the variable In which contains the list of the inputs (the variable Out contains the outputs) doing as follows:

sage: def search_history(keyword):
....:     L = ["In[{}]={}".format(i,cmd) for (i,cmd) in enumerate(In) if keyword in cmd]
....:     print('\n'.join(L))

and then:

sage: factor(100)
2^2 * 5^2
sage: print(43)
43
sage: identity_matrix(4)
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
sage: search_history('factor')
In[2]=factor(Integer(100))
In[5]=search_history('factor')
edit flag offensive delete link more

Comments

Coucou Sébastien, merci pour ta réponse. It is possible to search for any keyword, not just the prefix, though?

mcmug gravatar imagemcmug ( 2020-03-20 07:33:10 -0500 )edit

I edited my answer adding a way to search for any keyword in the history.

Sébastien gravatar imageSébastien ( 2020-03-20 07:59:44 -0500 )edit

Merci bien!

mcmug gravatar imagemcmug ( 2020-03-20 08:00:46 -0500 )edit

De rien MC:)

Sébastien gravatar imageSébastien ( 2020-03-20 08:03:20 -0500 )edit
2

Another way to search: ctrl-R and ctrl-S. This is an IPython feature: https://ipython.readthedocs.io/en/6.5...

John Palmieri gravatar imageJohn Palmieri ( 2020-03-20 11:35:08 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-03-20 00:48:58 -0500

Seen: 49 times

Last updated: Mar 20