1HISTORY(3)                 Library Functions Manual                 HISTORY(3)
2
3
4

NAME

6       history - GNU History Library
7
9       The GNU History Library is Copyright (C) 1989-2011 by the Free Software
10       Foundation, Inc.
11

DESCRIPTION

13       Many programs read input from the user a line at a time.  The GNU  His‐
14       tory  library is able to keep track of those lines, associate arbitrary
15       data with each line, and utilize information  from  previous  lines  in
16       composing new ones.
17

HISTORY EXPANSION

19       The  history library supports a history expansion feature that is iden‐
20       tical to the history expansion in bash.  This  section  describes  what
21       syntax features are available.
22
23       History expansions introduce words from the history list into the input
24       stream, making it easy to repeat commands, insert the  arguments  to  a
25       previous command into the current input line, or fix errors in previous
26       commands quickly.
27
28       History expansion is usually performed  immediately  after  a  complete
29       line  is read.  It takes place in two parts.  The first is to determine
30       which line from the history list to use during substitution.  The  sec‐
31       ond  is  to select portions of that line for inclusion into the current
32       one.  The line selected from the history is the event, and the portions
33       of  that  line  that  are  acted upon are words.  Various modifiers are
34       available to manipulate the selected words.  The line  is  broken  into
35       words in the same fashion as bash does when reading input, so that sev‐
36       eral words that would otherwise be separated are  considered  one  word
37       when  surrounded  by  quotes (see the description of history_tokenize()
38       below).  History expansions are introduced by  the  appearance  of  the
39       history expansion character, which is ! by default.  Only backslash (\)
40       and single quotes can quote the history expansion character.
41
42   Event Designators
43       An event designator is a reference to a command line entry in the  his‐
44       tory  list.   Unless  the reference is absolute, events are relative to
45       the current position in the history list.
46
47       !      Start a history substitution, except when followed by  a  blank,
48              newline, = or (.
49       !n     Refer to command line n.
50       !-n    Refer to the current command minus n.
51       !!     Refer to the previous command.  This is a synonym for `!-1'.
52       !string
53              Refer  to the most recent command preceding the current position
54              in the history list starting with string.
55       !?string[?]
56              Refer to the most recent command preceding the current postition
57              in  the  history  list containing string.  The trailing ? may be
58              omitted if string is followed immediately by a newline.
59       ^string1^string2^
60              Quick substitution.  Repeat the last command, replacing  string1
61              with string2.  Equivalent to ``!!:s/string1/string2/'' (see Mod‐
62              ifiers below).
63       !#     The entire command line typed so far.
64
65   Word Designators
66       Word designators are used to select desired words from the event.  A  :
67       separates  the event specification from the word designator.  It may be
68       omitted if the word designator begins with a ^, $, *, -, or  %.   Words
69       are  numbered from the beginning of the line, with the first word being
70       denoted by 0 (zero).  Words are inserted into the  current  line  sepa‐
71       rated by single spaces.
72
73       0 (zero)
74              The zeroth word.  For the shell, this is the command word.
75       n      The nth word.
76       ^      The first argument.  That is, word 1.
77       $      The last argument.
78       %      The word matched by the most recent `?string?' search.
79       x-y    A range of words; `-y' abbreviates `0-y'.
80       *      All  of  the words but the zeroth.  This is a synonym for `1-$'.
81              It is not an error to use * if there is just  one  word  in  the
82              event; the empty string is returned in that case.
83       x*     Abbreviates x-$.
84       x-     Abbreviates x-$ like x*, but omits the last word.
85
86       If  a  word  designator is supplied without an event specification, the
87       previous command is used as the event.
88
89   Modifiers
90       After the optional word designator, there may appear a sequence of  one
91       or more of the following modifiers, each preceded by a `:'.
92
93       h      Remove a trailing file name component, leaving only the head.
94       t      Remove all leading file name components, leaving the tail.
95       r      Remove a trailing suffix of the form .xxx, leaving the basename.
96       e      Remove all but the trailing suffix.
97       p      Print the new command but do not execute it.
98       q      Quote the substituted words, escaping further substitutions.
99       x      Quote  the  substituted words as with q, but break into words at
100              blanks and newlines.
101       s/old/new/
102              Substitute new for the first occurrence  of  old  in  the  event
103              line.   Any  delimiter  can  be  used  in place of /.  The final
104              delimiter is optional if it is the last character of  the  event
105              line.   The delimiter may be quoted in old and new with a single
106              backslash.  If & appears in new, it is replaced by old.  A  sin‐
107              gle  backslash  will  quote the &.  If old is null, it is set to
108              the last old substituted, or, if no previous  history  substitu‐
109              tions took place, the last string in a !?string[?]  search.
110       &      Repeat the previous substitution.
111       g      Cause changes to be applied over the entire event line.  This is
112              used in conjunction with `:s' (e.g.,  `:gs/old/new/')  or  `:&'.
113              If  used with `:s', any delimiter can be used in place of /, and
114              the final delimiter is optional if it is the last  character  of
115              the event line.  An a may be used as a synonym for g.
116       G      Apply  the following `s' modifier once to each word in the event
117              line.
118

PROGRAMMING WITH HISTORY FUNCTIONS

120       This section describes how to use the History  library  in  other  pro‐
121       grams.
122
123   Introduction to History
124       The  programmer  using  the History library has available functions for
125       remembering lines on a history list, associating arbitrary data with  a
126       line,  removing  lines  from the list, searching through the list for a
127       line containing an arbitrary text string, and referencing any  line  in
128       the list directly.  In addition, a history expansion function is avail‐
129       able which provides for a consistent user  interface  across  different
130       programs.
131
132       The  user using programs written with the History library has the bene‐
133       fit of a consistent user interface with a set  of  well-known  commands
134       for  manipulating the text of previous lines and using that text in new
135       commands.  The basic history manipulation commands are identical to the
136       history substitution provided by bash.
137
138       If  the  programmer  desires,  he  can  use the Readline library, which
139       includes some history manipulation by default, and has the added advan‐
140       tage of command line editing.
141
142       Before  declaring  any  functions  using  any functionality the History
143       library provides in other code, an application  writer  should  include
144       the  file  <readline/history.h>  in  any  file  that  uses  the History
145       library's features.  It supplies extern declarations  for  all  of  the
146       library's  public functions and variables, and declares all of the pub‐
147       lic data structures.
148
149
150   History Storage
151       The history list is an array of history entries.  A  history  entry  is
152       declared as follows:
153
154       typedef void * histdata_t;
155
156       typedef struct _hist_entry {
157         char *line;
158         char *timestamp;
159         histdata_t data;
160       } HIST_ENTRY;
161
162       The history list itself might therefore be declared as
163
164       HIST_ENTRY ** the_history_list;
165
166       The  state  of the History library is encapsulated into a single struc‐
167       ture:
168
169       /*
170        * A structure used to pass around the current state of the history.
171        */
172       typedef struct _hist_state {
173         HIST_ENTRY **entries; /* Pointer to the entries themselves. */
174         int offset;           /* The location pointer within this array. */
175         int length;           /* Number of elements within this array. */
176         int size;             /* Number of slots allocated to this array. */
177         int flags;
178       } HISTORY_STATE;
179
180       If the flags member includes HS_STIFLED, the history has been stifled.
181

History Functions

183       This section describes the calling sequence for the  various  functions
184       exported by the GNU History library.
185
186   Initializing History and State Management
187       This  section  describes  functions  used  to initialize and manage the
188       state of the History library when you want to use the history functions
189       in your program.
190
191       void using_history (void)
192       Begin  a  session  in  which the history functions might be used.  This
193       initializes the interactive variables.
194
195       HISTORY_STATE * history_get_history_state (void)
196       Return a structure describing the current state of the input history.
197
198       void history_set_history_state (HISTORY_STATE *state)
199       Set the state of the history list according to state.
200
201
202   History List Management
203       These functions manage individual entries on the history list,  or  set
204       parameters managing the list itself.
205
206       void add_history (const char *string)
207       Place string at the end of the history list.  The associated data field
208       (if any) is set to NULL.
209
210       void add_history_time (const char *string)
211       Change the time stamp associated with the most recent history entry  to
212       string.
213
214       HIST_ENTRY * remove_history (int which)
215       Remove  history  entry  at  offset which from the history.  The removed
216       element is returned so you can free  the  line,  data,  and  containing
217       structure.
218
219       histdata_t free_history_entry (HIST_ENTRY *histent)
220       Free  the  history  entry  histent and any history library private data
221       associated with it.  Returns the application-specific data so the call‐
222       er can dispose of it.
223
224       HIST_ENTRY  * replace_history_entry (int which, const char *line, hist‐
225       data_t data)
226       Make the history entry at  offset  which  have  line  and  data.   This
227       returns the old entry so the caller can dispose of any application-spe‐
228       cific data.  In the case  of  an  invalid  which,  a  NULL  pointer  is
229       returned.
230
231       void clear_history (void)
232       Clear the history list by deleting all the entries.
233
234       void stifle_history (int max)
235       Stifle the history list, remembering only the last max entries.
236
237       int unstifle_history (void)
238       Stop  stifling  the  history.   This returns the previously-set maximum
239       number of history entries (as set by  stifle_history()).   history  was
240       stifled.  The value is positive if the history was stifled, negative if
241       it wasn't.
242
243       int history_is_stifled (void)
244       Returns non-zero if the history is stifled, zero if it is not.
245
246
247   Information About the History List
248       These functions return information about the  entire  history  list  or
249       individual list entries.
250
251       HIST_ENTRY ** history_list (void)
252       Return  a  NULL  terminated  array of HIST_ENTRY * which is the current
253       input history.  Element 0 of this list is the beginning  of  time.   If
254       there is no history, return NULL.
255
256       int where_history (void)
257       Returns the offset of the current history element.
258
259       HIST_ENTRY * current_history (void)
260       Return  the  history  entry  at  the current position, as determined by
261       where_history().  If there is no entry there, return a NULL pointer.
262
263       HIST_ENTRY * history_get (int offset)
264       Return the  history  entry  at  position  offset,  starting  from  his‐
265       tory_base.   If  there  is no entry there, or if offset is greater than
266       the history length, return a NULL pointer.
267
268       time_t history_get_time (HIST_ENTRY *)
269       Return the time stamp associated with the history entry passed  as  the
270       argument.
271
272       int history_total_bytes (void)
273       Return  the number of bytes that the primary history entries are using.
274       This function returns the sum of the lengths of all the  lines  in  the
275       history.
276
277
278   Moving Around the History List
279       These functions allow the current index into the history list to be set
280       or changed.
281
282       int history_set_pos (int pos)
283       Set the current history offset to pos, an absolute index into the list.
284       Returns  1  on  success, 0 if pos is less than zero or greater than the
285       number of history entries.
286
287       HIST_ENTRY * previous_history (void)
288       Back up the current history offset to the previous history  entry,  and
289       return  a pointer to that entry.  If there is no previous entry, return
290       a NULL pointer.
291
292       HIST_ENTRY * next_history (void)
293       Move the current history offset forward to the next history entry,  and
294       return  the a pointer to that entry.  If there is no next entry, return
295       a NULL pointer.
296
297
298   Searching the History List
299       These functions allow searching of the history list  for  entries  con‐
300       taining a specific string.  Searching may be performed both forward and
301       backward  from  the  current  history  position.   The  search  may  be
302       anchored,  meaning  that  the string must match at the beginning of the
303       history entry.
304
305       int history_search (const char *string, int direction)
306       Search the history for string, starting at the current history  offset.
307       If  direction  is  less  than  0,  then  the search is through previous
308       entries, otherwise through subsequent entries.   If  string  is  found,
309       then  the  current  history index is set to that history entry, and the
310       value returned is the offset in the line of the entry where string  was
311       found.  Otherwise, nothing is changed, and a -1 is returned.
312
313       int history_search_prefix (const char *string, int direction)
314       Search  the history for string, starting at the current history offset.
315       The search is anchored: matching lines  must  begin  with  string.   If
316       direction  is less than 0, then the search is through previous entries,
317       otherwise through subsequent entries.  If string  is  found,  then  the
318       current  history index is set to that entry, and the return value is 0.
319       Otherwise, nothing is changed, and a -1 is returned.
320
321       int history_search_pos (const char *string, int direction, int pos)
322       Search for string in the history list, starting  at  pos,  an  absolute
323       index  into  the  list.   If direction is negative, the search proceeds
324       backward from pos, otherwise forward.  Returns the  absolute  index  of
325       the history element where string was found, or -1 otherwise.
326
327
328   Managing the History File
329       The  History  library can read the history from and write it to a file.
330       This section documents the functions for managing a history file.
331
332       int read_history (const char *filename)
333       Add the contents of filename to the history list, a line at a time.  If
334       filename  is NULL, then read from ~/.history.  Returns 0 if successful,
335       or errno if not.
336
337       int read_history_range (const char *filename, int from, int to)
338       Read a range of lines from filename, adding them to the  history  list.
339       Start  reading  at  line from and end at to.  If from is zero, start at
340       the beginning.  If to is less than from, then read until the end of the
341       file.   If  filename  is NULL, then read from ~/.history.  Returns 0 if
342       successful, or errno if not.
343
344       int write_history (const char *filename)
345       Write the current history to filename, overwriting filename  if  neces‐
346       sary.   If filename is NULL, then write the history list to ~/.history.
347       Returns 0 on success, or errno on a read or write error.
348
349
350       int append_history (int nelements, const char *filename)
351       Append the last nelements of the history list to filename.  If filename
352       is  NULL, then append to ~/.history.  Returns 0 on success, or errno on
353       a read or write error.
354
355       int history_truncate_file (const char *filename, int nlines)
356       Truncate the history file filename, leaving only the last nlines lines.
357       If  filename  is NULL, then ~/.history is truncated.  Returns 0 on suc‐
358       cess, or errno on failure.
359
360
361   History Expansion
362       These functions implement history expansion.
363
364       int history_expand (char *string, char **output)
365       Expand string, placing the result into output, a pointer to  a  string.
366       Returns:
367              0      If  no  expansions  took place (or, if the only change in
368                     the text was the removal of escape  characters  preceding
369                     the history expansion character);
370              1      if expansions did take place;
371              -1     if there was an error in expansion;
372              2      if  the  returned  line should be displayed, but not exe‐
373                     cuted, as with the :p modifier.
374       If an error ocurred in expansion, then output  contains  a  descriptive
375       error message.
376
377       char * get_history_event (const char *string, int *cindex, int qchar)
378       Returns  the  text  of the history event beginning at string + *cindex.
379       *cindex is modified to point to after the event specifier.  At function
380       entry,  cindex  points to the index into string where the history event
381       specification begins.  qchar is a character that is allowed to end  the
382       event  specification  in addition to the ``normal'' terminating charac‐
383       ters.
384
385       char ** history_tokenize (const char *string)
386       Return an array of tokens parsed out  of  string,  much  as  the  shell
387       might.    The   tokens   are  split  on  the  characters  in  the  his‐
388       tory_word_delimiters  variable,  and  shell  quoting  conventions   are
389       obeyed.
390
391       char * history_arg_extract (int first, int last, const char *string)
392       Extract a string segment consisting of the first through last arguments
393       present in string.  Arguments are split using history_tokenize().
394
395
396   History Variables
397       This section describes the externally-visible variables exported by the
398       GNU History Library.
399
400       int history_base
401       The logical offset of the first entry in the history list.
402
403       int history_length
404       The number of entries currently stored in the history list.
405
406       int history_max_entries
407       The maximum number of history entries.  This must be changed using sti‐
408       fle_history().
409
410       int history_wite_timestamps
411       If non-zero, timestamps are written to the history file, so they can be
412       preserved between sessions.  The default value is 0, meaning that time‐
413       stamps are not saved.  The current timestamp format uses the  value  of
414       history_comment_char  to delimit timestamp entries in the history file.
415       If that variable does not have a value (the default),  timestamps  will
416       not be written.
417
418       char history_expansion_char
419       The character that introduces a history event.  The default is !.  Set‐
420       ting this to 0 inhibits history expansion.
421
422       char history_subst_char
423       The character that invokes word substitution if found at the start of a
424       line.  The default is ^.
425
426       char history_comment_char
427       During  tokenization,  if this character is seen as the first character
428       of a word, then it and all subsequent characters up to  a  newline  are
429       ignored,  suppressing  history expansion for the remainder of the line.
430       This is disabled by default.
431
432       char * history_word_delimiters
433       The  characters  that  separate  tokens  for  history_tokenize().   The
434       default value is " \t\n()<>;&|".
435
436       char * history_no_expand_chars
437       The list of characters which inhibit history expansion if found immedi‐
438       ately following history_expansion_char.  The  default  is  space,  tab,
439       newline, \r, and =.
440
441       char * history_search_delimiter_chars
442       The  list  of  additional characters which can delimit a history search
443       string, in addition to space, tab, : and ? in the case of  a  substring
444       search.  The default is empty.
445
446       int history_quotes_inhibit_expansion
447       If non-zero, single-quoted words are not scanned for the history expan‐
448       sion character.  The default value is 0.
449
450       rl_linebuf_func_t * history_inhibit_expansion_function
451       This should be set to the address of a function that  takes  two  argu‐
452       ments:  a  char  *  (string) and an int index into that string (i).  It
453       should return a non-zero value if the  history  expansion  starting  at
454       string[i]  should  not  be  performed;  zero if the expansion should be
455       done.  It is intended for use by applications like bash  that  use  the
456       history  expansion character for additional purposes.  By default, this
457       variable is set to NULL.
458

FILES

460       ~/.history
461              Default filename for reading and writing saved history
462

SEE ALSO

464       The Gnu Readline Library, Brian Fox and Chet Ramey
465       The Gnu History Library, Brian Fox and Chet Ramey
466       bash(1)
467       readline(3)
468

AUTHORS

470       Brian Fox, Free Software Foundation
471       bfox@gnu.org
472
473       Chet Ramey, Case Western Reserve University
474       chet@ins.CWRU.Edu
475

BUG REPORTS

477       If you find a bug in the history library, you should  report  it.   But
478       first,  you  should  make  sure  that  it  really is a bug, and that it
479       appears in the latest version of the history library that you have.
480
481       Once you have determined that a bug actually exists, mail a bug  report
482       to  bug-readline@gnu.org.   If  you have a fix, you are welcome to mail
483       that as well!  Suggestions  and  `philosophical'  bug  reports  may  be
484       mailed  to  bug-readline@gnu.org  or  posted  to  the  Usenet newsgroup
485       gnu.bash.bug.
486
487       Comments and bug reports concerning this manual page should be directed
488       to chet@ins.CWRU.Edu.
489
490
491
492GNU History 6.2                 2010 August 12                      HISTORY(3)
Impressum