First time here? Check out the FAQ!

Ask Your Question
1

How to search for earlier commands SageMath terminal?

asked 5 years ago

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!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
4

answered 5 years ago

Sébastien gravatar image

updated 5 years ago

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')
Preview: (hide)
link

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 ( 5 years ago )

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

Sébastien gravatar imageSébastien ( 5 years ago )

Merci bien!

mcmug gravatar imagemcmug ( 5 years ago )

De rien MC:)

Sébastien gravatar imageSébastien ( 5 years ago )
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 ( 5 years ago )

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: 5 years ago

Seen: 487 times

Last updated: Mar 20 '20