EDITLINE(3edit)
β¨ NAME β¨
okay so bestie, this is editline(3edit), and it's literally the whole squad of line editor, history and tokenization functions. We're talking: editline, el_init, el_init_fd, el_end, el_reset, el_gets, el_wgets, el_getc, el_wgetc, el_push, el_wpush, el_parse, el_wparse, el_set, el_wset, el_get, el_wget, el_source, el_resize, el_cursor, el_line, el_wline, el_insertstr, el_winsertstr, el_deletestr, el_wdeletestr, history_init, history_winit, history_end, history_wend, history, history_w, tok_init, tok_winit, tok_end, tok_wend, tok_reset, tok_wreset, tok_line, tok_wline, tok_str, tok_wstr. All of them = line editor, history and tokenization functions, and yes, that's the whole vibe. π
π LIBRARY π
Command Line Editor Library (libedit, -ledit). Slay.
π οΈ SYNOPSIS π§
#include <histedit.h>
EditLine *
el_init(const char *prog, FILE *fin, FILE *fout, FILE *ferr);
EditLine *
el_init_fd(const char *prog, FILE *fin, FILE *fout, FILE *ferr,
int fdin, int fdout, int fderr);
void
el_end(EditLine *e);
void
el_reset(EditLine *e);
const char *
el_gets(EditLine *e, int *count);
const wchar_t *
el_wgets(EditLine *e, int *count);
int
el_getc(EditLine *e, char *ch);
int
el_wgetc(EditLine *e, wchar_t *wc);
void
el_push(EditLine *e, const char *mbs);
void
el_wpush(EditLine *e, const wchar_t *wcs);
int
el_parse(EditLine *e, int argc, const char *argv[]);
int
el_wparse(EditLine *e, int argc, const wchar_t *argv[]);
int
el_set(EditLine *e, int op, ...);
int
el_wset(EditLine *e, int op, ...);
int
el_get(EditLine *e, int op, ...);
int
el_wget(EditLine *e, int op, ...);
int
el_source(EditLine *e, const char *file);
void
el_resize(EditLine *e);
int
el_cursor(EditLine *e, int count);
const LineInfo *
el_line(EditLine *e);
const LineInfoW *
el_wline(EditLine *e);
int
el_insertstr(EditLine *e, const char *str);
int
el_winsertstr(EditLine *e, const wchar_t *str);
void
el_deletestr(EditLine *e, int count);
void
el_wdeletestr(EditLine *e, int count);
History *
history_init(void);
HistoryW *
history_winit(void);
void
history_end(History *h);
void
history_wend(HistoryW *h);
int
history(History *h, HistEvent *ev, int op, ...);
int
history_w(HistoryW *h, HistEventW *ev, int op, ...);
Tokenizer *
tok_init(const char *IFS);
TokenizerW *
tok_winit(const wchar_t *IFS);
void
tok_end(Tokenizer *t);
void
tok_wend(TokenizerW *t);
void
tok_reset(Tokenizer *t);
void
tok_wreset(TokenizerW *t);
int
tok_line(Tokenizer *t, const LineInfo *li, int *argc,
const char **argv[], int *cursorc, int *cursoro);
int
tok_wline(TokenizerW *t, const LineInfoW *li, int *argc,
const wchar_t **argv[], int *cursorc, int *cursoro);
int
tok_str(Tokenizer *t, const char *str, int *argc, const char **argv[]);
int
tok_wstr(TokenizerW *t, const wchar_t *str, int *argc,
const wchar_t **argv[]);
πΈ DESCRIPTION πΈ
so editline is out here providing generic line editing, history and tokenization functions, just like the ones you'd find in sh(1).
These functions live in the libedit library (which needs the libtermcap library, no cap). Link your programs with -ledit -ltermcap and you're good.
Also, editline respects the LC_CTYPE locale set by the application program and never uses setlocale(3) to change the locale. periodt.
π LINE EDITING FUNCTIONS π
okay so the line editing functions all share one common data structure, EditLine, and it's created by el_init() or el_init_fd() and freed by el_end(). iconic.
The wide-character functions behave the same way as their narrow counterparts. no notes.
The following functions are available, bestie:
el_init()
Initialize the line editor, and return a data structure to be used by all other line editing functions, or NULL on failure. prog is the name of the invoking program, used when reading the editrc(5) file to determine which settings to use. fin, fout and ferr are the input, output, and error streams (respectively) to use. In this documentation, references to βthe ttyβ are actually to this input/output stream combination. π
el_init_fd()
Like el_init() but allows specifying file descriptors for the stdio(3) corresponding streams, in case those were created with funopen(3). slay.
el_end()
Clean up and finish with e, assumed to have been created with el_init() or el_init_fd(). bye bestie.
el_reset()
Reset the tty and the parser. This should be called after an error which may have upset the tty's state. it's giving damage control.
el_gets()
Read a line from the tty. count is modified to contain the number of characters read. Returns the line read if successful, or NULL if no characters were read or if an error occurred. If an error occurred, count is set to -1 and errno contains the error code that caused it. Important: the return value may not remain valid across calls to el_gets() and must be copied if the data is to be retained. no cap.
el_wgetc()
Read a wide character from the tty, respecting the current locale, or from the input queue described in editline(7) if that is not empty, and store it in wc. If an invalid or incomplete character is found, it is discarded, errno is set to EILSEQ, and the next character is read and stored in wc. Returns 1 if a valid character was read, 0 on end of file, or -1 on read(2) failure. In the latter case, errno is set to indicate the error. messy but okay.
el_getc()
Read a wide character as described for el_wgetc() and return 0 on end of file or -1 on failure. If the wide character can be represented as a single-byte character, convert it with wctob(3), store the result in ch, and return 1; otherwise, set errno to ERANGE and return -1. In the C or POSIX locale, this simply reads a byte, but for any other locale, including UTF-8, this is rarely useful. iykyk.
el_wpush()
Push the wide character string wcs back onto the input queue described in editline(7). If the queue overflows, for example due to a recursive macro, or if an error occurs, for example because wcs is NULL or memory allocation fails, the function beeps at the user, but does not report the problem to the caller. it's giving silent treatment.
el_push()
Use the current locale to convert the multibyte string mbs to a wide character string, and pass the result to el_wpush(). teamwork.
el_parse()
Parses the argv array (which is argc elements in size) to execute builtin editline commands. If the command is prefixed with βprogβ: then el_parse() will only execute the command if βprogβ matches the prog argument supplied to el_init(). The return value is -1 if the command is unknown, 0 if there was no error or βprogβ didn't match, or 1 if the command returned an error. Refer to editrc(5) for more information. π
el_set()
Set editline parameters. op determines which parameter to set, and each operation has its own parameter list. Returns 0 on success, -1 on failure. let's go.
The following values for op are supported, along with the required argument list:
EL_PROMPT,char *(*f)(EditLine *)β Define prompt printing function asf, which is to return a string that contains the prompt.EL_PROMPT_ESC,char *(*f)(EditLine *),char cβ Same asEL_PROMPT, but thecargument indicates the start/stop literal prompt character.
If a start/stop literal character is found in the prompt, the character itself is not printed, but characters after it are printed directly to the terminal without affecting the state of the current line. A subsequent second start/stop literal character ends this behavior. This is typically used to embed literal escape sequences that change the color/style of the terminal in the prompt. Note that the literal escape character cannot be the last character in the prompt, as the escape sequence is attached to the next character in the prompt. 0 unsets it.
EL_REFRESHβ Re-display the current line on the next terminal line.EL_RPROMPT,char *(*f)(EditLine *)β Define right side prompt printing function asf, which is to return a string that contains the prompt.EL_RPROMPT_ESC,char *(*f)(EditLine *),char cβ Define the right prompt printing function but with a literal escape character.EL_TERMINAL,const char *typeβ Define terminal type of the tty to betype, or toTERMiftypeisNULL.EL_EDITOR,const char *modeβ Set editing mode tomode, which must be one of βemacsβ or βviβ.EL_SIGNAL,int flagβ Ifflagis non-zero, editline will install its own signal handler for the following signals when reading command input:SIGCONT,SIGHUP,SIGINT,SIGQUIT,SIGSTOP,SIGTERM,SIGTSTP, andSIGWINCH. Otherwise, the current signal handlers will be used.EL_BIND,const char *, ...,NULLβ Perform thebindbuiltin command. Refer toeditrc(5)for more information.EL_ECHOTC,const char *, ...,NULLβ Perform theechotcbuiltin command. Refer toeditrc(5)for more information.EL_SETTC,const char *, ...,NULLβ Perform thesettcbuiltin command. Refer toeditrc(5)for more information.EL_SETTY,const char *, ...,NULLβ Perform thesettybuiltin command. Refer toeditrc(5)for more information.EL_TELLTC,const char *, ...,NULLβ Perform thetelltcbuiltin command. Refer toeditrc(5)for more information.EL_ADDFN,const char *name,const char *help,unsigned char (*func)(EditLine *e, int ch)β Add a user defined function,func(), referred to asnamewhich is invoked when a key which is bound tonameis entered.helpis a description ofname. At invocation time,chis the key which caused the invocation. The return value offunc()should be one of:CC_NORMβ Add a normal character.CC_NEWLINEβ End of line was entered.CC_EOFβ EOF was entered.CC_ARGHACKβ Expecting further command input as arguments, do nothing visually.
CC_REFRESHβ refresh the display β¨CC_REFRESH_BEEPβ refresh the display and beep πCC_CURSORβ the cursor moved, so update and do aCC_REFRESHπ«CC_REDISPLAYβ redisplay the entire input line. This one's lowkey a lifesaver if a key binding outputs extra information π«ΆCC_ERRORβ an error happened: beep, and flush the tty πCC_FATALβ fatal error, reset the tty to a known state πEL_HIST,History *(*func)(History *, int op, ...),const char *ptrβ says which history function to use, which is usuallyhistory(). Andptrshould be the value thathistory_init()handed back to you πEL_EDITMODE,int flagβ ifflagis non-zero, editing is enabled (that's the default). But heads up bestie: this is literally just an indication, it does not affect howeditlineoperates. At this time, it is the caller's responsibility to check this (usingel_get()) to decide whether editing should be enabled or not πEL_UNBUFFERED,int flagβ ifflagis zero, unbuffered mode is disabled (the default). In unbuffered mode,el_gets()returns immediately after processing a single character β‘EL_SAFEREAD,int flagβ ifflagis non-zero,editlineattempts to recover from read errors, ignoring the first interrupted error and trying to reset the input file descriptor to reset non-blocking I/O. It's disabled by default, and it's only desirable wheneditlineis used in shell-like applications πEL_GETCFN,el_rfunc_t fβ whenever a character is being read, use this function:
int f(EditLine *e, wchar_t *wc)
which stores the character in wc and returns 1 on success, 0 on end of file, or -1 on I/O or encoding errors π¦ The functions internally using it include el_wgets(), el_wgetc(), el_gets(), and el_getc(). Initially a builtin function is installed, and replacing it is discouraged because writing such a function is very error prone π The builtin function can be restored at any time by passing the special value EL_BUILTIN_GETCFN instead of a function pointer π
EL_CLIENTDATA,void *dataβ register data to be associated with thisEditLinestructure. You can get it back with the correspondingel_get()call πΈEL_SETFP,int fd,FILE *fpβ set the currenteditlinefile pointer for"input"fd = 0,"output"fd = 1, or"error"fd = 2fromfpπEL_GETENV,char * (*funcp)(const char *)β makes the functioneditlineuses to obtain the value of an environment variable become the one you pass asfuncp: a pointer to a function that takes aconst char *as its parameter and returnschar *. The default isgetenv(3)π
el_get() β the getter era π
Get editline parameters. op decides which parameter gets retrieved into result. Returns 0 if it went well, -1 otherwise.
The following values for op are supported, along with the actual type of result:
EL_PROMPT,char *(*f)(EditLine *),char *cβ setfto a pointer to the function that displays the prompt. Ifcis notNULL, set it to the start/stop literal prompt character β¨EL_RPROMPT,char *(*f)(EditLine *),char *cβ setfto a pointer to the function that displays the prompt. Ifcis notNULL, set it to the start/stop literal prompt character β¨EL_EDITOR,const char **nβ set the name of the editor inn, which will be one of"emacs"or"vi"πEL_GETTC,const char *name,void *valueβ ifnameis a validtermcap(5)capability, setvalueto the current value of that capability π§EL_SIGNAL,int *sβ setsto non-zero ifeditlinehas installed private signal handlers (seeel_get()above) π‘EL_EDITMODE,int *cβ setcto non-zero if editing is enabled πEL_GETCFN,el_rfunc_t *fβ setfto a pointer to the function that reads characters, or toEL_BUILTIN_GETCFNif the builtin function is the one in use πEL_CLIENTDATA,void **dataβ setdatato the previously registered client data set by anel_set()call πEL_UNBUFFERED,int *cβ setcto non-zero if unbuffered mode is enabled β‘EL_SAFEREAD,int *cβ setcto non-zero if safe read is set π‘οΈEL_GETFP,int fd,FILE **fpβ setfpto the currenteditlinefile pointer for"input"fd = 0,"output"fd = 1, or"error"fd = 2πEL_WORDCHARS,Char *wordcharsβ define the list of characters that can be part of a word, in addition to alphanumeric characters. This list is*?_-.[]~=for emacs mode and_for vi mode. Resetting the editor binding to either emacs or vi also resets the wordchars to their default value π€EL_GETENV,char * (**funcp)(const char *)β return, via the pointer to a pointer to a function,funcp, the function thateditlineis using to obtain the values of environment variables π
el_source() β reading the vibes off a file π
Initialize editline by reading the contents of file. el_parse() is called for each line in file. If file is NULL, try $EDITRC and if that's not set, $HOME/.editrc. Peep editrc(5) for the details on the format of file. el_source() returns 0 on success and -1 on error π«Ά
el_resize() β for when the terminal changes shape π
Must be called if the terminal size changes. If EL_SIGNAL has been set with el_set(), then this happens automatically β¨ Otherwise, it's the responsibility of the application to call el_resize() on the appropriate occasions.
el_cursor() β scoot scoot π΄
Move the cursor to the right (if positive) or to the left (if negative) count characters. Returns the resulting offset of the cursor from the beginning of the line π
el_line() β all the tea about the current line π΅
Return the editing information for the current line in a LineInfo structure, which is defined as follows:
typedef struct lineinfo {
const char *buffer; /* address of buffer */
const char *cursor; /* address of cursor */
const char *lastchar; /* address of last character */
} LineInfo;
buffer is not NUL terminated π± This function may be called after el_gets() to obtain the LineInfo structure pertaining to the line returned by that function, and from within user defined functions added with EL_ADDFN π«
el_insertstr() β slide it in π
Insert str into the line at the cursor. Returns -1 if str is empty or won't fit, and 0 otherwise.
el_deletestr() β bye bye, backspace energy ποΈ
Delete count characters before the cursor.
π HISTORY LIST FUNCTIONS π
So all the history functions share one lil data structure called History, and you get it from history_init() and yeet it away with history_end(). That's the whole vibe. π«Ά
Here's every function you've got, bestie:
history_init()β Boots up the history list and hands you back a data structure that every other history function will use. If it flops, you getNULL. πhistory_end()β Wraps everything up and says bye-bye toh, which we're assuming came fromhistory_init().history()β Does the operationopon the history list, with whatever optional arguments that operation needs. Andevgets updated to match whatever operation you did. Here's all the stuffopcan be, plus the argument list each one demands:H_SETSIZE,int sizeβ Sets the size of the history tosizeelements.H_GETSIZEβ Gets the number of events currently in history.H_ENDβ Cleans up and finishes withh, which we assume came fromhistory_init().H_CLEARβ Clears the history. Poof. Gone. β¨H_FUNC,void *ptr,history_gfun_t first,history_gfun_t next,history_gfun_t last,history_gfun_t prev,history_gfun_t curr,history_sfun_t set,history_vfun_t clear,history_efun_t enter,history_efun_t addβ Defines the functions that carry out the various history operations.ptris the argument handed to a function when it gets invoked.H_FIRSTβ Returns the first element in the history.H_LASTβ Returns the last element in the history.H_PREVβ Returns the previous element in the history. It's newer than the current one.H_NEXTβ Returns the next element in the history. It's older than the current one.H_CURRβ Returns the current element in the history.H_SET,int positionβ Puts the cursor on the element you asked for.H_ADD,const char *strβ Appendsstronto the current element of the history, or if there's no current element it just does theH_ENTERoperation withstras the argument instead.H_APPEND,const char *strβ Appendsstrto the last new element of the history.H_ENTER,const char *strβ Addsstras a brand new element to the history and, if it has to, removes the oldest entry so the list stays at the size you created it with. Now listen: ifH_SETUNIQUEwas called with a non-zero argument, the element will not be entered into the history if its contents match the current history element. If the element does get entered,history()returns1; if it gets ignored as a duplicate, it returns0. And finally,history()returns-1if an error happened. πH_PREV_STR,const char *strβ Returns the closest previous event that starts withstr.H_NEXT_STR,const char *strβ Returns the closest next event that starts withstr.H_PREV_EVENT,int eβ Returns the previous event numberede.H_NEXT_EVENT,int eβ Returns the next event numberede.H_LOAD,const char *fileβ Loads the history list stored infile.H_SAVE,const char *fileβ Saves the history list tofile.H_SAVE_FP,FILE *fpβ Saves the history list to the openedFILEpointerfp.H_NSAVE_FP,size_t n,FILE *fpβ Saves the lastnhistory entries to the openedFILEpointerfp.H_SETUNIQUE,int uniqueβ Sets the flag so adjacent identical event strings don't get entered into the history.H_GETUNIQUEβ Gets the current setting for whether adjacent identical elements should be entered into the history.H_DEL,int eβ Deletes the event numberede. Heads up: this one is only there for readline compatibility. The caller is the one responsible forfree'ing the string in the returnedHistEvent. π
history() returns >= 0 if the operation op succeeds. Otherwise -1 comes back and ev gets updated with more details about the error.
πͺ TOKENIZATION FUNCTIONS πͺ
The tokenization functions all share a common data structure called Tokenizer, which is created by tok_init() and freed by tok_end().
Here's what you've got:
tok_init()β Initializes the tokenizer and returns a data structure that all the other tokenizer functions will use.IFSholds the Input Field Separators, which defaults to β¨spaceβ©, β¨tabβ©, and β¨newlineβ© if it'sNULL.tok_end()β Cleans up and finishes witht, assumed to have been created withtok_init().tok_reset()β Resets the tokenizer state. Use this after a line has been successfully tokenized bytok_line()ortok_str()and before you tokenize a new line.tok_line()β Tokenizesli. If it works, it modifies:argvto contain the words,argcto contain the number of words,cursorc(if it's notNULL) to contain the index of the word containing the cursor, andcursoro(if it's notNULL) to contain the offset withinargv[cursorc]of the cursor.
Returns 0 if successful, -1 for an internal error, 1 for an unmatched single quote, 2 for an unmatched double quote, and 3 for a backslash quoted β¨newlineβ©. A positive exit code means you should read another line and try the tokenization again. π
tok_str()β A simpler form oftok_line();stris aNULterminated string to tokenize.
π SEE ALSO π
sh(1), signal(3), termcap(3), editrc(5edit), termcap(5), editline(7edit)
β³ HISTORY (the other kind lol) β³
The editline library first appeared in 4.4BSD. CC_REDISPLAY appeared in NetBSD 1.3. CC_REFRESH_BEEP, EL_EDITMODE and the readline emulation appeared in NetBSD 1.4. EL_RPROMPT appeared in NetBSD 1.5. It's giving legacy. π
π AUTHORS π
The editline library was written by Christos Zoulas. Luke Mewburn wrote this manual and implemented CC_REDISPLAY, CC_REFRESH_BEEP, EL_EDITMODE, and EL_RPROMPT. Jaromir Dolecek implemented the readline emulation. Johny Mattsson implemented wide-character support. Not one, not two, not three, but four icons. We stan. π
π BUGS π
Okay so right now, it is the caller's responsibility to check the result of the EL_EDITMODE operation of el_get() (after an el_source() or el_parse()) to figure out whether editline should be used for further input. Like, EL_EDITMODE is purely an indication of the result of the most recent editrc(5) edit command. That's it. No cap. Don't @ me. π
And that's a wrap on EDITLINE(3edit), bestie β this page came from the Debian crew and is dated December 16, 2025, so if it looks fresh, that's because it is. Go forth and edit lines. Slay. πβ¨π
π¦ where this page comes from
- ubuntu stonking: libedit-dev 3.1-20260512-1
- Copyright (c) 1997-2014 The NetBSD Foundation, Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms