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

FILES

463       ~/.history
464              Default filename for reading and writing saved history
465

SEE ALSO

467       The Gnu Readline Library, Brian Fox and Chet Ramey
468       The Gnu History Library, Brian Fox and Chet Ramey
469       bash(1)
470       readline(3)
471

AUTHORS

473       Brian Fox, Free Software Foundation
474       bfox@gnu.org
475
476       Chet Ramey, Case Western Reserve University
477       chet.ramey@case.edu
478

BUG REPORTS

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