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 |
2993
|
36 of the options available. Type @samp{octave --help} to display all |
|
37 available options and briefly describe their use (@samp{octave -h} is a |
2670
|
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 |
2993
|
74 in the system or user startup files that set the built-in variable |
|
75 @code{EXEC_PATH}. |
2653
|
76 |
2670
|
77 @item --help |
|
78 @itemx -h |
|
79 @itemx -? |
|
80 @cindex @code{--help} |
|
81 @cindex @code{-h} |
|
82 @cindex @code{-?} |
|
83 Print short help message and exit. |
|
84 |
|
85 @item --info-file @var{filename} |
|
86 @cindex @code{--info-file @var{filename}} |
|
87 Specify the name of the info file to use. The value of @var{filename} |
|
88 specified on the command line will override any value of |
2689
|
89 @code{OCTAVE_INFO_FILE} found in the environment, but not any commands |
2993
|
90 in the system or user startup files that set the built-in variable |
|
91 @code{INFO_FILE}. |
2653
|
92 |
2670
|
93 @item --info-program @var{program} |
|
94 @cindex @code{--info-program @var{program}} |
|
95 Specify the name of the info program to use. The value of @var{program} |
|
96 specified on the command line will override any value of |
2689
|
97 @code{OCTAVE_INFO_PROGRAM} found in the environment, but not any |
2993
|
98 commands in the system or user startup files that set the built-in |
|
99 variable @code{INFO_PROGRAM}. |
2653
|
100 |
2670
|
101 @item --interactive |
|
102 @itemx -i |
|
103 @cindex @code{--interactive} |
|
104 @cindex @code{-i} |
2689
|
105 Force interactive behavior. This can be useful for running Octave via a |
|
106 remote shell command or inside an Emacs shell buffer. For another way |
|
107 to run Octave within Emacs, see @ref{Emacs}. |
2653
|
108 |
2670
|
109 @item --no-init-file |
|
110 @cindex @code{--no-init-file} |
|
111 Don't read the @file{~/.octaverc} or @file{.octaverc} files. |
|
112 |
|
113 @item --no-line-editing |
|
114 @cindex @code{--no-line-editing} |
3162
|
115 Disable command-line editing. |
2670
|
116 |
|
117 @item --no-site-file |
|
118 @cindex @code{--no-site-file} |
|
119 Don't read the site-wide @file{octaverc} file. |
2653
|
120 |
2670
|
121 @item --norc |
|
122 @itemx -f |
|
123 @cindex @code{--norc} |
|
124 @cindex @code{-f} |
|
125 Don't read any of the system or user initialization files at startup. |
|
126 This is equivalent to using both of the options @code{--no-init-file} |
|
127 and @code{--no-site-file}. |
2653
|
128 |
2670
|
129 @item --path @var{path} |
|
130 @itemx -p @var{path} |
|
131 @cindex @code{--path @var{path}} |
|
132 @cindex @code{-p @var{path}} |
|
133 Specify the path to search for function files. The value of @var{path} |
|
134 specified on the command line will override any value of |
2689
|
135 @code{OCTAVE_PATH} found in the environment, but not any commands in the |
2993
|
136 system or user startup files that set the built-in variable @code{LOADPATH}. |
2670
|
137 |
|
138 @item --silent |
|
139 @itemx --quiet |
|
140 @itemx -q |
|
141 @cindex @code{--silent} |
|
142 @cindex @code{--quiet} |
|
143 @cindex @code{-q} |
2689
|
144 Don't print the usual greeting and version message at startup. |
2670
|
145 |
|
146 @item --traditional |
|
147 @itemx --braindead |
|
148 @cindex @code{--traditional} |
|
149 @cindex @code{--braindead} |
|
150 Set initial values for user-preference variables to the following |
|
151 values for compatibility with @sc{Matlab}. |
2653
|
152 |
|
153 @example |
2670
|
154 PS1 = ">> " |
|
155 PS2 = "" |
|
156 beep_on_error = 1 |
|
157 default_save_format = "mat-binary" |
|
158 define_all_return_values = 1 |
|
159 do_fortran_indexing = 1 |
|
160 empty_list_elements_ok = 1 |
|
161 implicit_str_to_num_ok = 1 |
|
162 ok_to_lose_imaginary_part = 1 |
|
163 page_screen_output = 0 |
|
164 prefer_column_vectors = 0 |
|
165 print_empty_dimensions = 0 |
|
166 treat_neg_dim_as_zero = 1 |
|
167 warn_function_name_clash = 0 |
|
168 whitespace_in_literal_matrix = "traditional" |
|
169 @end example |
|
170 |
|
171 @item --verbose |
|
172 @itemx -V |
|
173 @cindex @code{--verbose} |
|
174 @cindex @code{-V} |
|
175 Turn on verbose output. |
|
176 |
|
177 @item --version |
|
178 @itemx -v |
|
179 @cindex @code{--version} |
|
180 @cindex @code{-v} |
|
181 Print the program version number and exit. |
|
182 |
|
183 @item @var{file} |
|
184 Execute commands from @var{file}. |
|
185 @end table |
|
186 |
|
187 Octave also includes several built-in variables that contain information |
|
188 about the command line, including the number of arguments and all of the |
|
189 options. |
|
190 |
|
191 @defvr {Built-in Variable} argv |
|
192 The command line arguments passed to Octave are available in this |
|
193 variable. For example, if you invoked Octave using the command |
|
194 |
|
195 @example |
|
196 octave --no-line-editing --silent |
2653
|
197 @end example |
|
198 |
|
199 @noindent |
2670
|
200 @code{argv} would be a string vector with the elements |
|
201 @code{--no-line-editing} and @code{--silent}. |
2653
|
202 |
2670
|
203 If you write an executable Octave script, @code{argv} will contain the |
|
204 list of arguments passed to the script. @pxref{Executable Octave Programs}. |
|
205 @end defvr |
2653
|
206 |
2670
|
207 @defvr {Built-in Variable} program_invocation_name |
|
208 @defvrx {Built-in Variable} program_name |
|
209 When Octave starts, the value of the built-in variable |
|
210 @code{program_invocation_name} is automatically set to the name that was |
|
211 typed at the shell prompt to run Octave, and the value of |
|
212 @code{program_name} is automatically set to the final component of |
|
213 @code{program_invocation_name}. For example, if you typed |
2993
|
214 @samp{@value{OCTAVEHOME}/bin/octave} to start Octave, |
2670
|
215 @code{program_invocation_name} would have the value |
|
216 @code{"@value{OCTAVEHOME}/bin/octave"}, and @code{program_name} would |
|
217 have the value @code{"octave"}. |
2653
|
218 |
2670
|
219 If executing a script from the command line (e.g., @code{octave foo.m} |
|
220 or using an executable Octave script, the program name is set to the |
|
221 name of the script. @xref{Executable Octave Programs} for an example of |
|
222 how to create an executable Octave script. |
|
223 @end defvr |
2653
|
224 |
2670
|
225 Here is an example of using these variables to reproduce Octave's |
|
226 command line. |
2653
|
227 |
|
228 @example |
|
229 printf ("%s", program_name); |
|
230 for i = 1:nargin |
|
231 printf (" %s", argv(i,:)); |
|
232 endfor |
|
233 printf ("\n"); |
|
234 @end example |
|
235 |
2670
|
236 @noindent |
|
237 @xref{Index Expressions} for an explanation of how to properly index |
3131
|
238 arrays of strings and substrings in Octave, and @xref{Defining Functions} |
|
239 for information about the variable @code{nargin}. |
2670
|
240 |
|
241 @node Startup Files, , Command Line Options, Invoking Octave |
|
242 @subsection Startup Files |
|
243 @cindex initialization |
|
244 @cindex startup |
|
245 |
|
246 When Octave starts, it looks for commands to execute from the following |
|
247 files: |
|
248 |
|
249 @cindex startup files |
|
250 |
|
251 @table @code |
2993
|
252 @item @var{octave-home}/share/octave/site/m/startup/octaverc |
|
253 Where @var{octave-home} is the directory in which all of Octave is |
2701
|
254 installed (the default is @file{@value{OCTAVEHOME}}). This file is |
|
255 provided so that changes to the default Octave environment can be made |
|
256 globally for all users at your site for all versions of Octave you have |
|
257 installed. Some care should be taken when making changes to this file, |
|
258 since all users of Octave at your site will be affected. |
2670
|
259 |
2993
|
260 @item @var{octave-home}/share/octave/@var{version}/m/startup/octaverc |
|
261 Where @var{octave-home} is the directory in which all of Octave is |
|
262 installed (the default is @file{@value{OCTAVEHOME}}), and @var{version} |
2701
|
263 is the version number of Octave. This file is provided so that changes |
|
264 to the default Octave environment can be made globally for all users for |
|
265 a particular version of Octave. Some care should be taken when making |
2670
|
266 changes to this file, since all users of Octave at your site will be |
|
267 affected. |
|
268 |
|
269 @item ~/.octaverc |
|
270 @cindex @code{~/.octaverc} |
|
271 This file is normally used to make personal changes to the default |
|
272 Octave environment. |
|
273 |
|
274 @item .octaverc |
|
275 @cindex @code{.octaverc} |
|
276 This file can be used to make changes to the default Octave environment |
|
277 for a particular project. Octave searches for this file in the current |
|
278 directory after it reads @file{~/.octaverc}. Any use of the @code{cd} |
|
279 command in the @file{~/.octaverc} file will affect the directory that |
|
280 Octave searches for the file @file{.octaverc}. |
|
281 |
|
282 If you start Octave in your home directory, commands from from the file |
|
283 @file{~/.octaverc} will only be executed once. |
|
284 @end table |
|
285 |
|
286 A message will be displayed as each of the startup files is read if you |
|
287 invoke Octave with the @code{--verbose} option but without the |
|
288 @code{--silent} option. |
|
289 |
|
290 Startup files may contain any valid Octave commands, including function |
|
291 definitions. |
|
292 |
|
293 @node Quitting Octave, Getting Help, Invoking Octave, Getting Started |
|
294 @section Quitting Octave |
|
295 @cindex exiting octave |
|
296 @cindex quitting octave |
|
297 |
|
298 @deftypefn {Built-in Function} {} exit (@var{status}) |
|
299 @deftypefnx {Built-in Function} {} quit (@var{status}) |
|
300 Exit the current Octave session. If the optional integer value |
|
301 @var{status} is supplied, pass that value to the operating system as the |
|
302 Octave's exit status. |
|
303 @end deftypefn |
|
304 |
|
305 @deftypefn {Built-in Function} {} atexit (@var{fcn}) |
2689
|
306 Register function to be called when Octave exits. For example, |
|
307 |
|
308 @example |
|
309 @group |
|
310 function print_flops_at_exit () |
|
311 printf ("\n%s\n", system ("fortune")); |
|
312 fflush (stdout); |
|
313 endfunction |
|
314 atexit ("print_flops_at_exit"); |
|
315 @end group |
|
316 @end example |
|
317 |
|
318 @noindent |
|
319 will print a message when Octave exits. |
2670
|
320 @end deftypefn |
|
321 |
|
322 @node Getting Help, Command Line Editing, Quitting Octave, Getting Started |
|
323 @section Commands for Getting Help |
|
324 @cindex on-line help |
|
325 @cindex help, on-line |
|
326 |
|
327 The entire text of this manual is available from the Octave prompt |
|
328 via the command @kbd{help -i}. In addition, the documentation for |
|
329 individual user-written functions and variables is also available via |
|
330 the @kbd{help} command. This section describes the commands used for |
|
331 reading the manual and the documentation strings for user-supplied |
|
332 functions and variables. @xref{Function Files}, for more information |
|
333 about how to document the functions you write. |
|
334 |
|
335 @deffn {Command} help |
|
336 Octave's @code{help} command can be used to print brief usage-style |
|
337 messages, or to display information directly from an on-line version of |
|
338 the printed manual, using the GNU Info browser. If invoked without any |
|
339 arguments, @code{help} prints a list of all the available operators, |
|
340 functions, and built-in variables. If the first argument is @code{-i}, |
|
341 the @code{help} command searches the index of the on-line version of |
|
342 this manual for the given topics. |
|
343 |
|
344 For example, the command @kbd{help help} prints a short message |
|
345 describing the @code{help} command, and @kbd{help -i help} starts the |
|
346 GNU Info browser at this node in the on-line version of the manual. |
|
347 |
2689
|
348 Once the GNU Info browser is running, help for using it is available |
2670
|
349 using the command @kbd{C-h}. |
|
350 @end deffn |
|
351 |
|
352 The help command can give you information about operators, but not the |
|
353 comma and semicolons that are used as command separators. To get help |
|
354 for those, you must type @kbd{help comma} or @kbd{help semicolon}. |
|
355 |
|
356 @defvr {Built-in Variable} INFO_FILE |
|
357 The variable @code{INFO_FILE} names the location of the Octave info file. |
2993
|
358 The default value is @code{"@var{octave-home}/info/octave.info"}, where |
|
359 @var{octave-home} is the directory where all of Octave is installed. |
2670
|
360 @end defvr |
|
361 |
|
362 @defvr {Built-in Variable} INFO_PROGRAM |
|
363 The variable @code{INFO_PROGRAM} names the info program to run. Its |
|
364 initial value is |
2993
|
365 @code{"@var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}/info"}, |
|
366 where @var{octave-home} is the directory where all of Octave is |
|
367 installed, @var{version} is the Octave version number, and @var{arch} is |
2701
|
368 the machine type. The value of @code{INFO_PROGRAM} can be overridden by |
|
369 the environment variable @code{OCTAVE_INFO_PROGRAM}, or the command line |
2993
|
370 argument @code{--info-program NAME}, or by setting the value of the |
|
371 built-in variable @code{INFO_PROGRAM} in a startup script. |
2670
|
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 |
2993
|
431 @item @key{DEL} |
2670
|
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 |
2993
|
500 @item M-@key{DEL} |
2670
|
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 |
2993
|
506 @kbd{M-@key{DEL}} because the word boundaries differ. |
2670
|
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 |
2993
|
541 @item M-@key{TAB} |
2670
|
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 |
2993
|
575 @item @key{TAB} |
2670
|
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 |
2993
|
615 @item @key{LFD} |
|
616 @itemx @key{RET} |
2670
|
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 |
3156
|
639 the history as necessary. |
2670
|
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 |
2993
|
654 @item -w @var{file} |
|
655 Write the current history to the file @var{file}. If the name is |
|
656 omitted, use the default history file (normally @file{~/.octave_hist}). |
2670
|
657 |
2993
|
658 @item -r @var{file} |
|
659 Read the file @var{file}, replacing the current history list with its |
2670
|
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 |
2993
|
719 @code{"emacs"}. |
2670
|
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 |
2993
|
762 The name of the program (usually just @samp{octave}). |
2670
|
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 |
2864
|
774 The hostname, up to the first `.'. |
2670
|
775 |
|
776 @item \H |
2864
|
777 The hostname. |
2670
|
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 \$ |
2993
|
787 If the effective UID is 0, a @samp{#}, otherwise a @samp{$}. |
2670
|
788 |
|
789 @item \nnn |
2993
|
790 The character whose character code in octal is @var{nnn}. |
2670
|
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 |
2993
|
917 Octave reports two kinds of errors for invalid programs. |
2653
|
918 |
|
919 A @dfn{parse error} occurs if Octave cannot understand something you |
|
920 have typed. For example, if you misspell a keyword, |
|
921 |
|
922 @example |
|
923 octave:13> functon y = f (x) y = x^2; endfunction |
|
924 @end example |
|
925 |
|
926 @noindent |
|
927 Octave will respond immediately with a message like this: |
|
928 |
|
929 @example |
|
930 parse error: |
|
931 |
|
932 functon y = f (x) y = x^2; endfunction |
|
933 ^ |
|
934 @end example |
|
935 |
|
936 @noindent |
|
937 For most parse errors, Octave uses a caret (@samp{^}) to mark the point |
|
938 on the line where it was unable to make sense of your input. In this |
|
939 case, Octave generated an error message because the keyword |
|
940 @code{function} was misspelled. Instead of seeing @samp{function f}, |
|
941 Octave saw two consecutive variable names, which is invalid in this |
|
942 context. It marked the error at the @code{y} because the first name by |
|
943 itself was accepted as valid input. |
|
944 |
3162
|
945 Another class of error message occurs at evaluation time. These |
2653
|
946 errors are called @dfn{run-time errors}, or sometimes |
|
947 @dfn{evaluation errors} because they occur when your program is being |
|
948 @dfn{run}, or @dfn{evaluated}. For example, if after correcting the |
|
949 mistake in the previous function definition, you type |
|
950 |
|
951 @example |
|
952 octave:13> f () |
|
953 @end example |
|
954 |
|
955 @noindent |
|
956 Octave will respond with |
|
957 |
|
958 @example |
|
959 @group |
|
960 error: `x' undefined near line 1 column 24 |
|
961 error: evaluating expression near line 1, column 24 |
|
962 error: evaluating assignment expression near line 1, column 22 |
|
963 error: called from `f' |
|
964 @end group |
|
965 @end example |
|
966 |
|
967 This error message has several parts, and gives you quite a bit of |
|
968 information to help you locate the source of the error. The messages |
|
969 are generated from the point of the innermost error, and provide a |
|
970 traceback of enclosing expressions and function calls. |
|
971 |
|
972 In the example above, the first line indicates that a variable named |
|
973 @samp{x} was found to be undefined near line 1 and column 24 of some |
|
974 function or expression. For errors occurring within functions, lines |
|
975 from the beginning of the file containing the function definition. For |
|
976 errors occurring at the top level, the line number indicates the input |
|
977 line number, which is usually displayed in the prompt string. |
|
978 |
|
979 The second and third lines in the example indicate that the error |
|
980 occurred within an assignment expression, and the last line of the error |
2689
|
981 message indicates that the error occurred within the function @code{f}. |
|
982 If the function @code{f} had been called from another function, for |
|
983 example, @code{g}, the list of errors would have ended with one more |
2653
|
984 line: |
|
985 |
|
986 @example |
|
987 error: called from `g' |
|
988 @end example |
|
989 |
|
990 These lists of function calls usually make it fairly easy to trace the |
|
991 path your program took before the error occurred, and to correct the |
|
992 error before trying again. |
|
993 |
2670
|
994 @node Executable Octave Programs, Comments, Errors, Getting Started |
|
995 @section Executable Octave Programs |
|
996 @cindex executable scripts |
|
997 @cindex scripts |
|
998 @cindex self contained programs |
|
999 @cindex program, self contained |
|
1000 @cindex @samp{#!} |
2653
|
1001 |
2670
|
1002 Once you have learned Octave, you may want to write self-contained |
|
1003 Octave scripts, using the @samp{#!} script mechanism. You can do this |
|
1004 on GNU systems and on many Unix systems @footnote{The @samp{#!} |
|
1005 mechanism works on Unix systems derived from Berkeley Unix, System V |
|
1006 Release 4, and some System V Release 3 systems.} |
2653
|
1007 |
2670
|
1008 For example, you could create a text file named @file{hello}, containing |
|
1009 the following lines: |
2653
|
1010 |
|
1011 @example |
2670
|
1012 @group |
2993
|
1013 #! @var{octave-interpreter-name} -qf |
2670
|
1014 # a sample Octave program |
|
1015 printf ("Hello, world!\n"); |
|
1016 @end group |
2653
|
1017 @end example |
|
1018 |
|
1019 @noindent |
2993
|
1020 (where @var{octave-interpreter-name} should be replaced with the full |
|
1021 file name for your Octave binary). After making this file executable |
|
1022 (with the @code{chmod} command), you 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 |
2993
|
1042 options to Octave, or data files, or both. The @samp{-qf} option is |
2670
|
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 |
2701
|
1061 #! /bin/octave -qf |
2670
|
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 |