2670
|
1 @c Copyright (C) 1996, 1997 John W. Eaton |
2653
|
2 @c This is part of the Octave manual. |
|
3 @c For copying conditions, see the file gpl.texi. |
|
4 |
2670
|
5 @node Getting Started, Data Types, Introduction, Top |
|
6 @chapter Getting Started |
|
7 |
|
8 This chapter explains some of Octave's basic features, including how to |
|
9 start an Octave session, get help at the command prompt, edit the |
|
10 command line, and write Octave programs that can be executed as commands |
|
11 from your shell. |
2653
|
12 |
|
13 @menu |
2670
|
14 * Invoking Octave:: |
|
15 * Quitting Octave:: |
|
16 * Getting Help:: |
|
17 * Command Line Editing:: |
2653
|
18 * Errors:: |
2670
|
19 * Executable Octave Programs:: |
|
20 * Comments:: |
|
21 @end menu |
|
22 |
|
23 @node Invoking Octave, Quitting Octave, Getting Started, Getting Started |
|
24 @section Invoking Octave |
|
25 |
|
26 Normally, Octave is used interactively by running the program |
|
27 @samp{octave} without any arguments. Once started, Octave reads |
|
28 commands from the terminal until you tell it to exit. |
|
29 |
|
30 You can also specify the name of a file on the command line, and Octave |
|
31 will read and execute the commands from the named file and then exit |
|
32 when it is finished. |
|
33 |
|
34 You can further control how Octave starts by using the command-line |
|
35 options described in the next section, and Octave itself can remind you |
|
36 of the options available. Type @kbd{octave --help} to display all |
|
37 available options and briefly describe their use (@kbd{octave -h} is a |
|
38 shorter equivalent). |
|
39 |
|
40 @menu |
|
41 * Command Line Options:: |
|
42 * Startup Files:: |
2653
|
43 @end menu |
|
44 |
2670
|
45 @node Command Line Options, Startup Files, Invoking Octave, Invoking Octave |
|
46 @subsection Command Line Options |
|
47 @cindex Octave command options |
|
48 @cindex command options |
|
49 @cindex options, Octave command |
|
50 |
|
51 Here is a complete list of all the command line options that Octave |
|
52 accepts. |
|
53 |
|
54 @table @code |
|
55 @item --debug |
|
56 @itemx -d |
|
57 @cindex @code{--debug} |
|
58 @cindex @code{-d} |
|
59 Enter parser debugging mode. Using this option will cause Octave's |
|
60 parser to print a lot of information about the commands it reads, and is |
|
61 probably only useful if you are actually trying to debug the parser. |
|
62 |
|
63 @item --echo-commands |
|
64 @itemx -x |
|
65 @cindex @code{--echo-commands} |
|
66 @cindex @code{-x} |
|
67 Echo commands as they are executed. |
2653
|
68 |
2670
|
69 @item --exec-path @var{path} |
|
70 @cindex @code{--exec-path @var{path}} |
|
71 Specify the path to search for programs to run. The value of @var{path} |
|
72 specified on the command line will override any value of |
2689
|
73 @code{OCTAVE_EXEC_PATH} found in the environment, but not any commands |
|
74 in the system or user startup files that set @code{EXEC_PATH}. |
2653
|
75 |
2670
|
76 @item --help |
|
77 @itemx -h |
|
78 @itemx -? |
|
79 @cindex @code{--help} |
|
80 @cindex @code{-h} |
|
81 @cindex @code{-?} |
|
82 Print short help message and exit. |
|
83 |
|
84 @item --info-file @var{filename} |
|
85 @cindex @code{--info-file @var{filename}} |
|
86 Specify the name of the info file to use. The value of @var{filename} |
|
87 specified on the command line will override any value of |
2689
|
88 @code{OCTAVE_INFO_FILE} found in the environment, but not any commands |
|
89 in the system or user startup files that set @samp{INFO_FILE}. |
2653
|
90 |
2670
|
91 @item --info-program @var{program} |
|
92 @cindex @code{--info-program @var{program}} |
|
93 Specify the name of the info program to use. The value of @var{program} |
|
94 specified on the command line will override any value of |
2689
|
95 @code{OCTAVE_INFO_PROGRAM} found in the environment, but not any |
|
96 commands in the system or user startup files that set |
|
97 @code{INFO_PROGRAM}. |
2653
|
98 |
2670
|
99 @item --interactive |
|
100 @itemx -i |
|
101 @cindex @code{--interactive} |
|
102 @cindex @code{-i} |
2689
|
103 Force interactive behavior. This can be useful for running Octave via a |
|
104 remote shell command or inside an Emacs shell buffer. For another way |
|
105 to run Octave within Emacs, see @ref{Emacs}. |
2653
|
106 |
2670
|
107 @item --no-init-file |
|
108 @cindex @code{--no-init-file} |
|
109 Don't read the @file{~/.octaverc} or @file{.octaverc} files. |
|
110 |
|
111 @item --no-line-editing |
|
112 @cindex @code{--no-line-editing} |
|
113 Disable command-line editing and history. |
|
114 |
|
115 @item --no-site-file |
|
116 @cindex @code{--no-site-file} |
|
117 Don't read the site-wide @file{octaverc} file. |
2653
|
118 |
2670
|
119 @item --norc |
|
120 @itemx -f |
|
121 @cindex @code{--norc} |
|
122 @cindex @code{-f} |
|
123 Don't read any of the system or user initialization files at startup. |
|
124 This is equivalent to using both of the options @code{--no-init-file} |
|
125 and @code{--no-site-file}. |
2653
|
126 |
2670
|
127 @item --path @var{path} |
|
128 @itemx -p @var{path} |
|
129 @cindex @code{--path @var{path}} |
|
130 @cindex @code{-p @var{path}} |
|
131 Specify the path to search for function files. The value of @var{path} |
|
132 specified on the command line will override any value of |
2689
|
133 @code{OCTAVE_PATH} found in the environment, but not any commands in the |
|
134 system or user startup files that set @code{LOADPATH}. |
2670
|
135 |
|
136 @item --silent |
|
137 @itemx --quiet |
|
138 @itemx -q |
|
139 @cindex @code{--silent} |
|
140 @cindex @code{--quiet} |
|
141 @cindex @code{-q} |
2689
|
142 Don't print the usual greeting and version message at startup. |
2670
|
143 |
|
144 @item --traditional |
|
145 @itemx --braindead |
|
146 @cindex @code{--traditional} |
|
147 @cindex @code{--braindead} |
|
148 Set initial values for user-preference variables to the following |
|
149 values for compatibility with @sc{Matlab}. |
2653
|
150 |
|
151 @example |
2670
|
152 PS1 = ">> " |
|
153 PS2 = "" |
|
154 beep_on_error = 1 |
|
155 default_save_format = "mat-binary" |
|
156 define_all_return_values = 1 |
|
157 do_fortran_indexing = 1 |
|
158 empty_list_elements_ok = 1 |
|
159 implicit_str_to_num_ok = 1 |
|
160 ok_to_lose_imaginary_part = 1 |
|
161 page_screen_output = 0 |
|
162 prefer_column_vectors = 0 |
|
163 prefer_zero_one_indexing = 1 |
|
164 print_empty_dimensions = 0 |
|
165 treat_neg_dim_as_zero = 1 |
|
166 warn_function_name_clash = 0 |
|
167 whitespace_in_literal_matrix = "traditional" |
|
168 @end example |
|
169 |
|
170 @item --verbose |
|
171 @itemx -V |
|
172 @cindex @code{--verbose} |
|
173 @cindex @code{-V} |
|
174 Turn on verbose output. |
|
175 |
|
176 @item --version |
|
177 @itemx -v |
|
178 @cindex @code{--version} |
|
179 @cindex @code{-v} |
|
180 Print the program version number and exit. |
|
181 |
|
182 @item @var{file} |
|
183 Execute commands from @var{file}. |
|
184 @end table |
|
185 |
|
186 Octave also includes several built-in variables that contain information |
|
187 about the command line, including the number of arguments and all of the |
|
188 options. |
|
189 |
|
190 @defvr {Built-in Variable} argv |
|
191 The command line arguments passed to Octave are available in this |
|
192 variable. For example, if you invoked Octave using the command |
|
193 |
|
194 @example |
|
195 octave --no-line-editing --silent |
2653
|
196 @end example |
|
197 |
|
198 @noindent |
2670
|
199 @code{argv} would be a string vector with the elements |
|
200 @code{--no-line-editing} and @code{--silent}. |
2653
|
201 |
2670
|
202 If you write an executable Octave script, @code{argv} will contain the |
|
203 list of arguments passed to the script. @pxref{Executable Octave Programs}. |
|
204 @end defvr |
2653
|
205 |
2670
|
206 @defvr {Built-in Variable} nargin |
|
207 At the top level, this variable is defined as the number of command line |
|
208 arguments that were passed to Octave. |
|
209 @end defvr |
2653
|
210 |
2670
|
211 @defvr {Built-in Variable} program_invocation_name |
|
212 @defvrx {Built-in Variable} program_name |
|
213 When Octave starts, the value of the built-in variable |
|
214 @code{program_invocation_name} is automatically set to the name that was |
|
215 typed at the shell prompt to run Octave, and the value of |
|
216 @code{program_name} is automatically set to the final component of |
|
217 @code{program_invocation_name}. For example, if you typed |
|
218 @kbd{@value{OCTAVEHOME}/bin/octave} to start Octave, |
|
219 @code{program_invocation_name} would have the value |
|
220 @code{"@value{OCTAVEHOME}/bin/octave"}, and @code{program_name} would |
|
221 have the value @code{"octave"}. |
2653
|
222 |
2670
|
223 If executing a script from the command line (e.g., @code{octave foo.m} |
|
224 or using an executable Octave script, the program name is set to the |
|
225 name of the script. @xref{Executable Octave Programs} for an example of |
|
226 how to create an executable Octave script. |
|
227 @end defvr |
2653
|
228 |
2670
|
229 Here is an example of using these variables to reproduce Octave's |
|
230 command line. |
2653
|
231 |
|
232 @example |
|
233 printf ("%s", program_name); |
|
234 for i = 1:nargin |
|
235 printf (" %s", argv(i,:)); |
|
236 endfor |
|
237 printf ("\n"); |
|
238 @end example |
|
239 |
2670
|
240 @noindent |
|
241 @xref{Index Expressions} for an explanation of how to properly index |
|
242 arrays of strings and substrings in Octave. |
|
243 |
|
244 @node Startup Files, , Command Line Options, Invoking Octave |
|
245 @subsection Startup Files |
|
246 @cindex initialization |
|
247 @cindex startup |
|
248 |
|
249 When Octave starts, it looks for commands to execute from the following |
|
250 files: |
|
251 |
|
252 @cindex startup files |
|
253 |
|
254 @table @code |
|
255 @item OCTAVE_HOME/share/octave/site/m/startup/octaverc |
|
256 Where @code{OCTAVE_HOME} is the directory in which all of Octave is |
|
257 installed (the default is @file{/usr/local}). This file is provided so |
|
258 that changes to the default Octave environment can be made globally for |
|
259 all users at your site for all versions of Octave you have installed. |
|
260 Some care should be taken when making changes to this file, since all |
|
261 users of Octave at your site will be affected. |
|
262 |
|
263 @item OCTAVE_HOME/share/octave/VERSION/m/startup/octaverc |
|
264 Where @code{OCTAVE_HOME} is the directory in which all of Octave is |
|
265 installed (the default is @file{/usr/local}), and @code{VERSION} is the |
|
266 version number of Octave. This file is provided so that changes to the |
|
267 default Octave environment can be made globally for all users for a |
|
268 particular version of Octave. Some care should be taken when making |
|
269 changes to this file, since all users of Octave at your site will be |
|
270 affected. |
|
271 |
|
272 @item ~/.octaverc |
|
273 @cindex @code{~/.octaverc} |
|
274 This file is normally used to make personal changes to the default |
|
275 Octave environment. |
|
276 |
|
277 @item .octaverc |
|
278 @cindex @code{.octaverc} |
|
279 This file can be used to make changes to the default Octave environment |
|
280 for a particular project. Octave searches for this file in the current |
|
281 directory after it reads @file{~/.octaverc}. Any use of the @code{cd} |
|
282 command in the @file{~/.octaverc} file will affect the directory that |
|
283 Octave searches for the file @file{.octaverc}. |
|
284 |
|
285 If you start Octave in your home directory, commands from from the file |
|
286 @file{~/.octaverc} will only be executed once. |
|
287 @end table |
|
288 |
|
289 A message will be displayed as each of the startup files is read if you |
|
290 invoke Octave with the @code{--verbose} option but without the |
|
291 @code{--silent} option. |
|
292 |
|
293 Startup files may contain any valid Octave commands, including function |
|
294 definitions. |
|
295 |
|
296 @node Quitting Octave, Getting Help, Invoking Octave, Getting Started |
|
297 @section Quitting Octave |
|
298 @cindex exiting octave |
|
299 @cindex quitting octave |
|
300 |
|
301 @deftypefn {Built-in Function} {} exit (@var{status}) |
|
302 @deftypefnx {Built-in Function} {} quit (@var{status}) |
|
303 Exit the current Octave session. If the optional integer value |
|
304 @var{status} is supplied, pass that value to the operating system as the |
|
305 Octave's exit status. |
|
306 @end deftypefn |
|
307 |
|
308 @deftypefn {Built-in Function} {} atexit (@var{fcn}) |
2689
|
309 Register function to be called when Octave exits. For example, |
|
310 |
|
311 @example |
|
312 @group |
|
313 function print_flops_at_exit () |
|
314 printf ("\n%s\n", system ("fortune")); |
|
315 fflush (stdout); |
|
316 endfunction |
|
317 atexit ("print_flops_at_exit"); |
|
318 @end group |
|
319 @end example |
|
320 |
|
321 @noindent |
|
322 will print a message when Octave exits. |
2670
|
323 @end deftypefn |
|
324 |
|
325 @node Getting Help, Command Line Editing, Quitting Octave, Getting Started |
|
326 @section Commands for Getting Help |
|
327 @cindex on-line help |
|
328 @cindex help, on-line |
|
329 |
|
330 The entire text of this manual is available from the Octave prompt |
|
331 via the command @kbd{help -i}. In addition, the documentation for |
|
332 individual user-written functions and variables is also available via |
|
333 the @kbd{help} command. This section describes the commands used for |
|
334 reading the manual and the documentation strings for user-supplied |
|
335 functions and variables. @xref{Function Files}, for more information |
|
336 about how to document the functions you write. |
|
337 |
|
338 @deffn {Command} help |
|
339 Octave's @code{help} command can be used to print brief usage-style |
|
340 messages, or to display information directly from an on-line version of |
|
341 the printed manual, using the GNU Info browser. If invoked without any |
|
342 arguments, @code{help} prints a list of all the available operators, |
|
343 functions, and built-in variables. If the first argument is @code{-i}, |
|
344 the @code{help} command searches the index of the on-line version of |
|
345 this manual for the given topics. |
|
346 |
|
347 For example, the command @kbd{help help} prints a short message |
|
348 describing the @code{help} command, and @kbd{help -i help} starts the |
|
349 GNU Info browser at this node in the on-line version of the manual. |
|
350 |
2689
|
351 Once the GNU Info browser is running, help for using it is available |
2670
|
352 using the command @kbd{C-h}. |
|
353 @end deffn |
|
354 |
|
355 The help command can give you information about operators, but not the |
|
356 comma and semicolons that are used as command separators. To get help |
|
357 for those, you must type @kbd{help comma} or @kbd{help semicolon}. |
|
358 |
|
359 @defvr {Built-in Variable} INFO_FILE |
|
360 The variable @code{INFO_FILE} names the location of the Octave info file. |
|
361 The default value is @code{"@value{OCTAVEHOME}/info/octave.info"}. |
|
362 @end defvr |
|
363 |
|
364 @defvr {Built-in Variable} INFO_PROGRAM |
|
365 The variable @code{INFO_PROGRAM} names the info program to run. Its |
|
366 initial value is |
|
367 @code{@value{OCTAVEHOME}/libexec/octave/VERSION/exec/ARCH/info}, but |
|
368 that value can be overridden by the environment variable |
|
369 @code{OCTAVE_INFO_PROGRAM}, or the command line argument |
|
370 @code{--info-program NAME}, or by setting the value of |
|
371 @code{INFO_PROGRAM} in a startup script. |
|
372 @end defvr |
|
373 |
|
374 @defvr {Built-in Variable} suppress_verbose_help_message |
|
375 If the value of @code{suppress_verbose_help_message} is nonzero, Octave |
|
376 will not add additional help information to the end of the output from |
|
377 the @code{help} command and usage messages for built-in commands. |
|
378 @end defvr |
|
379 |
|
380 @node Command Line Editing, Errors, Getting Help, Getting Started |
|
381 @section Command Line Editing |
|
382 @cindex command-line editing |
|
383 @cindex editing the command line |
|
384 |
|
385 Octave uses the GNU readline library to provide an extensive set of |
|
386 command-line editing and history features. Only the most common |
|
387 features are described in this manual. Please see The GNU Readline |
2689
|
388 Library manual for more information. |
2670
|
389 |
|
390 To insert printing characters (letters, digits, symbols, etc.), simply |
|
391 type the character. Octave will insert the character at the cursor and |
|
392 advance the cursor forward. |
|
393 |
|
394 Many of the command-line editing functions operate using control |
|
395 characters. For example, the character @kbd{Control-a} moves the cursor |
|
396 to the beginning of the line. To type @kbd{C-a}, hold down @key{CTRL} |
|
397 and then press @key{a}. In the following sections, control characters |
|
398 such as @kbd{Control-a} are written as @kbd{C-a}. |
|
399 |
|
400 Another set of command-line editing functions use Meta characters. On |
|
401 some terminals, you type @kbd{M-u} by holding down @key{META} and |
|
402 pressing @key{u}. If your terminal does not have a @key{META} key, you |
|
403 can still type Meta charcters using two-character sequences starting |
|
404 with @kbd{ESC}. Thus, to enter @kbd{M-u}, you could type |
|
405 @key{ESC}@key{u}. The @kbd{ESC} character sequences are also allowed on |
|
406 terminals with real Meta keys. In the following sections, Meta |
|
407 characters such as @kbd{Meta-u} are written as @kbd{M-u}. |
|
408 |
|
409 @menu |
|
410 * Cursor Motion:: |
|
411 * Killing and Yanking:: |
|
412 * Commands For Text:: |
|
413 * Commands For Completion:: |
|
414 * Commands For History:: |
|
415 * Customizing the Prompt:: |
|
416 * Diary and Echo Commands:: |
|
417 @end menu |
|
418 |
|
419 @node Cursor Motion, Killing and Yanking, Command Line Editing, Command Line Editing |
|
420 @subsection Cursor Motion |
|
421 |
|
422 The following commands allow you to position the cursor. |
|
423 |
|
424 @table @kbd |
|
425 @item C-b |
|
426 Move back one character. |
|
427 |
|
428 @item C-f |
|
429 Move forward one character. |
|
430 |
|
431 @item DEL |
|
432 Delete the character to the left of the cursor. |
|
433 |
|
434 @item C-d |
|
435 Delete the character underneath the cursor. |
|
436 |
|
437 @item M-f |
|
438 Move forward a word. |
|
439 |
|
440 @item M-b |
|
441 Move backward a word. |
|
442 |
|
443 @item C-a |
|
444 Move to the start of the line. |
|
445 |
|
446 @item C-e |
|
447 Move to the end of the line. |
|
448 |
|
449 @item C-l |
|
450 Clear the screen, reprinting the current line at the top. |
|
451 |
|
452 @item C-_ |
|
453 @itemx C-/ |
|
454 Undo the last thing that you did. You can undo all the way back to an |
|
455 empty line. |
|
456 |
|
457 @item M-r |
|
458 Undo all changes made to this line. This is like typing the `undo' |
|
459 command enough times to get back to the beginning. |
|
460 @end table |
|
461 |
|
462 The above table describes the most basic possible keystrokes that you need |
|
463 in order to do editing of the input line. On most terminals, you can |
|
464 also use the arrow keys in place of @kbd{C-f} and @kbd{C-b} to move |
|
465 forward and backward. |
|
466 |
|
467 Notice how @kbd{C-f} moves forward a character, while @kbd{M-f} moves |
|
468 forward a word. It is a loose convention that control keystrokes |
|
469 operate on characters while meta keystrokes operate on words. |
|
470 |
|
471 There is also a function available so that you can clear the screen from |
|
472 within Octave programs. |
|
473 |
|
474 @cindex clearing the screen |
|
475 |
|
476 @deftypefn {Built-in Function} {} clc () |
|
477 @deftypefnx {Built-in Function} {} home () |
|
478 Clear the terminal screen and move the cursor to the upper left corner. |
|
479 @end deftypefn |
|
480 |
|
481 @node Killing and Yanking, Commands For Text, Cursor Motion, Command Line Editing |
|
482 @subsection Killing and Yanking |
|
483 |
|
484 @dfn{Killing} text means to delete the text from the line, but to save |
|
485 it away for later use, usually by @dfn{yanking} it back into the line. |
|
486 If the description for a command says that it `kills' text, then you can |
|
487 be sure that you can get the text back in a different (or the same) |
|
488 place later. |
|
489 |
|
490 Here is the list of commands for killing text. |
|
491 |
|
492 @table @kbd |
|
493 @item C-k |
|
494 Kill the text from the current cursor position to the end of the line. |
|
495 |
|
496 @item M-d |
|
497 Kill from the cursor to the end of the current word, or if between |
|
498 words, to the end of the next word. |
|
499 |
|
500 @item M-DEL |
|
501 Kill from the cursor to the start of the previous word, or if between |
|
502 words, to the start of the previous word. |
|
503 |
|
504 @item C-w |
|
505 Kill from the cursor to the previous whitespace. This is different than |
|
506 @kbd{M-DEL} because the word boundaries differ. |
|
507 @end table |
|
508 |
|
509 And, here is how to @dfn{yank} the text back into the line. Yanking |
|
510 means to copy the most-recently-killed text from the kill buffer. |
|
511 |
|
512 @table @kbd |
|
513 @item C-y |
|
514 Yank the most recently killed text back into the buffer at the cursor. |
|
515 |
|
516 @item M-y |
|
517 Rotate the kill-ring, and yank the new top. You can only do this if |
|
518 the prior command is @kbd{C-y} or @kbd{M-y}. |
|
519 @end table |
|
520 |
|
521 When you use a kill command, the text is saved in a @dfn{kill-ring}. |
|
522 Any number of consecutive kills save all of the killed text together, so |
|
523 that when you yank it back, you get it in one clean sweep. The kill |
|
524 ring is not line specific; the text that you killed on a previously |
|
525 typed line is available to be yanked back later, when you are typing |
|
526 another line. |
|
527 |
|
528 @node Commands For Text, Commands For Completion, Killing and Yanking, Command Line Editing |
|
529 @subsection Commands For Changing Text |
|
530 |
|
531 The following commands can be used for entering characters that would |
|
532 otherwise have a special meaning (e.g., @kbd{TAB}, @kbd{C-q}, etc.), or |
|
533 for quickly correcting typing mistakes. |
|
534 |
|
535 @table @kbd |
|
536 @item C-q |
|
537 @itemx C-v |
|
538 Add the next character that you type to the line verbatim. This is |
|
539 how to insert things like @kbd{C-q} for example. |
|
540 |
|
541 @item M-TAB |
|
542 Insert a tab character. |
|
543 |
|
544 @item C-t |
|
545 Drag the character before the cursor forward over the character at the |
|
546 cursor, also moving the cursor forward. If the cursor is at the end of |
|
547 the line, then transpose the two characters before it. |
|
548 |
|
549 @item M-t |
|
550 Drag the word behind the cursor past the word in front of the cursor |
|
551 moving the cursor over that word as well. |
|
552 |
|
553 @item M-u |
|
554 Uppercase the characters following the cursor to the end of the current |
|
555 (or following) word, moving the cursor to the end of the word. |
|
556 |
|
557 @item M-l |
|
558 Lowecase the characters following the cursor to the end of the current |
|
559 (or following) word, moving the cursor to the end of the word. |
|
560 |
|
561 @item M-c |
|
562 Uppercase the character following the cursor (or the beginning of the |
|
563 next word if the cursor is between words), moving the cursor to the end |
|
564 of the word. |
|
565 @end table |
|
566 |
|
567 @node Commands For Completion, Commands For History, Commands For Text, Command Line Editing |
|
568 @subsection Letting Readline Type For You |
|
569 @cindex command completion |
|
570 |
2689
|
571 The following commands allow Octave to complete command and variable |
|
572 names for you. |
2670
|
573 |
|
574 @table @kbd |
|
575 @item TAB |
|
576 Attempt to do completion on the text before the cursor. Octave can |
2689
|
577 complete the names of commands and variables. |
2670
|
578 |
|
579 @item M-? |
|
580 List the possible completions of the text before the cursor. |
|
581 @end table |
|
582 |
|
583 @defvr {Built-in Variable} completion_append_char |
|
584 The value of @code{completion_append_char} is used as the character to |
|
585 append to successful command-line completion attempts. The default |
|
586 value is @code{" "} (a single space). |
|
587 @end defvr |
|
588 |
|
589 @deftypefn {Built-in Function} {} completion_matches (@var{hint}) |
|
590 Generate possible completions given @var{hint}. |
|
591 |
|
592 This function is provided for the benefit of programs like Emacs which |
|
593 might be controlling Octave and handling user input. The current |
|
594 command number is not incremented when this function is called. This is |
|
595 a feature, not a bug. |
|
596 @end deftypefn |
|
597 |
|
598 @node Commands For History, Customizing the Prompt, Commands For Completion, Command Line Editing |
|
599 @subsection Commands For Manipulating The History |
|
600 @cindex command history |
|
601 @cindex input history |
|
602 @cindex history of commands |
|
603 |
|
604 Octave normally keeps track of the commands you type so that you can |
|
605 recall previous commands to edit or execute them again. When you exit |
|
606 Octave, the most recent commands you have typed, up to the number |
|
607 specified by the variable @code{history_size}, are saved in a file. |
|
608 When Octave starts, it loads an initial list of commands from the file |
|
609 named by the variable @code{history_file}. |
|
610 |
|
611 Here are the commands for simple browsing and searching the history |
|
612 list. |
|
613 |
|
614 @table @kbd |
|
615 @item LFD |
|
616 @itemx RET |
|
617 Accept the line regardless of where the cursor is. If this line is |
|
618 non-empty, add it to the history list. If this line was a history |
|
619 line, then restore the history line to its original state. |
|
620 |
|
621 @item C-p |
|
622 Move `up' through the history list. |
|
623 |
|
624 @item C-n |
|
625 Move `down' through the history list. |
|
626 |
|
627 @item M-< |
|
628 Move to the first line in the history. |
|
629 |
|
630 @item M-> |
|
631 Move to the end of the input history, i.e., the line you are entering! |
|
632 |
|
633 @item C-r |
|
634 Search backward starting at the current line and moving `up' through |
|
635 the history as necessary. This is an incremental search. |
|
636 |
|
637 @item C-s |
|
638 Search forward starting at the current line and moving `down' through |
|
639 the the history as necessary. |
|
640 @end table |
|
641 |
|
642 On most terminals, you can also use the arrow keys in place of @kbd{C-p} |
|
643 and @kbd{C-n} to move through the history list. |
|
644 |
|
645 In addition to the keyboard commands for moving through the history |
|
646 list, Octave provides three functions for viewing, editing, and |
|
647 re-running chunks of commands from the history list. |
|
648 |
|
649 @deffn {Command} history options |
|
650 If invoked with no arguments, @code{history} displays a list of commands |
|
651 that you have executed. Valid options are: |
|
652 |
|
653 @table @code |
|
654 @item -w file |
|
655 Write the current history to the named file. If the name is omitted, |
|
656 use the default history file (normally @file{~/.octave_hist}). |
|
657 |
|
658 @item -r file |
|
659 Read the named file, replacing the current history list with its |
|
660 contents. If the name is omitted, use the default history file |
|
661 (normally @file{~/.octave_hist}). |
|
662 |
|
663 @item @var{N} |
|
664 Only display the most recent @var{N} lines of history. |
|
665 |
|
666 @item -q |
|
667 Don't number the displayed lines of history. This is useful for cutting |
|
668 and pasting commands if you are using the X Window System. |
|
669 @end table |
|
670 |
|
671 For example, to display the five most recent commands that you have |
|
672 typed without displaying line numbers, use the command |
2689
|
673 @kbd{history -q 5}. |
2670
|
674 @end deffn |
|
675 |
|
676 @deffn {Command} edit_history options |
|
677 If invoked with no arguments, @code{edit_history} allows you to edit the |
|
678 history list using the editor named by the variable @code{EDITOR}. The |
|
679 commands to be edited are first copied to a temporary file. When you |
|
680 exit the editor, Octave executes the commands that remain in the file. |
|
681 It is often more convenient to use @code{edit_history} to define functions |
|
682 rather than attempting to enter them directly on the command line. |
|
683 By default, the block of commands is executed as soon as you exit the |
|
684 editor. To avoid executing any commands, simply delete all the lines |
|
685 from the buffer before exiting the editor. |
|
686 |
|
687 The @code{edit_history} command takes two optional arguments specifying |
|
688 the history numbers of first and last commands to edit. For example, |
|
689 the command |
|
690 |
|
691 @example |
|
692 edit_history 13 |
|
693 @end example |
|
694 |
|
695 @noindent |
|
696 extracts all the commands from the 13th through the last in the history |
|
697 list. The command |
|
698 |
|
699 @example |
|
700 edit_history 13 169 |
|
701 @end example |
|
702 |
|
703 @noindent |
|
704 only extracts commands 13 through 169. Specifying a larger number for |
|
705 the first command than the last command reverses the list of commands |
|
706 before placing them in the buffer to be edited. If both arguments are |
|
707 omitted, the previous command in the history list is used. |
|
708 @end deffn |
|
709 |
|
710 @deffn {Command} run_history |
|
711 Similar to @code{edit_history}, except that the editor is not invoked, |
|
712 and the commands are simply executed as they appear in the history list. |
|
713 @end deffn |
|
714 |
|
715 @defvr {Built-in Variable} EDITOR |
|
716 A string naming the editor to use with the @code{edit_history} command. |
|
717 If the environment variable @code{EDITOR} is set when Octave starts, its |
|
718 value is used as the default. Otherwise, @code{EDITOR} is set to |
|
719 @code{"vi"}. |
|
720 @end defvr |
|
721 |
|
722 @defvr {Built-in Variable} history_file |
|
723 This variable specifies the name of the file used to store command |
|
724 history. The default value is @code{"~/.octave_hist"}, but may be |
|
725 overridden by the environment variable @code{OCTAVE_HISTFILE}. |
|
726 @end defvr |
|
727 |
|
728 @defvr {Built-in Variable} history_size |
|
729 This variable specifies how many entries to store in the history file. |
|
730 The default value is @code{1024}, but may be overridden by the |
|
731 environment variable @code{OCTAVE_HISTSIZE}. |
|
732 @end defvr |
|
733 |
|
734 @defvr {Built-in Variable} saving_history |
|
735 If the value of @code{saving_history} is @code{"true"}, command entered |
|
736 on the command line are saved in the file specified by the variable |
|
737 @code{history_file}. |
|
738 @end defvr |
|
739 |
|
740 @node Customizing the Prompt, Diary and Echo Commands, Commands For History, Command Line Editing |
|
741 @subsection Customizing the Prompt |
|
742 @cindex prompt customization |
|
743 @cindex customizing the prompt |
|
744 |
|
745 The following variables are available for customizing the appearance of |
|
746 the command-line prompts. Octave allows the prompt to be customized by |
|
747 inserting a number of backslash-escaped special characters that are |
|
748 decoded as follows: |
|
749 |
|
750 @table @samp |
|
751 @item \t |
|
752 The time. |
|
753 |
|
754 @item \d |
|
755 The date. |
|
756 |
|
757 @item \n |
|
758 Begins a new line by printing the equivalent of a carriage return |
|
759 followed by a line feed. |
|
760 |
|
761 @item \s |
|
762 The name of the program (usually just @code{octave}). |
|
763 |
|
764 @item \w |
|
765 The current working directory. |
|
766 |
|
767 @item \W |
|
768 The basename of the current working directory. |
|
769 |
|
770 @item \u |
|
771 The username of the current user. |
|
772 |
|
773 @item \h |
|
774 The hostname. |
|
775 |
|
776 @item \H |
|
777 The hostname, up to the first `.'. |
|
778 |
|
779 @item \# |
|
780 The command number of this command, counting from when Octave starts. |
|
781 |
|
782 @item \! |
|
783 The history number of this command. This differs from @samp{\#} by the |
|
784 number of commands in the history list when Octave starts. |
|
785 |
|
786 @item \$ |
|
787 If the effective UID is 0, a #, otherwise a $. |
|
788 |
|
789 @item \nnn |
|
790 The character whose character code in octal is @samp{nnn}. |
|
791 |
|
792 @item \\ |
|
793 A backslash. |
|
794 @end table |
|
795 |
|
796 @defvr {Built-in Variable} PS1 |
|
797 The primary prompt string. When executing interactively, Octave |
|
798 displays the primary prompt @code{PS1} when it is ready to read a |
|
799 command. |
|
800 |
|
801 The default value of @code{PS1} is @code{"\s:\#> "}. To change it, use a |
|
802 command like |
|
803 |
|
804 @example |
|
805 octave:13> PS1 = "\\u@@\\H> " |
|
806 @end example |
|
807 |
|
808 @noindent |
|
809 which will result in the prompt @samp{boris@@kremvax> } for the user |
|
810 @samp{boris} logged in on the host @samp{kremvax.kgb.su}. Note that two |
|
811 backslashes are required to enter a backslash into a string. |
|
812 @xref{Strings}. |
|
813 @end defvr |
|
814 |
|
815 @defvr {Built-in Variable} PS2 |
|
816 The secondary prompt string, which is printed when Octave is |
|
817 expecting additional input to complete a command. For example, when |
|
818 defining a function over several lines, Octave will print the value of |
|
819 @code{PS1} at the beginning of each line after the first. The default |
|
820 value of @code{PS2} is @code{"> "}. |
|
821 @end defvr |
|
822 |
|
823 @defvr {Built-in Variable} PS4 |
|
824 If Octave is invoked with the @code{--echo-input} option, the value of |
|
825 @code{PS4} is printed before each line of input that is echoed. The |
|
826 default value of @code{PS4} is @code{"+ "}. @xref{Invoking Octave}, for |
|
827 a description of @code{--echo-input}. |
|
828 @end defvr |
|
829 |
|
830 @node Diary and Echo Commands, , Customizing the Prompt, Command Line Editing |
|
831 @subsection Diary and Echo Commands |
|
832 @cindex diary of commands and output |
|
833 @cindex command and ouput logs |
|
834 @cindex logging commands and output |
|
835 @cindex echoing executing commands |
|
836 @cindex command echoing |
|
837 |
|
838 Octave's diary feature allows you to keep a log of all or part of an |
|
839 interactive session by recording the input you type and the output that |
|
840 Octave produces in a separate file. |
|
841 |
|
842 @deffn {Command} diary options |
|
843 Create a list of all commands @emph{and} the output they produce, mixed |
|
844 together just as you see them on your terminal. Valid options are: |
|
845 |
|
846 @table @code |
|
847 @item on |
|
848 Start recording your session in a file called @file{diary} in your |
|
849 current working directory. |
|
850 |
|
851 @item off |
|
852 Stop recording your session in the diary file. |
|
853 |
|
854 @item @var{file} |
|
855 Record your session in the file named @var{file}. |
|
856 @end table |
|
857 |
|
858 Without any arguments, @code{diary} toggles the current diary state. |
|
859 @end deffn |
|
860 |
|
861 Sometimes it is useful to see the commands in a function or script as |
|
862 they are being evaluated. This can be especially helpful for debugging |
|
863 some kinds of problems. |
|
864 |
|
865 @deffn {Command} echo options |
|
866 Control whether commands are displayed as they are executed. Valid |
|
867 options are: |
|
868 |
|
869 @table @code |
|
870 @item on |
|
871 Enable echoing of commands as they are executed in script files. |
|
872 |
|
873 @item off |
|
874 Disable echoing of commands as they are executed in script files. |
|
875 |
|
876 @item on all |
|
877 Enable echoing of commands as they are executed in script files and |
|
878 functions. |
|
879 |
|
880 @item off all |
|
881 Disable echoing of commands as they are executed in script files and |
|
882 functions. |
|
883 @end table |
|
884 |
|
885 @noindent |
|
886 If invoked without any arguments, @code{echo} toggles the current echo |
|
887 state. |
|
888 @end deffn |
|
889 |
|
890 @defvr {Built-in Variable} echo_executing_commands |
|
891 This variable is may also be used to control the echo state. It may be |
|
892 the sum of the following values: |
|
893 |
|
894 @table @asis |
|
895 @item 1 |
|
896 Echo commands read from script files. |
|
897 |
|
898 @item 2 |
|
899 Echo commands from functions. |
|
900 |
|
901 @item 4 |
|
902 Echo commands read from command line. |
|
903 @end table |
|
904 |
|
905 More than one state can be active at once. For example, a value of 3 is |
|
906 equivalent to the command @kbd{echo on all}. |
|
907 |
|
908 The value of @code{echo_executing_commands} is set by the @kbd{echo} |
|
909 command and the command line option @code{--echo-input}. |
|
910 @end defvr |
|
911 |
|
912 @node Errors, Executable Octave Programs, Command Line Editing, Getting Started |
|
913 @section How Octave Reports Errors |
|
914 @cindex error messages |
|
915 @cindex messages, error |
2653
|
916 |
|
917 There are two classes of errors that Octave produces when it encounters |
2689
|
918 input that it is unable to understand. |
2653
|
919 |
|
920 A @dfn{parse error} occurs if Octave cannot understand something you |
|
921 have typed. For example, if you misspell a keyword, |
|
922 |
|
923 @example |
|
924 octave:13> functon y = f (x) y = x^2; endfunction |
|
925 @end example |
|
926 |
|
927 @noindent |
|
928 Octave will respond immediately with a message like this: |
|
929 |
|
930 @example |
|
931 parse error: |
|
932 |
|
933 functon y = f (x) y = x^2; endfunction |
|
934 ^ |
|
935 @end example |
|
936 |
|
937 @noindent |
|
938 For most parse errors, Octave uses a caret (@samp{^}) to mark the point |
|
939 on the line where it was unable to make sense of your input. In this |
|
940 case, Octave generated an error message because the keyword |
|
941 @code{function} was misspelled. Instead of seeing @samp{function f}, |
|
942 Octave saw two consecutive variable names, which is invalid in this |
|
943 context. It marked the error at the @code{y} because the first name by |
|
944 itself was accepted as valid input. |
|
945 |
|
946 Another class of error message occurs occurs at evaluation time. These |
|
947 errors are called @dfn{run-time errors}, or sometimes |
|
948 @dfn{evaluation errors} because they occur when your program is being |
|
949 @dfn{run}, or @dfn{evaluated}. For example, if after correcting the |
|
950 mistake in the previous function definition, you type |
|
951 |
|
952 @example |
|
953 octave:13> f () |
|
954 @end example |
|
955 |
|
956 @noindent |
|
957 Octave will respond with |
|
958 |
|
959 @example |
|
960 @group |
|
961 error: `x' undefined near line 1 column 24 |
|
962 error: evaluating expression near line 1, column 24 |
|
963 error: evaluating assignment expression near line 1, column 22 |
|
964 error: called from `f' |
|
965 @end group |
|
966 @end example |
|
967 |
|
968 This error message has several parts, and gives you quite a bit of |
|
969 information to help you locate the source of the error. The messages |
|
970 are generated from the point of the innermost error, and provide a |
|
971 traceback of enclosing expressions and function calls. |
|
972 |
|
973 In the example above, the first line indicates that a variable named |
|
974 @samp{x} was found to be undefined near line 1 and column 24 of some |
|
975 function or expression. For errors occurring within functions, lines |
|
976 from the beginning of the file containing the function definition. For |
|
977 errors occurring at the top level, the line number indicates the input |
|
978 line number, which is usually displayed in the prompt string. |
|
979 |
|
980 The second and third lines in the example indicate that the error |
|
981 occurred within an assignment expression, and the last line of the error |
2689
|
982 message indicates that the error occurred within the function @code{f}. |
|
983 If the function @code{f} had been called from another function, for |
|
984 example, @code{g}, the list of errors would have ended with one more |
2653
|
985 line: |
|
986 |
|
987 @example |
|
988 error: called from `g' |
|
989 @end example |
|
990 |
|
991 These lists of function calls usually make it fairly easy to trace the |
|
992 path your program took before the error occurred, and to correct the |
|
993 error before trying again. |
|
994 |
2670
|
995 @node Executable Octave Programs, Comments, Errors, Getting Started |
|
996 @section Executable Octave Programs |
|
997 @cindex executable scripts |
|
998 @cindex scripts |
|
999 @cindex self contained programs |
|
1000 @cindex program, self contained |
|
1001 @cindex @samp{#!} |
2653
|
1002 |
2670
|
1003 Once you have learned Octave, you may want to write self-contained |
|
1004 Octave scripts, using the @samp{#!} script mechanism. You can do this |
|
1005 on GNU systems and on many Unix systems @footnote{The @samp{#!} |
|
1006 mechanism works on Unix systems derived from Berkeley Unix, System V |
|
1007 Release 4, and some System V Release 3 systems.} |
2653
|
1008 |
2670
|
1009 For example, you could create a text file named @file{hello}, containing |
|
1010 the following lines: |
2653
|
1011 |
|
1012 @example |
2670
|
1013 @group |
|
1014 #! @value{OCTAVEHOME}/bin/octave -qf |
|
1015 # a sample Octave program |
|
1016 printf ("Hello, world!\n"); |
|
1017 @end group |
2653
|
1018 @end example |
|
1019 |
|
1020 @noindent |
2670
|
1021 After making this file executable (with the @code{chmod} command), you |
|
1022 can simply type: |
2653
|
1023 |
|
1024 @example |
2670
|
1025 hello |
2653
|
1026 @end example |
|
1027 |
|
1028 @noindent |
2670
|
1029 at the shell, and the system will arrange to run Octave as if you had |
|
1030 typed: |
2653
|
1031 |
|
1032 @example |
2670
|
1033 octave hello |
2653
|
1034 @end example |
|
1035 |
2670
|
1036 The line beginning with @samp{#!} lists the full file name of an |
|
1037 interpreter to be run, and an optional initial command line argument to |
|
1038 pass to that interpreter. The operating system then runs the |
|
1039 interpreter with the given argument and the full argument list of the |
|
1040 executed program. The first argument in the list is the full file name |
|
1041 of the Octave program. The rest of the argument list will either be |
|
1042 options to Octave, or data files, or both. The @code{-qf} option is |
|
1043 usually specified in stand-alone Octave programs to prevent them from |
|
1044 printing the normal startup message, and to keep them from behaving |
|
1045 differently depending on the contents of a particular user's |
|
1046 @file{~/.octaverc} file. @xref{Invoking Octave}. Note that some |
|
1047 operating systems may place a limit on the number of characters that are |
|
1048 recognized after @samp{#!}. |
|
1049 |
|
1050 Self-contained Octave scripts are useful when you want to write a |
|
1051 program which users can invoke without knowing that the program is |
|
1052 written in the Octave language. |
|
1053 |
|
1054 If you invoke an executable Octave script with command line arguments, |
|
1055 the arguments are available in the built-in variable @code{argv}. |
|
1056 @xref{Command Line Options}. For example, the following program will |
|
1057 reproduce the command line that is used to execute it. |
2653
|
1058 |
|
1059 @example |
2670
|
1060 @group |
|
1061 #! @value{OCTAVEHOME}/bin/octave -qf |
|
1062 printf ("%s", program_name); |
|
1063 for i = 1:nargin |
|
1064 printf (" %s", argv(i,:)); |
|
1065 endfor |
|
1066 printf ("\n"); |
|
1067 @end group |
2653
|
1068 @end example |
|
1069 |
2670
|
1070 @node Comments, , Executable Octave Programs, Getting Started |
|
1071 @section Comments in Octave Programs |
|
1072 @cindex @samp{#} |
|
1073 @cindex @samp{%} |
|
1074 @cindex comments |
|
1075 @cindex use of comments |
|
1076 @cindex documenting Octave programs |
|
1077 @cindex programs |
2653
|
1078 |
2670
|
1079 A @dfn{comment} is some text that is included in a program for the sake |
|
1080 of human readers, and that is not really part of the program. Comments |
|
1081 can explain what the program does, and how it works. Nearly all |
|
1082 programming languages have provisions for comments, because programs are |
|
1083 typically hard to understand without them. |
2653
|
1084 |
2670
|
1085 In the Octave language, a comment starts with either the sharp sign |
|
1086 character, @samp{#}, or the percent symbol @samp{%} and continues to the |
|
1087 end of the line. The Octave interpreter ignores the rest of a |
|
1088 line following a sharp sign or percent symbol. For example, we could |
|
1089 have put the following into the function @code{f}: |
2653
|
1090 |
|
1091 @example |
2670
|
1092 @group |
|
1093 function xdot = f (x, t) |
|
1094 |
|
1095 # usage: f (x, t) |
|
1096 # |
|
1097 # This function defines the right hand |
|
1098 # side functions for a set of nonlinear |
|
1099 # differential equations. |
|
1100 |
|
1101 r = 0.25; |
|
1102 @dots{} |
|
1103 endfunction |
|
1104 @end group |
2653
|
1105 @end example |
|
1106 |
2670
|
1107 The @code{help} command (@pxref{Getting Help}) is able to find the first |
|
1108 block of comments in a function (even those that are composed directly |
|
1109 on the command line). This means that users of Octave can use the same |
|
1110 commands to get help for built-in functions, and for functions that you |
|
1111 have defined. For example, after defining the function @code{f} above, |
|
1112 the command @kbd{help f} produces the output |
2653
|
1113 |
|
1114 @example |
2670
|
1115 @group |
|
1116 usage: f (x, t) |
2653
|
1117 |
2670
|
1118 This function defines the right hand |
|
1119 side functions for a set of nonlinear |
|
1120 differential equations. |
|
1121 @end group |
2653
|
1122 @end example |
|
1123 |
2670
|
1124 Although it is possible to put comment lines into keyboard-composed |
|
1125 throw-away Octave programs, it usually isn't very useful, because the |
|
1126 purpose of a comment is to help you or another person understand the |
|
1127 program at a later time. |
2653
|
1128 |