Debian Codegraph @

docs/features/history.md

raw symbols SCIP
1 # History
2
3 ## For Developers
4
5 Previously, `cmd2` relied on the GNU Readline library for command history. As of version 4.0.0,
6 `cmd2` has migrated to [prompt-toolkit](https://github.com/prompt-toolkit/python-prompt-toolkit) for
7 all input and history handling.
8
9 [cmd2.Cmd][] uses `prompt-toolkit` to provide familiar command-line history capabilities while also
10 maintaining its own data structures for the history of all commands entered by the user. When the
11 class is initialized, it creates an instance of the [cmd2.history.History][] class (which is a
12 subclass of `list`) as `cmd2.Cmd.history`.
13
14 Each time a command is executed (this gets complex, see
15 [Command Processing Loop](./hooks.md#command-processing-loop) for exactly when) the parsed
16 [cmd2.Statement][] is appended to `cmd2.Cmd.history`.
17
18 `cmd2` adds the option of making this history persistent via optional arguments to
19 [cmd2.Cmd.\_\_init\_\_][]. If you pass a filename in the `persistent_history_file` argument, the
20 contents of `cmd2.Cmd.history` will be written as compressed JSON to that history file. We chose
21 this format instead of plain text to preserve the complete `cmd2.Statement` object for each command.
22
23 !!! note
24
25 `prompt-toolkit` saves everything you type, whether it is a valid command or not. `cmd2` only saves input to internal history if the command parses successfully and is a valid command. This design choice was intentional, because the contents of history can be saved to a file as a script, or can be re-run. Not saving invalid input reduces unintentional errors when doing so.
26
27 However, this design choice causes an inconsistency between the `prompt-toolkit` history and the `cmd2` history when you enter an invalid command: it is saved to the `prompt-toolkit` history, but not to the `cmd2` history.
28
29 The `cmd2.Cmd.history` attribute, the `cmd2.history.History` class, and the
30 [cmd2.history.HistoryItem][] class are all part of the public API for `cmd2.Cmd`. You could use
31 these classes to implement your own `history` command (see below for documentation on how the
32 built-in `history` command works).
33
34 ## For Users
35
36 You can use the :arrow_up: up and :arrow_down: down arrow keys to move through the history of
37 previously entered commands.
38
39 You can press `Control-p` to move to the previously entered command, and `Control-n` to move to the
40 next command. You can also search through the command history using `Control-r`.
41
42 By default, `prompt-toolkit` provides Emacs-style key bindings which will be familiar to users of
43 the GNU Readline library. You can refer to the
44 [readline cheat sheet](http://readline.kablamo.org/emacs.html) or you can dig into the
45 [Prompt Toolkit User Manual](https://python-prompt-toolkit.readthedocs.io/en/stable/pages/advanced_topics/key_bindings.html)
46 for all the details, including instructions for customizing the key bindings.
47
48 `cmd2` makes a third type of history access available with the `history` command. Each time the user
49 enters a command, `cmd2` saves the input. The `history` command lets you do interesting things with
50 that saved input. The examples to follow all assume that you have entered the following commands:
51
52 (Cmd) alias create one !echo one
53 Alias 'one' created
54 (Cmd) alias create two !echo two
55 Alias 'two' created
56 (Cmd) alias create three !echo three
57 Alias 'three' created
58 (Cmd) alias create four !echo four
59 Alias 'four' created
60
61 In its simplest form, the `history` command displays previously entered commands. With no additional
62 arguments, it displays all previously entered commands:
63
64 (Cmd) history
65 1 alias create one !echo one
66 2 alias create two !echo two
67 3 alias create three !echo three
68 4 alias create four !echo four
69
70 If you give a positive integer as an argument, then it only displays the specified command:
71
72 (Cmd) history 4
73 4 alias create four !echo four
74
75 If you give a negative integer _N_ as an argument, then it displays the _Nth_ last command. For
76 example, if you give `-1` it will display the last command you entered. If you give `-2` it will
77 display the next to last command you entered, and so forth:
78
79 (Cmd) history -2
80 3 alias create three !echo three
81
82 You can use a similar mechanism to display a range of commands. Simply give two command numbers
83 separated by `..` or `:`, and you will see all commands between, and including, those two numbers:
84
85 (Cmd) history 1:3
86 1 alias create one !echo one
87 2 alias create two !echo two
88 3 alias create three !echo three
89
90 If you omit the first number, it will start at the beginning. If you omit the last number, it will
91 continue to the end:
92
93 (Cmd) history :2
94 1 alias create one !echo one
95 2 alias create two !echo two
96 (Cmd) history 2:
97 2 alias create two !echo two
98 3 alias create three !echo three
99 4 alias create four !echo four
100
101 If you want to display the last three commands entered:
102
103 (Cmd) history -- -3:
104 2 alias create two !echo two
105 3 alias create three !echo three
106 4 alias create four !echo four
107
108 Notice the double dashes. These are required because the history command uses `argparse` to parse
109 the command line arguments. As described in the
110 [argparse documentation](https://docs.python.org/3/library/argparse.html) , `-3:` is an option, not
111 an argument:
112
113 > If you have positional arguments that must begin with - and don't look like negative numbers, you
114 > can insert the pseudo-argument '--' which tells parse[args]{#args}() that everything after that is
115 > a positional argument:
116
117 There is no zeroth command, so don't ask for it. If you are a Python programmer, you've probably
118 noticed this looks a lot like the slice syntax for lists and arrays. It is, with the exception that
119 the first history command is 1, where the first element in a Python array is 0.
120
121 Besides selecting previous commands by number, you can also search for them. You can use a simple
122 string search:
123
124 (Cmd) history two
125 2 alias create two !echo two
126
127 Or a regular expression search by enclosing your regex in slashes:
128
129 (Cmd) history '/te\ +th/'
130 3 alias create three !echo three
131
132 If your regular expression contains any characters that `argparse` finds interesting, like dash or
133 plus, you also need to enclose your regular expression in quotation marks.
134
135 This all sounds great, but doesn't it seem like a bit of overkill to have all these ways to select
136 commands if all we can do is display them? Turns out, displaying history commands is just the
137 beginning. The history command can perform many other actions:
138
139 - running previously entered commands
140 - saving previously entered commands to a text file
141 - opening previously entered commands in your favorite text editor
142 - running previously entered commands, saving the commands and their output to a text file
143 - clearing the history of entered commands
144
145 Each of these actions is invoked using a command line option. The `-r` or `--run` option runs one or
146 more previously entered commands. To run command number 1:
147
148 (Cmd) history --run 1
149
150 To rerun the last two commands (there's that double dash again to make argparse stop looking for
151 options):
152
153 (Cmd) history -r -- -2:
154
155 Say you want to re-run some previously entered commands, but you would really like to make a few
156 changes to them before doing so. When you use the `-e` or `--edit` option, `history` will write the
157 selected commands out to a text file, and open that file with a text editor. You make whatever
158 changes, additions, or deletions, you want. When you leave the text editor, all the commands in the
159 file are executed. To edit and then re-run commands 2-4 you would:
160
161 (Cmd) history --edit 2:4
162
163 If you want to save the commands to a text file, but not edit and re-run them, use the `-o` or
164 `--output-file` option. This is a great way to create [Scripts](./scripting.md), which can be
165 executed using the `run_script` command. To save the first 5 commands entered in this session to a
166 text file:
167
168 (Cmd) history :5 -o history.txt
169
170 The last action the history command can perform is to clear the command history using `-c` or
171 `--clear`:
172
173 (Cmd) history -c
174
175 In addition to these five actions, the `history` command also has some options to control how the
176 output is formatted. With no arguments, the `history` command displays the command number before
177 each command. This is great when displaying history to the screen because it gives you an easy
178 reference to identify previously entered commands. However, when creating a script, the command
179 numbers would prevent the script from loading properly. The `-s` or `--script` option instructs the
180 `history` command to suppress the line numbers. This option is automatically set by the
181 `--output-file` and `--edit` options. If you want to output the history commands with line numbers
182 to a file, you can do it with output redirection:
183
184 (Cmd) history 1:4 > history.txt
185
186 You might use `-s` or `--script` on its own if you want to display history commands to the screen
187 without line numbers, so you can copy them to the clipboard:
188
189 (Cmd) history -s 1:3
190
191 `cmd2` supports both aliases and macros, which allow you to substitute a short, more convenient
192 input string with a longer replacement string. Say we create an alias like this, and then use it:
193
194 (Cmd) alias create ls shell ls -aF
195 Alias 'ls' created
196 (Cmd) ls -d h*
197 history.txt htmlcov/
198
199 By default, the `history` command shows exactly what we typed:
200
201 (Cmd) history
202 1 alias create ls shell ls -aF
203 2 ls -d h*
204
205 There are two ways to modify the display so you can see what aliases and macros were expanded to.
206 The first is to use `-x` or `--expanded`. These options show the expanded command instead of the
207 entered command:
208
209 (Cmd) history -x
210 1 alias create ls shell ls -aF
211 2 shell ls -aF -d h*
212
213 If you want to see both the entered command and the expanded command, use the `-v` or `--verbose`
214 option:
215
216 (Cmd) history -v
217 1 alias create ls shell ls -aF
218 2 ls -d h*
219 2x shell ls -aF -d h*
220
221 If the entered command had no expansion, it is displayed as usual. However, if there is some change
222 as the result of expanding macros and aliases, then the entered command is displayed with the
223 number, and the expanded command is displayed with the number followed by an `x`.