Ask Your Question

How to search for earlier commands SageMath terminal?

asked 2020-03-20 06:48:58 +0200

mcmug gravatar image


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

answered 2020-03-20 08:22:21 +0200

Sébastien gravatar image

updated 2020-03-20 13:50:01 +0200

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)
sage: identity_matrix(4)
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
sage: search_history('factor')
edit flag offensive delete link more


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 13:33:10 +0200 )edit

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

Sébastien gravatar imageSébastien ( 2020-03-20 13:59:44 +0200 )edit

Merci bien!

mcmug gravatar imagemcmug ( 2020-03-20 14:00:46 +0200 )edit

De rien MC:)

Sébastien gravatar imageSébastien ( 2020-03-20 14:03:20 +0200 )edit

Another way to search: ctrl-R and ctrl-S. This is an IPython feature:

John Palmieri gravatar imageJohn Palmieri ( 2020-03-20 17:35:08 +0200 )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


Asked: 2020-03-20 06:48:58 +0200

Seen: 76 times

Last updated: Mar 20 '20