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-2017 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  position
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  word.   This  is  usually the last argument, but will
78              expand to the zeroth word if there is only one word in the line.
79       %      The word matched by the most recent `?string?' search.
80       x-y    A range of words; `-y' abbreviates `0-y'.
81       *      All of the words but the zeroth.  This is a synonym  for  `1-$'.
82              It  is  not  an  error to use * if there is just one word in the
83              event; the empty string is returned in that case.
84       x*     Abbreviates x-$.
85       x-     Abbreviates x-$ like x*, but omits the last word.
86
87       If a word designator is supplied without an  event  specification,  the
88       previous command is used as the event.
89
90   Modifiers
91       After  the optional word designator, there may appear a sequence of one
92       or more of the following modifiers, each preceded by a `:'.
93
94       h      Remove a trailing file name component, leaving only the head.
95       t      Remove all leading file name components, leaving the tail.
96       r      Remove a trailing suffix of the form .xxx, leaving the basename.
97       e      Remove all but the trailing suffix.
98       p      Print the new command but do not execute it.
99       q      Quote the substituted words, escaping further substitutions.
100       x      Quote the substituted words as with q, but break into  words  at
101              blanks and newlines.
102       s/old/new/
103              Substitute  new  for  the  first  occurrence of old in the event
104              line.  Any delimiter can be used  in  place  of  /.   The  final
105              delimiter  is  optional if it is the last character of the event
106              line.  The delimiter may be quoted in old and new with a  single
107              backslash.   If & appears in new, it is replaced by old.  A sin‐
108              gle backslash will quote the &.  If old is null, it  is  set  to
109              the  last  old substituted, or, if no previous history substitu‐
110              tions took place, the last string in a !?string[?]  search.
111       &      Repeat the previous substitution.
112       g      Cause changes to be applied over the entire event line.  This is
113              used  in  conjunction  with `:s' (e.g., `:gs/old/new/') or `:&'.
114              If used with `:s', any delimiter can be used in place of /,  and
115              the  final  delimiter is optional if it is the last character of
116              the event line.  An a may be used as a synonym for g.
117       G      Apply the following `s' modifier once to each word in the  event
118              line.
119

PROGRAMMING WITH HISTORY FUNCTIONS

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

History Functions

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

FILES

467       ~/.history
468              Default filename for reading and writing saved history
469

SEE ALSO

471       The Gnu Readline Library, Brian Fox and Chet Ramey
472       The Gnu History Library, Brian Fox and Chet Ramey
473       bash(1)
474       readline(3)
475

AUTHORS

477       Brian Fox, Free Software Foundation
478       bfox@gnu.org
479
480       Chet Ramey, Case Western Reserve University
481       chet.ramey@case.edu
482

BUG REPORTS

484       If  you  find  a bug in the history library, you should report it.  But
485       first, you should make sure that it  really  is  a  bug,  and  that  it
486       appears in the latest version of the history library that you have.
487
488       Once  you have determined that a bug actually exists, mail a bug report
489       to bug-readline@gnu.org.  If you have a fix, you are  welcome  to  mail
490       that  as  well!   Suggestions  and  `philosophical'  bug reports may be
491       mailed to  bug-readline@gnu.org  or  posted  to  the  Usenet  newsgroup
492       gnu.bash.bug.
493
494       Comments and bug reports concerning this manual page should be directed
495       to chet.ramey@case.edu.
496
497
498
499GNU History 6.3                 2017 October 8                      HISTORY(3)
Impressum