Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab-like command history retrieval in unix command line

Tags:

unix

matlab

In Matlab, there is a very nice feature that I like. Suppose I typed the command very-long-command and then a few several commands afterwards. Then later if I need the long command again, I just type very and press the up arrow key, my long command appears. It finds the last command that starts with very. I couldn't do the same in unix command line, when I try to do it, it disregards whatever I typed, and goes back to the last commands in chronological order. Is there a way to do it?

like image 682
AgCl Avatar asked Aug 20 '10 22:08

AgCl


People also ask

Where is MATLAB Command History stored?

MATLAB saves statements that run in the Command Window to the history file History. xml .

How do I recall a previous command in Unix?

Press CTRL+P to switch to the last command, and then press CTRL+O to execute it. This will do the wonder. No configuration needed! You can use CTRL+O as many times as you want to keep re-executing the last commands.

How do I search MATLAB history?

You can search for text in the Command History Window. You can search for text either at the beginning of a command, or anywhere within a command. In the Command History window, type in the Search field. To display the Search field if it is not visible, click the action button , and then select Find.

How do I save MATLAB Command History?

To avoid that problem, select File -> Preferences -> Command History. Set the Saving option to Save after n commands and click OK. For example, set n to 1 . MATLAB then saves the history after every line you execute.


2 Answers

In bash this functionality is provided by the commands history-search-forward and history-search-backward, which by default are not bound to any keys (see here). If you run

bind '"\e[A":history-search-backward'
bind '"\e[B":history-search-forward'

it will make up-arrow and down-arrow search backward and forward through the history for the string of characters between the start of the current line and the point. See also this related Stack Overflow question.

like image 154
SCFrench Avatar answered Sep 28 '22 04:09

SCFrench


In bash, hitting ctrl-r will let you do a history search:

$ echo 'something very long'
something very long
$ # blah
$ # many commands later...
(reverse-i-search)`ec': echo 'something very long'

In the above snippet, I hit ctrl-r on the next line after # many commands later..., and then typed ec which brought me back to the echo command. At that point hitting Enter will execute the command.

like image 33
Mark Rushakoff Avatar answered Sep 28 '22 04:09

Mark Rushakoff