]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - readline/doc/history.3
bfd/
[thirdparty/binutils-gdb.git] / readline / doc / history.3
CommitLineData
9255ee31
EZ
1.\"
2.\" MAN PAGE COMMENTS to
3.\"
4.\" Chet Ramey
5.\" Information Network Services
6.\" Case Western Reserve University
7.\" chet@ins.CWRU.Edu
8.\"
5bdf8622 9.\" Last Change: Thu Jul 31 08:46:08 EDT 2003
9255ee31 10.\"
5bdf8622 11.TH HISTORY 3 "2003 July 31" "GNU History 5.0"
9255ee31
EZ
12.\"
13.\" File Name macro. This used to be `.PN', for Path Name,
14.\" but Sun doesn't seem to like that very much.
15.\"
16.de FN
17\fI\|\\$1\|\fP
18..
19.ds lp \fR\|(\fP
20.ds rp \fR\|)\fP
21.\" FnN return-value fun-name N arguments
22.de Fn1
23\fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3\fP\\*(rp
24.br
25..
26.de Fn2
27.if t \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3,\|\\$4\fP\\*(rp
28.if n \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3, \\$4\fP\\*(rp
29.br
30..
31.de Fn3
32.if t \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3,\|\\$4,\|\\$5\fP\|\\*(rp
33.if n \fI\\$1\fP \fB\\$2\fP \\*(lp\fI\\$3, \\$4, \\$5\fP\\*(rp
34.br
35..
36.de Vb
37\fI\\$1\fP \fB\\$2\fP
38.br
39..
40.SH NAME
41history \- GNU History Library
42.SH COPYRIGHT
43.if t The GNU History Library is Copyright \(co 1989-2002 by the Free Software Foundation, Inc.
44.if n The GNU History Library is Copyright (C) 1989-2002 by the Free Software Foundation, Inc.
45.SH DESCRIPTION
46Many programs read input from the user a line at a time. The GNU
47History library is able to keep track of those lines, associate arbitrary
48data with each line, and utilize information from previous lines in
49composing new ones.
50.PP
51.SH "HISTORY EXPANSION"
52.PP
53The history library supports a history expansion feature that
54is identical to the history expansion in
55.BR bash.
56This section describes what syntax features are available.
57.PP
58History expansions introduce words from the history list into
59the input stream, making it easy to repeat commands, insert the
60arguments to a previous command into the current input line, or
61fix errors in previous commands quickly.
62.PP
63History expansion is usually performed immediately after a complete line
64is read.
65It takes place in two parts.
66The first is to determine which line from the history list
67to use during substitution.
68The second is to select portions of that line for inclusion into
69the current one.
70The line selected from the history is the \fIevent\fP,
71and the portions of that line that are acted upon are \fIwords\fP.
72Various \fImodifiers\fP are available to manipulate the selected words.
73The line is broken into words in the same fashion as \fBbash\fP
74does when reading input,
75so that several words that would otherwise be separated
76are considered one word when surrounded by quotes (see the
77description of \fBhistory_tokenize()\fP below).
78History expansions are introduced by the appearance of the
79history expansion character, which is \^\fB!\fP\^ by default.
80Only backslash (\^\fB\e\fP\^) and single quotes can quote
81the history expansion character.
82.SS Event Designators
83.PP
84An event designator is a reference to a command line entry in the
85history list.
86.PP
87.PD 0
88.TP
89.B !
90Start a history substitution, except when followed by a
91.BR blank ,
92newline, = or (.
93.TP
94.B !\fIn\fR
95Refer to command line
96.IR n .
97.TP
98.B !\-\fIn\fR
99Refer to the current command line minus
100.IR n .
101.TP
102.B !!
103Refer to the previous command. This is a synonym for `!\-1'.
104.TP
105.B !\fIstring\fR
106Refer to the most recent command starting with
107.IR string .
108.TP
109.B !?\fIstring\fR\fB[?]\fR
110Refer to the most recent command containing
111.IR string .
112The trailing \fB?\fP may be omitted if
113.I string
114is followed immediately by a newline.
115.TP
116.B \d\s+2^\s-2\u\fIstring1\fP\d\s+2^\s-2\u\fIstring2\fP\d\s+2^\s-2\u
117Quick substitution. Repeat the last command, replacing
118.I string1
119with
120.IR string2 .
121Equivalent to
122``!!:s/\fIstring1\fP/\fIstring2\fP/''
123(see \fBModifiers\fP below).
124.TP
125.B !#
126The entire command line typed so far.
127.PD
128.SS Word Designators
129.PP
130Word designators are used to select desired words from the event.
131A
132.B :
133separates the event specification from the word designator.
134It may be omitted if the word designator begins with a
135.BR ^ ,
136.BR $ ,
137.BR * ,
138.BR \- ,
139or
140.BR % .
141Words are numbered from the beginning of the line,
142with the first word being denoted by 0 (zero).
143Words are inserted into the current line separated by single spaces.
144.PP
145.PD 0
146.TP
147.B 0 (zero)
148The zeroth word. For the shell, this is the command
149word.
150.TP
151.I n
152The \fIn\fRth word.
153.TP
154.B ^
155The first argument. That is, word 1.
156.TP
157.B $
158The last argument.
159.TP
160.B %
161The word matched by the most recent `?\fIstring\fR?' search.
162.TP
163.I x\fB\-\fPy
164A range of words; `\-\fIy\fR' abbreviates `0\-\fIy\fR'.
165.TP
166.B *
167All of the words but the zeroth. This is a synonym
168for `\fI1\-$\fP'. It is not an error to use
169.B *
170if there is just one
171word in the event; the empty string is returned in that case.
172.TP
173.B x*
174Abbreviates \fIx\-$\fP.
175.TP
176.B x\-
177Abbreviates \fIx\-$\fP like \fBx*\fP, but omits the last word.
178.PD
179.PP
180If a word designator is supplied without an event specification, the
181previous command is used as the event.
182.SS Modifiers
183.PP
184After the optional word designator, there may appear a sequence of
185one or more of the following modifiers, each preceded by a `:'.
186.PP
187.PD 0
188.PP
189.TP
190.B h
191Remove a trailing file name component, leaving only the head.
192.TP
193.B t
194Remove all leading file name components, leaving the tail.
195.TP
196.B r
197Remove a trailing suffix of the form \fI.xxx\fP, leaving the
198basename.
199.TP
200.B e
201Remove all but the trailing suffix.
202.TP
203.B p
204Print the new command but do not execute it.
205.TP
206.B q
207Quote the substituted words, escaping further substitutions.
208.TP
209.B x
210Quote the substituted words as with
211.BR q ,
212but break into words at
213.B blanks
214and newlines.
215.TP
216.B s/\fIold\fP/\fInew\fP/
217Substitute
218.I new
219for the first occurrence of
220.I old
221in the event line. Any delimiter can be used in place of /. The
222final delimiter is optional if it is the last character of the
223event line. The delimiter may be quoted in
224.I old
225and
226.I new
227with a single backslash. If & appears in
228.IR new ,
229it is replaced by
230.IR old .
231A single backslash will quote the &. If
232.I old
233is null, it is set to the last
234.I old
235substituted, or, if no previous history substitutions took place,
236the last
237.I string
238in a
239.B !?\fIstring\fR\fB[?]\fR
240search.
241.TP
242.B &
243Repeat the previous substitution.
244.TP
245.B g
246Cause changes to be applied over the entire event line. This is
247used in conjunction with `\fB:s\fP' (e.g., `\fB:gs/\fIold\fP/\fInew\fP/\fR')
248or `\fB:&\fP'. If used with
249`\fB:s\fP', any delimiter can be used
250in place of /, and the final delimiter is optional
251if it is the last character of the event line.
5bdf8622
DJ
252An \fBa\fP may be used as a synonym for \fBg\fP.
253.TP
254.B G
255Apply the following `\fBs\fP' modifier once to each word in the event line.
9255ee31
EZ
256.PD
257.SH "PROGRAMMING WITH HISTORY FUNCTIONS"
258This section describes how to use the History library in other programs.
259.SS Introduction to History
260.PP
261The programmer using the History library has available functions
262for remembering lines on a history list, associating arbitrary data
263with a line, removing lines from the list, searching through the list
264for a line containing an arbitrary text string, and referencing any line
265in the list directly. In addition, a history \fIexpansion\fP function
266is available which provides for a consistent user interface across
267different programs.
268.PP
269The user using programs written with the History library has the
270benefit of a consistent user interface with a set of well-known
271commands for manipulating the text of previous lines and using that text
272in new commands. The basic history manipulation commands are
273identical to
274the history substitution provided by \fBbash\fP.
275.PP
276If the programmer desires, he can use the Readline library, which
277includes some history manipulation by default, and has the added
278advantage of command line editing.
279.PP
280Before declaring any functions using any functionality the History
281library provides in other code, an application writer should include
282the file
283.FN <readline/history.h>
284in any file that uses the
285History library's features. It supplies extern declarations for all
286of the library's public functions and variables, and declares all of
287the public data structures.
288
289.SS History Storage
290.PP
291The history list is an array of history entries. A history entry is
292declared as follows:
293.PP
294.Vb "typedef void *" histdata_t;
295.PP
296.nf
297typedef struct _hist_entry {
298 char *line;
5bdf8622 299 char *timestamp;
9255ee31
EZ
300 histdata_t data;
301} HIST_ENTRY;
302.fi
303.PP
304The history list itself might therefore be declared as
305.PP
306.Vb "HIST_ENTRY **" the_history_list;
307.PP
308The state of the History library is encapsulated into a single structure:
309.PP
310.nf
311/*
312 * A structure used to pass around the current state of the history.
313 */
314typedef struct _hist_state {
315 HIST_ENTRY **entries; /* Pointer to the entries themselves. */
316 int offset; /* The location pointer within this array. */
317 int length; /* Number of elements within this array. */
318 int size; /* Number of slots allocated to this array. */
319 int flags;
320} HISTORY_STATE;
321.fi
322.PP
323If the flags member includes \fBHS_STIFLED\fP, the history has been
324stifled.
325.SH "History Functions"
326.PP
327This section describes the calling sequence for the various functions
328exported by the GNU History library.
329.SS Initializing History and State Management
330This section describes functions used to initialize and manage
331the state of the History library when you want to use the history
332functions in your program.
333
334.Fn1 void using_history void
335Begin a session in which the history functions might be used. This
336initializes the interactive variables.
337
338.Fn1 "HISTORY_STATE *" history_get_history_state void
339Return a structure describing the current state of the input history.
340
341.Fn1 void history_set_history_state "HISTORY_STATE *state"
342Set the state of the history list according to \fIstate\fP.
343
344.SS History List Management
345
346These functions manage individual entries on the history list, or set
347parameters managing the list itself.
348
349.Fn1 void add_history "const char *string"
350Place \fIstring\fP at the end of the history list. The associated data
351field (if any) is set to \fBNULL\fP.
352
5bdf8622
DJ
353.Fn1 void add_history_time "const char *string"
354Change the time stamp associated with the most recent history entry to
355\fIstring\fP.
356
9255ee31
EZ
357.Fn1 "HIST_ENTRY *" remove_history "int which"
358Remove history entry at offset \fIwhich\fP from the history. The
359removed element is returned so you can free the line, data,
360and containing structure.
361
5bdf8622
DJ
362.Fn1 "histdata_t" free_history_entry "HIST_ENTRY *histent"
363Free the history entry \fIhistent\fP and any history library private
364data associated with it. Returns the application-specific data
365so the caller can dispose of it.
366
9255ee31
EZ
367.Fn3 "HIST_ENTRY *" replace_history_entry "int which" "const char *line" "histdata_t data"
368Make the history entry at offset \fIwhich\fP have \fIline\fP and \fIdata\fP.
5bdf8622
DJ
369This returns the old entry so the caller can dispose of any
370application-specific data. In the case
9255ee31
EZ
371of an invalid \fIwhich\fP, a \fBNULL\fP pointer is returned.
372
373.Fn1 void clear_history "void"
374Clear the history list by deleting all the entries.
375
376.Fn1 void stifle_history "int max"
377Stifle the history list, remembering only the last \fImax\fP entries.
378
379.Fn1 int unstifle_history "void"
380Stop stifling the history. This returns the previously-set
381maximum number of history entries (as set by \fBstifle_history()\fP).
382history was stifled. The value is positive if the history was
383stifled, negative if it wasn't.
384
385.Fn1 int history_is_stifled "void"
386Returns non-zero if the history is stifled, zero if it is not.
387
388.SS Information About the History List
389
390These functions return information about the entire history list or
391individual list entries.
392
393.Fn1 "HIST_ENTRY **" history_list "void"
394Return a \fBNULL\fP terminated array of \fIHIST_ENTRY *\fP which is the
395current input history. Element 0 of this list is the beginning of time.
396If there is no history, return \fBNULL\fP.
397
398.Fn1 int where_history "void"
399Returns the offset of the current history element.
400
401.Fn1 "HIST_ENTRY *" current_history "void"
402Return the history entry at the current position, as determined by
403\fBwhere_history()\fP. If there is no entry there, return a \fBNULL\fP
404pointer.
405
406.Fn1 "HIST_ENTRY *" history_get "int offset"
407Return the history entry at position \fIoffset\fP, starting from
408\fBhistory_base\fP.
409If there is no entry there, or if \fIoffset\fP
410is greater than the history length, return a \fBNULL\fP pointer.
411
5bdf8622
DJ
412.Fn1 "time_t" history_get_time "HIST_ENTRY *"
413Return the time stamp associated with the history entry passed as the argument.
414
9255ee31
EZ
415.Fn1 int history_total_bytes "void"
416Return the number of bytes that the primary history entries are using.
417This function returns the sum of the lengths of all the lines in the
418history.
419
420.SS Moving Around the History List
421
422These functions allow the current index into the history list to be
423set or changed.
424
425.Fn1 int history_set_pos "int pos"
426Set the current history offset to \fIpos\fP, an absolute index
427into the list.
428Returns 1 on success, 0 if \fIpos\fP is less than zero or greater
429than the number of history entries.
430
431.Fn1 "HIST_ENTRY *" previous_history "void"
432Back up the current history offset to the previous history entry, and
433return a pointer to that entry. If there is no previous entry, return
434a \fBNULL\fP pointer.
435
436.Fn1 "HIST_ENTRY *" next_history "void"
437Move the current history offset forward to the next history entry, and
438return the a pointer to that entry. If there is no next entry, return
439a \fBNULL\fP pointer.
440
441.SS Searching the History List
442
443These functions allow searching of the history list for entries containing
444a specific string. Searching may be performed both forward and backward
445from the current history position. The search may be \fIanchored\fP,
446meaning that the string must match at the beginning of the history entry.
447
448.Fn2 int history_search "const char *string" "int direction"
449Search the history for \fIstring\fP, starting at the current history offset.
450If \fIdirection\fP is less than 0, then the search is through
451previous entries, otherwise through subsequent entries.
452If \fIstring\fP is found, then
453the current history index is set to that history entry, and the value
454returned is the offset in the line of the entry where
455\fIstring\fP was found. Otherwise, nothing is changed, and a -1 is
456returned.
457
458.Fn2 int history_search_prefix "const char *string" "int direction"
459Search the history for \fIstring\fP, starting at the current history
460offset. The search is anchored: matching lines must begin with
461\fIstring\fP. If \fIdirection\fP is less than 0, then the search is
462through previous entries, otherwise through subsequent entries.
463If \fIstring\fP is found, then the
464current history index is set to that entry, and the return value is 0.
465Otherwise, nothing is changed, and a -1 is returned.
466
467.Fn3 int history_search_pos "const char *string" "int direction" "int pos"
468Search for \fIstring\fP in the history list, starting at \fIpos\fP, an
469absolute index into the list. If \fIdirection\fP is negative, the search
470proceeds backward from \fIpos\fP, otherwise forward. Returns the absolute
471index of the history element where \fIstring\fP was found, or -1 otherwise.
472
473.SS Managing the History File
474The History library can read the history from and write it to a file.
475This section documents the functions for managing a history file.
476
477.Fn1 int read_history "const char *filename"
478Add the contents of \fIfilename\fP to the history list, a line at a time.
479If \fIfilename\fP is \fBNULL\fP, then read from \fI~/.history\fP.
480Returns 0 if successful, or \fBerrno\fP if not.
481
482.Fn3 int read_history_range "const char *filename" "int from" "int to"
483Read a range of lines from \fIfilename\fP, adding them to the history list.
484Start reading at line \fIfrom\fP and end at \fIto\fP.
485If \fIfrom\fP is zero, start at the beginning. If \fIto\fP is less than
486\fIfrom\fP, then read until the end of the file. If \fIfilename\fP is
487\fBNULL\fP, then read from \fI~/.history\fP. Returns 0 if successful,
488or \fBerrno\fP if not.
489
490.Fn1 int write_history "const char *filename"
491Write the current history to \fIfilename\fP, overwriting \fIfilename\fP
492if necessary.
493If \fIfilename\fP is \fBNULL\fP, then write the history list to \fI~/.history\fP.
494Returns 0 on success, or \fBerrno\fP on a read or write error.
495
496
497.Fn2 int append_history "int nelements" "const char *filename"
498Append the last \fInelements\fP of the history list to \fIfilename\fP.
499If \fIfilename\fP is \fBNULL\fP, then append to \fI~/.history\fP.
500Returns 0 on success, or \fBerrno\fP on a read or write error.
501
502.Fn2 int history_truncate_file "const char *filename" "int nlines"
503Truncate the history file \fIfilename\fP, leaving only the last
504\fInlines\fP lines.
505If \fIfilename\fP is \fBNULL\fP, then \fI~/.history\fP is truncated.
506Returns 0 on success, or \fBerrno\fP on failure.
507
508.SS History Expansion
509
510These functions implement history expansion.
511
512.Fn2 int history_expand "char *string" "char **output"
513Expand \fIstring\fP, placing the result into \fIoutput\fP, a pointer
514to a string. Returns:
515.RS
516.PD 0
517.TP
5180
519If no expansions took place (or, if the only change in
520the text was the removal of escape characters preceding the history expansion
521character);
522.TP
5231
524if expansions did take place;
525.TP
526-1
527if there was an error in expansion;
528.TP
5292
530if the returned line should be displayed, but not executed,
531as with the \fB:p\fP modifier.
532.PD
533.RE
534If an error ocurred in expansion, then \fIoutput\fP contains a descriptive
535error message.
536
537.Fn3 "char *" get_history_event "const char *string" "int *cindex" "int qchar"
538Returns the text of the history event beginning at \fIstring\fP +
539\fI*cindex\fP. \fI*cindex\fP is modified to point to after the event
540specifier. At function entry, \fIcindex\fP points to the index into
541\fIstring\fP where the history event specification begins. \fIqchar\fP
542is a character that is allowed to end the event specification in addition
543to the ``normal'' terminating characters.
544
545.Fn1 "char **" history_tokenize "const char *string"
546Return an array of tokens parsed out of \fIstring\fP, much as the
547shell might.
548The tokens are split on the characters in the
549\fBhistory_word_delimiters\fP variable,
550and shell quoting conventions are obeyed.
551
552.Fn3 "char *" history_arg_extract "int first" "int last" "const char *string"
553Extract a string segment consisting of the \fIfirst\fP through \fIlast\fP
554arguments present in \fIstring\fP. Arguments are split using
555\fBhistory_tokenize()\fP.
556
557.SS History Variables
558
559This section describes the externally-visible variables exported by
560the GNU History Library.
561
562.Vb int history_base
563The logical offset of the first entry in the history list.
564
565.Vb int history_length
566The number of entries currently stored in the history list.
567
568.Vb int history_max_entries
569The maximum number of history entries. This must be changed using
570\fBstifle_history()\fP.
571
5bdf8622
DJ
572.Vb int history_write_timestamps
573If non-zero, timestamps are written to the history file, so they can be
574preserved between sessions. The default value is 0, meaning that
575timestamps are not saved.
576
9255ee31
EZ
577.Vb char history_expansion_char
578The character that introduces a history event. The default is \fB!\fP.
579Setting this to 0 inhibits history expansion.
580
581.Vb char history_subst_char
582The character that invokes word substitution if found at the start of
583a line. The default is \fB^\fP.
584
585.Vb char history_comment_char
586During tokenization, if this character is seen as the first character
587of a word, then it and all subsequent characters up to a newline are
588ignored, suppressing history expansion for the remainder of the line.
589This is disabled by default.
590
591.Vb "char *" history_word_delimiters
592The characters that separate tokens for \fBhistory_tokenize()\fP.
593The default value is \fB"\ \et\en()<>;&|"\fP.
594
595.Vb "char *" history_no_expand_chars
596The list of characters which inhibit history expansion if found immediately
597following \fBhistory_expansion_char\fP. The default is space, tab, newline,
598\fB\er\fP, and \fB=\fP.
599
600.Vb "char *" history_search_delimiter_chars
601The list of additional characters which can delimit a history search
602string, in addition to space, tab, \fI:\fP and \fI?\fP in the case of
603a substring search. The default is empty.
604
605.Vb int history_quotes_inhibit_expansion
606If non-zero, single-quoted words are not scanned for the history expansion
607character. The default value is 0.
608
609.Vb "rl_linebuf_func_t *" history_inhibit_expansion_function
610This should be set to the address of a function that takes two arguments:
611a \fBchar *\fP (\fIstring\fP)
612and an \fBint\fP index into that string (\fIi\fP).
613It should return a non-zero value if the history expansion starting at
614\fIstring[i]\fP should not be performed; zero if the expansion should
615be done.
616It is intended for use by applications like \fBbash\fP that use the history
617expansion character for additional purposes.
618By default, this variable is set to \fBNULL\fP.
619.SH FILES
620.PD 0
621.TP
622.FN ~/.history
623Default filename for reading and writing saved history
624.PD
625.SH "SEE ALSO"
626.PD 0
627.TP
628\fIThe Gnu Readline Library\fP, Brian Fox and Chet Ramey
629.TP
630\fIThe Gnu History Library\fP, Brian Fox and Chet Ramey
631.TP
632\fIbash\fP(1)
633.TP
634\fIreadline\fP(3)
635.PD
636.SH AUTHORS
637Brian Fox, Free Software Foundation
638.br
639bfox@gnu.org
640.PP
641Chet Ramey, Case Western Reserve University
642.br
643chet@ins.CWRU.Edu
644.SH BUG REPORTS
645If you find a bug in the
646.B history
647library, you should report it. But first, you should
648make sure that it really is a bug, and that it appears in the latest
649version of the
650.B history
651library that you have.
652.PP
653Once you have determined that a bug actually exists, mail a
654bug report to \fIbug\-readline\fP@\fIgnu.org\fP.
655If you have a fix, you are welcome to mail that
656as well! Suggestions and `philosophical' bug reports may be mailed
657to \fPbug-readline\fP@\fIgnu.org\fP or posted to the Usenet
658newsgroup
659.BR gnu.bash.bug .
660.PP
661Comments and bug reports concerning
662this manual page should be directed to
663.IR chet@ins.CWRU.Edu .