:

## Manipulating History in the Maple Command Line Interface

Maple 2018

Last week, in the first of a series of three articles, I demonstrated the new color syntax highlighting in the command line (TTY) interface of Maple 2018.1. This week, we'll look at a new facility for manipulating the command line history, the history meta-commands.

For the series of screen shots in this article, assume that the `.maple_history` file in your home directory initially contains the following:

```    p1 := plot(sin,color="DeepPink"):
p2 := plot(cos,color="DodgerBlue"):
plots[display](p1,p2);```

Now we'll start `cmaple` and execute a series of commands:

```    |\^/|     Maple 2018.1 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2018
<____ ____>  Waterloo Maple Inc.
|       Type ? for help.
> y := 1/(x^4+1);
1
y := ------
4
x  + 1

> int(y,x);
1/2           1/2             1/2           1/2
1/4 2    arctan(x 2    + 1) + 1/4 2    arctan(x 2    - 1)

2      1/2
1/2    x  + x 2    + 1
+ 1/8 2    ln(---------------)
2      1/2
x  - x 2    + 1

> diff(%,x);
1                       1              1/2
--------------------- + --------------------- + 2
1/2     2               1/2     2
2 ((x 2    + 1)  + 1)   2 ((x 2    - 1)  + 1)

/         1/2        2      1/2              1/2 \
|  2 x + 2         (x  + x 2    + 1) (2 x - 2   )|
|--------------- - ------------------------------|
| 2      1/2               2      1/2     2      |
\x  - x 2    + 1         (x  - x 2    + 1)       /

2      1/2        /      2      1/2
(x  - x 2    + 1)  /  (8 (x  + x 2    + 1))
/

> simplify(%);
memory used=5.2MB, alloc=41.3MB, time=0.11
4
x  + 1
-------------------------------------
2      1/2     2     1/2    2     2
(x  + x 2    + 1)  (x 2    - x  - 1)

> normal(%,expanded);
1
------
4
x  + 1
```

There are two parts to the history:

1. Session history consists of the commands you've entered in the current session.
2. Command history consists of all commands from previous sessions, together with the session history (up to a maxium specified by `interface(historysize)`, which is 1000 lines by default).

History meta-commands all begin with two exclamation marks at the beginning of the line. The `!!=` meta-command lists the session history:

```> !!=
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

Notice that each displayed line of the history is preceeded by `<<` to differentiate it from Maple input and other Maple output. Each meta-command also has a long form. The long form for `!!=` is `!!list`:

```> !!list
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

Most meta-commands can take an argument specifying the amount of history to be affected. For example, a numeric argument N refers to the last N lines of the command history. Here, `!!=8` includes the 3 lines from the previous history, as well as the 5 lines of session history:

```> !!=8
<< p1 := plot(sin,color="DeepPink"):
<< p2 := plot(cos,color="DodgerBlue"):
<< plots[display](p1,p2);
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

An argument beginning with a forward slash ("`/`") refers to all history lines beginning with the most recent one that contains the text entered after the slash (if the text to be searched for does not begin with a space or a digit, the slash can be omitted):

```> !!=/diff
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

We'll quit Maple and start a new session to illustrate another aspect of the history meta-commands.

```> quit
memory used=6.4MB, alloc=41.3MB, time=0.14
```

In the new session, the meta-command `!!=/x^4` will list everyting from the previous session starting from the most recent matching line:

```    |\^/|     Maple 2018.1 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2018
<____ ____>  Waterloo Maple Inc.
|       Type ? for help.
> !!=/x^4
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

If a `!!=` meta-command with a numeric or search argument is executed as the first command in a fresh session, not only is the specified amount of command history listed, but that part of the history is then considered to be session history. Executing just `!!=` with no argument shows that this is the case:

```> !!=
<< y := 1/(x^4+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
```

The `!!!` meta-command (long form `!!play`) re-executes the entire session history:

```> !!!
>> y := 1/(x^4+1);
1
y := ------
4
x  + 1

>> int(y,x);
1/2           1/2             1/2           1/2
1/4 2    arctan(x 2    + 1) + 1/4 2    arctan(x 2    - 1)

2      1/2
1/2    x  + x 2    + 1
+ 1/8 2    ln(---------------)
2      1/2
x  - x 2    + 1

>> diff(%,x);
1                       1              1/2
--------------------- + --------------------- + 2
1/2     2               1/2     2
2 ((x 2    + 1)  + 1)   2 ((x 2    - 1)  + 1)

/         1/2        2      1/2              1/2 \
|  2 x + 2         (x  + x 2    + 1) (2 x - 2   )|
|--------------- - ------------------------------|
| 2      1/2               2      1/2     2      |
\x  - x 2    + 1         (x  - x 2    + 1)       /

2      1/2        /      2      1/2
(x  - x 2    + 1)  /  (8 (x  + x 2    + 1))
/

>> simplify(%);
memory used=5.2MB, alloc=41.3MB, time=0.10
4
x  + 1
-------------------------------------
2      1/2     2     1/2    2     2
(x  + x 2    + 1)  (x 2    - x  - 1)

>> normal(%,expanded);
1
------
4
x  + 1
```

Notice that each command is displayed with a `>>` prompt. This prompt is used whenever a command in the history is being played back.

History commands can be played back one step at a time using the `!!.` (or `!!step`) meta-command. Each command is displayed with the `>>` prompt, and you are given the opportunity to edit it before pressing Enter to execute it. Here, we've changed the exponent of x from 4 to 3 before pressing Enter, and then pressed Enter four more times to re-execute the remaining commands:

```> !!.
>> y := 1/(x^3+1);
1
y := ------
3
x  + 1

>> int(y,x);
1/2
2                 1/2        (2 x - 1) 3
1/3 ln(x + 1) - 1/6 ln(x  - x + 1) + 1/3 3    arctan(--------------)
3

>> diff(%,x);
1          2 x - 1               2
--------- - -------------- + ------------------
3 (x + 1)       2              /         2    \
6 (x  - x + 1)     |(2 x - 1)     |
3 |---------- + 1|
\    3         /

>> simplify(%);
1
--------------------
2
(x + 1) (x  - x + 1)

>> normal(%,expanded);
1
------
3
x  + 1
```

When editing a command prefixed with the `>>` prompt, the command is edited in-place in the history. In other words, the history is permanently modified. History meta-commands are not played back (or infinite loops could result), and lines containing only comments are displayed but not offered for editing.

Still in the same session, let's use `!!.` with a search pattern to generate and display the plots from the older history, pressing Enter after each command:

```> !!./p1 :=
>> p1 := plot(sin,color="DeepPink"):
>> p2 := plot(cos,color="DodgerBlue"):
>> plots[display](p1,p2);

|
-*\    /*-*                      1-+*     *-*\                     /*-
\   //  \\                    // |\\   /   \                     /
\  /    \\                   /  | \  //    \                   /
\ /      *                  *   |  * *     \\                  /
**      ||                 |   |  |*       *                 *
*|       |                |    |   *       ||               ||
||       ||               |    |  |*        |               |
|||        |              |     |  | |       |               |
| ||       |              |     | || |        |             ||
|  |       ||            || 0.5 | |  |        |             |
|   |        |            |      | |   |       |             |
|   ||       |            |      |||   |        |           ||
|    |       ||           |      ||    |        |           |
|     |        |          |       ||     |       |           |
|     ||       |          |       ||     |        |         ||
|      |       ||         |       |      |        |         |
||      |        |        |        |       |       |         |
|       ||       |        |        |       |       ||       ||
+--------+-------++-------+-------++-------+--------+-------+--------+
-6      |  -4    |    -2|       0||        |2      |   4   |      6||
||       |      |        ||        |       ||     ||       |
|       |      |       | |        |        |     |        |
|        |    ||       | |        ||       |     |       ||
|        |    |        | |         |       ||    |       |
|       |    |       || |         |        |   |        |
|        |  ||       |  |         ||       |   |        |
|        |  |      -0.5 |          |       ||  |       |
|       |  |       ||  |          |        | |        |
|        |||       |   |          ||       | |       ||
||       ||        |   |           |       |*|       |
|       **       |    |           |        *        |
|*      |*       |    |            |      **|      *
\     /*\\     *     |            *\     / *      /
\\    /  \    //     |             \    // \\    /
\\  //   \   /      |             \\   /   \\  //
*-*/    \*-*    -1 |              \*-*     *-*/
|

>> !! y := 1/(x^3+1);
> _
```

On the last line above, we typed `!!` followed by a space (long form `!!stop`) to tell Maple to stop playing back commands and give a fresh prompt. If we now issue `!!=`, we see that the session history still contains only the five commands it contained originally. The played back commands were not appended to the history:

```> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);```

Issuing a new command at the regular `>` prompt adds it to the end of the session history:

```> sin(Pi/2) + 1;
2

> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
<< sin(Pi/2) + 1;```

The `!!-` (or `!!drop`) meta-command deletes one or more commands from the history:

```> !!-
> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
<< simplify(%);
<< normal(%,expanded);
> !!-/simpl
> !!=
<< y := 1/(x^3+1);
<< int(y,x);
<< diff(%,x);
```

In addition to the meta-commands described above for manipulating the history within a session, there are three meta-commands to copy history to and from files:

• `!!>filename` (or `!!save filename`) writes the session history to the specified file.
• `!!<filename` (or `!!read filename`) replaces the session history with the contents of the specified file.
• `!!+filename` (or `!!append filename`) appends the contents of the specified file to the session history.

Used together, these meta-commands let you save important sessions for later examination or reuse without relying on the `.maple_history` file, or let you make large changes to the current session history using your favorite text editor.

The `!!?` (or `!!help`) meta-command displays a short summary of all the meta-commands:

```> !!?

The sequence "!!" at the beginning of a line introduces a history meta-command.
Each has a short or long form (shown below in parentheses). The long form name
may be used instead of the single character appearing after "!!".

!!=specifier    - list session history or specified lines (list)
!!!specifier    - play back session history or specified lines (play)
!!.specifier    - step through session history or specified lines (step)
!!-specifier    - drop one or specified lines from history (drop)
!!+filename     - append file to session history (append)
!!>filename     - save session history to file (save)
!!?             - help for history meta-commands (help)
!!              - stop the single-step playback in progress (stop)

The 'specifier' is optional. If present, it can be an integer, N, referring to
the N most recent command lines, or a string beginning with a "/" character,
referring to the lines from the most recent one containing that string. The "/"
may be omitted if the search string does not begin with a space or digit.```

Detailed information can be found in the `?edit_history` help page. ﻿