{
    "mode": "man",
    "parameter": "history",
    "section": "3readline",
    "url": "https://www.chedong.com/phpMan.php/man/history/3readline/json",
    "generated": "2026-10-09T16:52:40Z",
    "sections": {
        "NAME": {
            "content": "history - GNU History Library\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "The GNU History Library is Copyright (C) 1989-2020 by the Free Software Foundation, Inc.\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Many  programs read input from the user a line at a time.  The GNU History library is able to\nkeep track of those lines, associate arbitrary data with each line, and  utilize  information\nfrom previous lines in composing new ones.\n",
            "subsections": []
        },
        "HISTORY EXPANSION": {
            "content": "The history library supports a history expansion feature that is identical to the history ex‐\npansion in bash.  This section describes what syntax features are available.\n\nHistory  expansions  introduce  words  from the history list into the input stream, making it\neasy to repeat commands, insert the arguments to a previous command into  the  current  input\nline, or fix errors in previous commands quickly.\n\nHistory  expansion  is usually performed immediately after a complete line is read.  It takes\nplace in two parts.  The first is to determine which line from the history list to use during\nsubstitution.  The second is to select portions of that line for inclusion into  the  current\none.  The line selected from the history is the event, and the portions of that line that are\nacted upon are words.  Various modifiers are available to manipulate the selected words.  The\nline  is  broken into words in the same fashion as bash does when reading input, so that sev‐\neral words that would otherwise be separated are  considered  one  word  when  surrounded  by\nquotes  (see the description of historytokenize() below).  History expansions are introduced\nby the appearance of the history expansion character, which is ! by default.  Only  backslash\n(\\) and single quotes can quote the history expansion character.\n",
            "subsections": [
                {
                    "name": "Event Designators",
                    "content": "An  event  designator is a reference to a command line entry in the history list.  Unless the\nreference is absolute, events are relative to the current position in the history list.\n\n!      Start a history substitution, except when followed by a blank, newline, = or (.\n!n     Refer to command line n.\n!-n    Refer to the current command minus n.\n!!     Refer to the previous command.  This is a synonym for `!-1'.\n!string\nRefer to the most recent command preceding the current position in  the  history  list\nstarting with string.\n!?string[?]\nRefer  to  the  most recent command preceding the current position in the history list\ncontaining string.  The trailing ? may be omitted if string is followed immediately by\na newline.  If string is missing, the string from the most recent search is  used;  it\nis an error if there is no previous search string.\n^string1^string2^\nQuick substitution.  Repeat the last command, replacing string1 with string2.  Equiva‐\nlent to ``!!:s^string1^string2^'' (see Modifiers below).\n!#     The entire command line typed so far.\n"
                },
                {
                    "name": "Word Designators",
                    "content": "Word  designators  are  used to select desired words from the event.  A : separates the event\nspecification from the word designator.  It may be omitted if the word designator begins with\na ^, $, *, -, or %.  Words are numbered from the beginning of the line, with the  first  word\nbeing  denoted  by  0  (zero).   Words are inserted into the current line separated by single\nspaces.\n"
                },
                {
                    "name": "0 (zero)",
                    "content": "The zeroth word.  For the shell, this is the command word.\nn      The nth word.\n^      The first argument.  That is, word 1.\n$      The last word.  This is usually the last argument, but will expand to the zeroth  word\nif there is only one word in the line.\n%      The  first word matched by the most recent `?string?' search, if the search string be‐\ngins with a character that is part of a word.\nx-y    A range of words; `-y' abbreviates `0-y'.\n*      All of the words but the zeroth.  This is a synonym for `1-$'.  It is not an error  to\nuse  *  if  there  is just one word in the event; the empty string is returned in that\ncase.\nx*     Abbreviates x-$.\nx-     Abbreviates x-$ like x*, but omits the last word.  If x is missing, it defaults to 0.\n\nIf a word designator is supplied without an event specification, the previous command is used\nas the event.\n"
                },
                {
                    "name": "Modifiers",
                    "content": "After the optional word designator, there may appear a sequence of one or more of the follow‐\ning modifiers, each preceded by a `:'.  These modify, or edit, the  word  or  words  selected\nfrom the history event.\n\nh      Remove a trailing file name component, leaving only the head.\nt      Remove all leading file name components, leaving the tail.\nr      Remove a trailing suffix of the form .xxx, leaving the basename.\ne      Remove all but the trailing suffix.\np      Print the new command but do not execute it.\nq      Quote the substituted words, escaping further substitutions.\nx      Quote  the  substituted  words as with q, but break into words at blanks and newlines.\nThe q and x modifiers are mutually exclusive; the last one supplied is used.\ns/old/new/\nSubstitute new for the first occurrence of old in the event line.  Any  character  may\nbe  used as the delimiter in place of /.  The final delimiter is optional if it is the\nlast character of the event line.  The delimiter may be quoted in old and new  with  a\nsingle  backslash.   If  &  appears in new, it is replaced by old.  A single backslash\nwill quote the &.  If old is null, it is set to the last old substituted,  or,  if  no\nprevious  history  substitutions took place, the last string in a !?string[?]  search.\nIf new is null, each matching old is deleted.\n&      Repeat the previous substitution.\ng      Cause changes to be applied over the entire event line.  This is used  in  conjunction\nwith  `:s'  (e.g.,  `:gs/old/new/')  or `:&'.  If used with `:s', any delimiter can be\nused in place of /, and the final delimiter is optional if it is the last character of\nthe event line.  An a may be used as a synonym for g.\nG      Apply the following `s' or `&' modifier once to each word in the event line.\n"
                }
            ]
        },
        "PROGRAMMING WITH HISTORY FUNCTIONS": {
            "content": "This section describes how to use the History library in other programs.\n",
            "subsections": [
                {
                    "name": "Introduction to History",
                    "content": "A programmer using the History library has available functions for  remembering  lines  on  a\nhistory list, associating arbitrary data with a line, removing lines from the list, searching\nthrough  the list for a line containing an arbitrary text string, and referencing any line in\nthe list directly.  In addition, a history expansion function is available which provides for\na consistent user interface across different programs.\n\nThe user using programs written with the History library has the benefit of a consistent user\ninterface with a set of well-known commands for manipulating the text of previous  lines  and\nusing  that  text  in new commands.  The basic history manipulation commands are identical to\nthe history substitution provided by bash.\n\nThe programmer can also use the readline library, which includes some history manipulation by\ndefault, and has the added advantage of command line editing.\n\nBefore declaring any functions using any functionality the History library provides in  other\ncode,  an  application  writer  should include the file <readline/history.h> in any file that\nuses the History library's features.  It supplies extern declarations  for  all  of  the  li‐\nbrary's public functions and variables, and declares all of the public data structures.\n"
                },
                {
                    "name": "History Storage",
                    "content": "The history list is an array of history entries.  A history entry is declared as follows:\n\ntypedef void * histdatat;\n\ntypedef struct histentry {\nchar *line;\nchar *timestamp;\nhistdatat data;\n} HISTENTRY;\n\nThe history list itself might therefore be declared as\n\nHISTENTRY  thehistorylist;\n\nThe state of the History library is encapsulated into a single structure:\n\n/*\n* A structure used to pass around the current state of the history.\n*/\ntypedef struct histstate {\nHISTENTRY entries; /* Pointer to the entries themselves. */\nint offset;           /* The location pointer within this array. */\nint length;           /* Number of elements within this array. */\nint size;             /* Number of slots allocated to this array. */\nint flags;\n} HISTORYSTATE;\n\nIf the flags member includes HSSTIFLED, the history has been stifled.\n"
                }
            ]
        },
        "History Functions": {
            "content": "This  section  describes  the  calling sequence for the various functions exported by the GNU\nHistory library.\n",
            "subsections": [
                {
                    "name": "Initializing History and State Management",
                    "content": "This section describes functions used to initialize and manage the state of the  History  li‐\nbrary when you want to use the history functions in your program.\n\nvoid usinghistory (void)\nBegin  a session in which the history functions might be used.  This initializes the interac‐\ntive variables.\n\nHISTORYSTATE * historygethistorystate (void)\nReturn a structure describing the current state of the input history.\n\nvoid historysethistorystate (HISTORYSTATE *state)\nSet the state of the history list according to state.\n\n"
                },
                {
                    "name": "History List Management",
                    "content": "These functions manage individual entries on the history list, or set parameters managing the\nlist itself.\n\nvoid addhistory (const char *string)\nPlace string at the end of the history list.  The associated data field (if any)  is  set  to\nNULL.   If the maximum number of history entries has been set using stiflehistory(), and the\nnew number of history entries would exceed that maximum, the oldest history entry is removed.\n\nvoid addhistorytime (const char *string)\nChange the time stamp associated with the most recent history entry to string.\n\nHISTENTRY * removehistory (int which)\nRemove history entry at offset which from the history.  The removed element  is  returned  so\nyou can free the line, data, and containing structure.\n\nhistdatat freehistoryentry (HISTENTRY *histent)\nFree  the history entry histent and any history library private data associated with it.  Re‐\nturns the application-specific data so the caller can dispose of it.\n\nHISTENTRY * replacehistoryentry (int which, const char *line, histdatat data)\nMake the history entry at offset which have line and data.  This returns the old entry so the\ncaller can dispose of any application-specific data.  In the case of an invalid which, a NULL\npointer is returned.\n\nvoid clearhistory (void)\nClear the history list by deleting all the entries.\n\nvoid stiflehistory (int max)\nStifle the history list, remembering only the last max entries.  The history list  will  con‐\ntain only max entries at a time.\n\nint unstiflehistory (void)\nStop stifling the history.  This returns the previously-set maximum number of history entries\n(as set by stiflehistory()).  history was stifled.  The value is positive if the history was\nstifled, negative if it wasn't.\n\nint historyisstifled (void)\nReturns non-zero if the history is stifled, zero if it is not.\n\n"
                },
                {
                    "name": "Information About the History List",
                    "content": "These functions return information about the entire history list or individual list entries.\n\nHISTENTRY  historylist (void)\nReturn a NULL terminated array of HISTENTRY * which is the current input history.  Element 0\nof this list is the beginning of time.  If there is no history, return NULL.\n\nint wherehistory (void)\nReturns the offset of the current history element.\n\nHISTENTRY * currenthistory (void)\nReturn the history entry at the current position, as determined by wherehistory().  If there\nis no entry there, return a NULL pointer.\n\nHISTENTRY * historyget (int offset)\nReturn  the  history entry at position offset.  The range of valid values of offset starts at\nhistorybase and ends at historylength - 1.  If there is no entry there,  or  if  offset  is\noutside the valid range, return a NULL pointer.\n\ntimet historygettime (HISTENTRY *)\nReturn the time stamp associated with the history entry passed as the argument.\n\nint historytotalbytes (void)\nReturn the number of bytes that the primary history entries are using.  This function returns\nthe sum of the lengths of all the lines in the history.\n\n"
                },
                {
                    "name": "Moving Around the History List",
                    "content": "These functions allow the current index into the history list to be set or changed.\n\nint historysetpos (int pos)\nSet  the  current  history offset to pos, an absolute index into the list.  Returns 1 on suc‐\ncess, 0 if pos is less than zero or greater than the number of history entries.\n\nHISTENTRY * previoushistory (void)\nBack up the current history offset to the previous history entry, and  return  a  pointer  to\nthat entry.  If there is no previous entry, return a NULL pointer.\n\nHISTENTRY * nexthistory (void)\nIf  the current history offset refers to a valid history entry, increment the current history\noffset.  If the possibly-incremented history offset refers to a valid history entry, return a\npointer to that entry; otherwise, return a NULL pointer.\n\n"
                },
                {
                    "name": "Searching the History List",
                    "content": "These functions allow searching of the history list for entries containing a specific string.\nSearching may be performed both forward and backward from the current history position.   The\nsearch  may  be  anchored, meaning that the string must match at the beginning of the history\nentry.\n\nint historysearch (const char *string, int direction)\nSearch the history for string, starting at the current history offset.  If direction is  less\nthan  0,  then  the search is through previous entries, otherwise through subsequent entries.\nIf string is found, then the current history index is set to  that  history  entry,  and  the\nvalue  returned  is  the  offset in the line of the entry where string was found.  Otherwise,\nnothing is changed, and a -1 is returned.\n\nint historysearchprefix (const char *string, int direction)\nSearch the history for string, starting at the current history offset.   The  search  is  an‐\nchored:  matching lines must begin with string.  If direction is less than 0, then the search\nis through previous entries, otherwise through subsequent entries.  If string is found,  then\nthe  current history index is set to that entry, and the return value is 0.  Otherwise, noth‐\ning is changed, and a -1 is returned.\n\nint historysearchpos (const char *string, int direction, int pos)\nSearch for string in the history list, starting at pos, an absolute index into the list.   If\ndirection is negative, the search proceeds backward from pos, otherwise forward.  Returns the\nabsolute index of the history element where string was found, or -1 otherwise.\n\n"
                },
                {
                    "name": "Managing the History File",
                    "content": "The History library can read the history from and write it to a file.  This section documents\nthe functions for managing a history file.\n\nint readhistory (const char *filename)\nAdd  the  contents  of  filename to the history list, a line at a time.  If filename is NULL,\nthen read from ~/.history.  Returns 0 if successful, or errno if not.\n\nint readhistoryrange (const char *filename, int from, int to)\nRead a range of lines from filename, adding them to the history list.  Start reading at  line\nfrom  and end at to.  If from is zero, start at the beginning.  If to is less than from, then\nread until the end of the file.  If filename is NULL, then read from ~/.history.   Returns  0\nif successful, or errno if not.\n\nint writehistory (const char *filename)\nWrite  the  current  history  to filename, overwriting filename if necessary.  If filename is\nNULL, then write the history list to ~/.history.  Returns 0 on success, or errno on a read or\nwrite error.\n\n\nint appendhistory (int nelements, const char *filename)\nAppend the last nelements of the history list to filename.  If filename is NULL, then  append\nto ~/.history.  Returns 0 on success, or errno on a read or write error.\n\nint historytruncatefile (const char *filename, int nlines)\nTruncate the history file filename, leaving only the last nlines lines.  If filename is NULL,\nthen ~/.history is truncated.  Returns 0 on success, or errno on failure.\n\n"
                },
                {
                    "name": "History Expansion",
                    "content": "These functions implement history expansion.\n\nint historyexpand (char *string, char output)\nExpand string, placing the result into output, a pointer to a string.  Returns:\n0      If no expansions took place (or, if the only change in the text was the removal\nof escape characters preceding the history expansion character);\n1      if expansions did take place;\n-1     if there was an error in expansion;\n2      if the returned line should be displayed, but not executed, as with the :p mod‐\nifier.\nIf an error occurred in expansion, then output contains a descriptive error message.\n\nchar * gethistoryevent (const char *string, int *cindex, int qchar)\nReturns  the text of the history event beginning at string + *cindex.  *cindex is modified to\npoint to after the event specifier.  At function entry,  cindex  points  to  the  index  into\nstring where the history event specification begins.  qchar is a character that is allowed to\nend the event specification in addition to the ``normal'' terminating characters.\n\nchar  historytokenize (const char *string)\nReturn  an  array  of  tokens  parsed out of string, much as the shell might.  The tokens are\nsplit on the characters in the historyworddelimiters variable, and  shell  quoting  conven‐\ntions are obeyed.\n\nchar * historyargextract (int first, int last, const char *string)\nExtract  a  string  segment consisting of the first through last arguments present in string.\nArguments are split using historytokenize().\n\n"
                },
                {
                    "name": "History Variables",
                    "content": "This section describes the externally-visible variables exported by the GNU History Library.\n\nint historybase\nThe logical offset of the first entry in the history list.\n\nint historylength\nThe number of entries currently stored in the history list.\n\nint historymaxentries\nThe maximum number of history entries.  This must be changed using stiflehistory().\n\nint historywritetimestamps\nIf non-zero, timestamps are written to the history file, so they  can  be  preserved  between\nsessions.   The default value is 0, meaning that timestamps are not saved.  The current time‐\nstamp format uses the value of historycommentchar to delimit timestamp entries in the  his‐\ntory  file.   If  that  variable  does not have a value (the default), timestamps will not be\nwritten.\n\nchar historyexpansionchar\nThe character that introduces a history event.  The default is !.  Setting this to 0 inhibits\nhistory expansion.\n\nchar historysubstchar\nThe character that invokes word substitution if found at the start of a line.  The default is\n^.\n\nchar historycommentchar\nDuring tokenization, if this character is seen as the first character of a word, then it  and\nall  subsequent characters up to a newline are ignored, suppressing history expansion for the\nremainder of the line.  This is disabled by default.\n\nchar * historyworddelimiters\nThe  characters  that  separate  tokens  for  historytokenize().   The  default   value   is\n\" \\t\\n()<>;&|\".\n\nchar * historynoexpandchars\nThe  list  of  characters which inhibit history expansion if found immediately following his‐\ntoryexpansionchar.  The default is space, tab, newline, \\r, and =.\n\nchar * historysearchdelimiterchars\nThe list of additional characters which can delimit a history search string, in  addition  to\nspace, tab, : and ? in the case of a substring search.  The default is empty.\n\nint historyquotesinhibitexpansion\nIf  non-zero,  double-quoted words are not scanned for the history expansion character or the\nhistory comment character.  The default value is 0.\n\nrllinebuffunct * historyinhibitexpansionfunction\nThis should be set to the address of a function that takes two arguments: a char  *  (string)\nand  an int index into that string (i).  It should return a non-zero value if the history ex‐\npansion starting at string[i] should not be performed; zero if the expansion should be  done.\nIt is intended for use by applications like bash that use the history expansion character for\nadditional purposes.  By default, this variable is set to NULL.\n"
                }
            ]
        },
        "FILES": {
            "content": "~/.history\nDefault filename for reading and writing saved history\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "The Gnu Readline Library, Brian Fox and Chet Ramey\nThe Gnu History Library, Brian Fox and Chet Ramey\nbash(1)\nreadline(3)\n",
            "subsections": []
        },
        "AUTHORS": {
            "content": "Brian Fox, Free Software Foundation\nbfox@gnu.org\n\nChet Ramey, Case Western Reserve University\nchet.ramey@case.edu\n",
            "subsections": []
        },
        "BUG REPORTS": {
            "content": "If  you  find a bug in the history library, you should report it.  But first, you should make\nsure that it really is a bug, and that it appears in the latest version of  the  history  li‐\nbrary that you have.\n\nOnce  you  have  determined  that  a  bug  actually  exists,  mail  a bug report to bug-read‐\nline@gnu.org.  If you have a fix, you are welcome to mail  that  as  well!   Suggestions  and\n`philosophical'  bug  reports  may  be mailed to bug-readline@gnu.org or posted to the Usenet\nnewsgroup gnu.bash.bug.\n\nComments  and  bug  reports   concerning   this   manual   page   should   be   directed   to\nchet.ramey@case.edu.\n\nGNU History 8.1                             2020 July 17                                  HISTORY(3)",
            "subsections": []
        }
    },
    "summary": "history - GNU History Library",
    "flags": [],
    "examples": [],
    "see_also": [
        {
            "name": "bash",
            "section": "1",
            "url": "https://www.chedong.com/phpMan.php/man/bash/1/json"
        },
        {
            "name": "readline",
            "section": "3",
            "url": "https://www.chedong.com/phpMan.php/man/readline/3/json"
        }
    ]
}