# history

## NAME



           history - GNU History Library
    


## DESCRIPTION



           Many programs read input from the user a line at a time.  The GNU
           History library is able to keep track of those lines, associate
           arbitrary data with each line, and utilize information from
           previous lines when composing new ones.
    
           The History library provides functions that allow applications to
           their _history_ , the set of previously-typed lines, which it keeps
           in a list.  Applications can choose which lines to save into a
           history list, how many commands to save, save a history list to a
           file, read a history list from a file, and display lines from the
           history in various formats.
    


## History Functions



           This section describes the calling sequence for the various
           functions exported by the GNU History library.
    
       **Initializing History and State Management**
           This section describes functions used to initialize and manage the
           state of the History library when you want to use the history
           functions in your program.
    
           _void_ **using_history**(_void_)
           Begin a session in which the history functions might be used.
           This initializes the interactive variables.
    
           _HISTORY_STATE *_ **history_get_history_state**(_void_)
           Return a structure describing the current state of the input
           history.
    
           _void_ **history_set_history_state**(_HISTORY_STATE *state_)
           Set the state of the history list according to _state_.
    
       **History List Management**
           These functions manage individual entries on the history list, or
           set parameters managing the list itself.
    
           _void_ **add_history**(_const char *string_)
           Place _string_ at the end of the history list.  The associated data
           field (if any) is set to **NULL**.  If the maximum number of history
           entries has been set using **stifle_history()** , and the new number of
           history entries would exceed that maximum, the oldest history
           entry is removed.
    
           _void_ **add_history_time**(_const char *string_)
           Change the time stamp associated with the most recent history
           entry to _string_.
    
           _HIST_ENTRY *_ **remove_history**(_int which_)
           Remove history entry at offset _which_ from the history.  The
           removed element is returned so you can free the line, data, and
           containing structure.
    
           _histdata_t_ **free_history_entry**(_HIST_ENTRY *histent_)
           Free the history entry _histent_ and any history library private
           data associated with it.  Returns the application-specific data so
           the caller can dispose of it.
    
           _HIST_ENTRY *_ **replace_history_entry**(_int which, const char *line,_
           _histdata_t data_)
           Make the history entry at offset _which_ have _line_ and _data_.  This
           returns the old entry so the caller can dispose of any
           application-specific data.  In the case of an invalid _which_ , a
           **NULL** pointer is returned.
    
           _void_ **clear_history**(_void_)
           Clear the history list by deleting all the entries.
    
           _void_ **stifle_history**(_int max_)
           Stifle the history list, remembering only the last _max_ entries.
           The history list will contain only _max_ entries at a time.
    
           _int_ **unstifle_history**(_void_)
           Stop stifling the history.  This returns the previously-set
           maximum number of history entries (as set by **stifle_history()**).
           history was stifled.  The value is positive if the history was
           stifled, negative if it wasn't.
    
           _int_ **history_is_stifled**(_void_)
           Returns non-zero if the history is stifled, zero if it is not.
    
       **Information About the History List**
           These functions return information about the entire history list
           or individual list entries.
    
           _HIST_ENTRY **_ **history_list**(_void_)
           Return a **NULL** terminated array of _HIST_ENTRY *_ which is the
           current input history.  Element 0 of this list is the beginning of
           time.  If there is no history, return **NULL**.
    
           _int_ **where_history**(_void_)
           Returns the offset of the current history element.
    
           _HIST_ENTRY *_ **current_history**(_void_)
           Return the history entry at the current position, as determined by
           **where_history()**.  If there is no entry there, return a **NULL**
           pointer.
    
           _HIST_ENTRY *_ **history_get**(_int offset_)
           Return the history entry at position _offset_.  The range of valid
           values of _offset_ starts at **history_base** and ends at **history_length**
           - 1.  If there is no entry there, or if _offset_ is outside the
           valid range, return a **NULL** pointer.
    
           _time_t_ **history_get_time**(_HIST_ENTRY *_)
           Return the time stamp associated with the history entry passed as
           the argument.
    
           _int_ **history_total_bytes**(_void_)
           Return the number of bytes that the primary history entries are
           using.  This function returns the sum of the lengths of all the
           lines in the history.
    
       **Moving Around the History List**
           These functions allow the current index into the history list to
           be set or changed.
    
           _int_ **history_set_pos**(_int pos_)
           Set the current history offset to _pos_ , an absolute index into the
           list.  Returns 1 on success, 0 if _pos_ is less than zero or greater
           than the number of history entries.
    
           _HIST_ENTRY *_ **previous_history**(_void_)
           Back up the current history offset to the previous history entry,
           and return a pointer to that entry.  If there is no previous
           entry, return a **NULL** pointer.
    
           _HIST_ENTRY *_ **next_history**(_void_)
           If the current history offset refers to a valid history entry,
           increment the current history offset.  If the possibly-incremented
           history offset refers to a valid history entry, return a pointer
           to that entry; otherwise, return a **NULL** pointer.
    
       **Searching the History List**
           These functions allow searching of the history list for entries
           containing a specific string.  Searching may be performed both
           forward and backward from the current history position.  The
           search may be _anchored_ , meaning that the string must match at the
           beginning of the history entry.
    
           _int_ **history_search**(_const char *string, int direction_)
           Search the history for _string_ , starting at the current history
           offset.  If _direction_ is less than 0, then the search is through
           previous entries, otherwise through subsequent entries.  If _string_
           is found, then the current history index is set to that history
           entry, and the value returned is the offset in the line of the
           entry where _string_ was found.  Otherwise, nothing is changed, and
           the function returns -1.
    
           _int_ **history_search_prefix**(_const char *string, int direction_)
           Search the history for _string_ , starting at the current history
           offset.  The search is anchored: matching lines must begin with
           _string_.  If _direction_ is less than 0, then the search is through
           previous entries, otherwise through subsequent entries.  If _string_
           is found, then the current history index is set to that entry, and
           the return value is 0.  Otherwise, nothing is changed, and the
           function returns -1.
    
           _int_ **history_search_pos**(_const char *string, int direction, int_
           _pos_)
           Search for _string_ in the history list, starting at _pos_ , an
           absolute index into the list.  If _direction_ is negative, the
           search proceeds backward from _pos_ , otherwise forward.  Returns the
           absolute index of the history element where _string_ was found, or
           -1 otherwise.
    
       **Managing the History File**
           The History library can read the history from and write it to a
           file.  This section documents the functions for managing a history
           file.
    
           _int_ **read_history**(_const char *filename_)
           Add the contents of _filename_ to the history list, a line at a
           time.  If _filename_ is **NULL** , then read from _~/.history_.  Returns 0
           if successful, or **errno** if not.
    
           _int_ **read_history_range**(_const char *filename, int from, int to_)
           Read a range of lines from _filename_ , adding them to the history
           list.  Start reading at line _from_ and end at _to_.  If _from_ is zero,
           start at the beginning.  If _to_ is less than _from_ , then read until
           the end of the file.  If _filename_ is **NULL** , then read from
           _~/.history_.  Returns 0 if successful, or **errno** if not.
    
           _int_ **write_history**(_const char *filename_)
           Write the current history to _filename_ , overwriting _filename_ if
           necessary.  If _filename_ is **NULL** , then write the history list to
           _~/.history_.  Returns 0 on success, or **errno** on a read or write
           error.
    
           _int_ **append_history**(_int nelements, const char *filename_)
           Append the last _nelements_ of the history list to _filename_.  If
           _filename_ is **NULL** , then append to _~/.history_.  Returns 0 on
           success, or **errno** on a read or write error.
    
           _int_ **history_truncate_file**(_const char *filename, int nlines_)
           Truncate the history file _filename_ , leaving only the last _nlines_
           lines.  If _filename_ is **NULL** , then _~/.history_ is truncated.
           Returns 0 on success, or **errno** on failure.
    
       **History Expansion**
           These functions implement history expansion.
    
           _int_ **history_expand**(_const char *string, char **output_)
           Expand _string_ , placing the result into _output_ , a pointer to a
           string.  Returns:
                  0      If no expansions took place (or, if the only change
                         in the text was the removal of escape characters
                         preceding the history expansion character);
                  1      if expansions did take place;
                  -1     if there was an error in expansion;
                  2      if the returned line should be displayed, but not
                         executed, as with the **:p** modifier.
           If an error occurred in expansion, then _output_ contains a
           descriptive error message.
    
           _char *_ **get_history_event**(_const char *string, int *cindex, int_
           _qchar_)
           Returns the text of the history event beginning at _string_ +
           _*cindex_.  _*cindex_ is modified to point to after the event
           specifier.  At function entry, _cindex_ points to the index into
           _string_ where the history event specification begins.  _qchar_ is a
           character that is allowed to end the event specification in
           addition to the “normal” terminating characters.
    
           _char **_ **history_tokenize**(_const char *string_)
           Return an array of tokens parsed out of _string_ , much as the shell
           might.  The tokens are split on the characters in the
           **history_word_delimiters** variable, and shell quoting conventions
           are obeyed.
    
           _char *_ **history_arg_extract**(_int first, int last, const char_
           _*string_)
           Extract a string segment consisting of the _first_ through _last_
           arguments present in _string_.  Arguments are split using
           **history_tokenize()**.
    
       **History Variables**
           This section describes the externally-visible variables exported
           by the GNU History Library.
    
           _int_ **history_base**
           The logical offset of the first entry in the history list.
    
           _int_ **history_length**
           The number of entries currently stored in the history list.
    
           _int_ **history_max_entries**
           The maximum number of history entries.  This must be changed using
           **stifle_history()**.
    
           _int_ **history_write_timestamps**
           If non-zero, timestamps are written to the history file, so they
           can be preserved between sessions.  The default value is 0,
           meaning that timestamps are not saved.  The current timestamp
           format uses the value of _history_comment_char_ to delimit timestamp
           entries in the history file.  If that variable does not have a
           value (the default), the history library will not write
           timestamps.
    
           _char_ **history_expansion_char**
           The character that introduces a history event.  The default is **!**.
           Setting this to 0 inhibits history expansion.
    
           _char_ **history_subst_char**
           The character that invokes word substitution if found at the start
           of a line.  The default is **^**.
    
           _char_ **history_comment_char**
           During tokenization, if this character is seen as the first
           character of a word, then it and all subsequent characters up to a
           newline are ignored, suppressing history expansion for the
           remainder of the line.  This is disabled by default.
    
           _char *_ **history_word_delimiters**
           The characters that separate tokens for **history_tokenize()**.  The
           default value is **" \t\n() <>;&|"**.
    
           _char *_ **history_no_expand_chars**
           The list of characters which inhibit history expansion if found
           immediately following **history_expansion_char**.  The default is
           space, tab, newline, **\r** , and **=**.
    
           _char *_ **history_search_delimiter_chars**
           The list of additional characters which can delimit a history
           search string, in addition to space, tab, _:_ and _?_ in the case of a
           substring search.  The default is empty.
    
           _int_ **history_quotes_inhibit_expansion**
           If non-zero, the history expansion code implements shell-like
           quoting: single-quoted words are not scanned for the history
           expansion character or the history comment character, and double-
           quoted words may have history expansion performed, since single
           quotes are not special within double quotes.  The default value is
           0.
    
           _int_ **history_quoting_state**
           An application may set this variable to indicate that the current
           line being expanded is subject to existing quoting.  If set to _'_ ,
           history expansion assumes that the line is single-quoted and
           inhibit expansion until it reads an unquoted closing single quote;
           if set to _"_ , history expansion assumes the line is double quoted
           until it reads an unquoted closing double quote.  If set to zero,
           the default, history expansion assumes the line is not quoted and
           treats quote characters within the line as described above.  This
           is only effective if **history_quotes_inhibit_expansion** is set.
    
           _rl_linebuf_func_t *_ **history_inhibit_expansion_function**
           This should be set to the address of a function that takes two
           arguments: a **char ***(_string_) and an **int** index into that string
           (_i_).  It should return a non-zero value if the history expansion
           starting at _string[i]_ should not be performed; zero if the
           expansion should be done.  It is intended for use by applications
           like **bash** that use the history expansion character for additional
           purposes.  By default, this variable is set to **NULL**.
    


## FILES



           _~/.history_
                  Default filename for reading and writing saved history
    


## BUG REPORTS



           If you find a bug in the **history** library, you should report it.
           But first, you should make sure that it really is a bug, and that
           it appears in the latest version of the **history** library that you
           have.
    
           Once you have determined that a bug actually exists, mail a bug
           report to _bug-readline_ @_gnu.org_.  If you have a fix, you are
           welcome to mail that as well!  Suggestions and “philosophical” bug
           reports may be mailed to _bug-readline_ @_gnu.org_ or posted to the
           Usenet newsgroup **gnu.bash.bug**.
    
           Comments and bug reports concerning this manual page should be
           directed to _chet.ramey@case.edu_.
    


* * *