17db96d56Sopenharmony_ci:mod:`readline` --- GNU readline interface 27db96d56Sopenharmony_ci========================================== 37db96d56Sopenharmony_ci 47db96d56Sopenharmony_ci.. module:: readline 57db96d56Sopenharmony_ci :platform: Unix 67db96d56Sopenharmony_ci :synopsis: GNU readline support for Python. 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci.. sectionauthor:: Skip Montanaro <skip@pobox.com> 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci-------------- 117db96d56Sopenharmony_ci 127db96d56Sopenharmony_ciThe :mod:`readline` module defines a number of functions to facilitate 137db96d56Sopenharmony_cicompletion and reading/writing of history files from the Python interpreter. 147db96d56Sopenharmony_ciThis module can be used directly, or via the :mod:`rlcompleter` module, which 157db96d56Sopenharmony_cisupports completion of Python identifiers at the interactive prompt. Settings 167db96d56Sopenharmony_cimade using this module affect the behaviour of both the interpreter's 177db96d56Sopenharmony_ciinteractive prompt and the prompts offered by the built-in :func:`input` 187db96d56Sopenharmony_cifunction. 197db96d56Sopenharmony_ci 207db96d56Sopenharmony_ciReadline keybindings may be configured via an initialization file, typically 217db96d56Sopenharmony_ci``.inputrc`` in your home directory. See `Readline Init File 227db96d56Sopenharmony_ci<https://tiswww.cwru.edu/php/chet/readline/rluserman.html#Readline-Init-File>`_ 237db96d56Sopenharmony_ciin the GNU Readline manual for information about the format and 247db96d56Sopenharmony_ciallowable constructs of that file, and the capabilities of the 257db96d56Sopenharmony_ciReadline library in general. 267db96d56Sopenharmony_ci 277db96d56Sopenharmony_ci.. note:: 287db96d56Sopenharmony_ci 297db96d56Sopenharmony_ci The underlying Readline library API may be implemented by 307db96d56Sopenharmony_ci the ``libedit`` library instead of GNU readline. 317db96d56Sopenharmony_ci On macOS the :mod:`readline` module detects which library is being used 327db96d56Sopenharmony_ci at run time. 337db96d56Sopenharmony_ci 347db96d56Sopenharmony_ci The configuration file for ``libedit`` is different from that 357db96d56Sopenharmony_ci of GNU readline. If you programmatically load configuration strings 367db96d56Sopenharmony_ci you can check for the text "libedit" in :const:`readline.__doc__` 377db96d56Sopenharmony_ci to differentiate between GNU readline and libedit. 387db96d56Sopenharmony_ci 397db96d56Sopenharmony_ci If you use *editline*/``libedit`` readline emulation on macOS, the 407db96d56Sopenharmony_ci initialization file located in your home directory is named 417db96d56Sopenharmony_ci ``.editrc``. For example, the following content in ``~/.editrc`` will 427db96d56Sopenharmony_ci turn ON *vi* keybindings and TAB completion:: 437db96d56Sopenharmony_ci 447db96d56Sopenharmony_ci python:bind -v 457db96d56Sopenharmony_ci python:bind ^I rl_complete 467db96d56Sopenharmony_ci 477db96d56Sopenharmony_ci 487db96d56Sopenharmony_ciInit file 497db96d56Sopenharmony_ci--------- 507db96d56Sopenharmony_ci 517db96d56Sopenharmony_ciThe following functions relate to the init file and user configuration: 527db96d56Sopenharmony_ci 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci.. function:: parse_and_bind(string) 557db96d56Sopenharmony_ci 567db96d56Sopenharmony_ci Execute the init line provided in the *string* argument. This calls 577db96d56Sopenharmony_ci :c:func:`rl_parse_and_bind` in the underlying library. 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci 607db96d56Sopenharmony_ci.. function:: read_init_file([filename]) 617db96d56Sopenharmony_ci 627db96d56Sopenharmony_ci Execute a readline initialization file. The default filename is the last filename 637db96d56Sopenharmony_ci used. This calls :c:func:`rl_read_init_file` in the underlying library. 647db96d56Sopenharmony_ci 657db96d56Sopenharmony_ci 667db96d56Sopenharmony_ciLine buffer 677db96d56Sopenharmony_ci----------- 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ciThe following functions operate on the line buffer: 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci 727db96d56Sopenharmony_ci.. function:: get_line_buffer() 737db96d56Sopenharmony_ci 747db96d56Sopenharmony_ci Return the current contents of the line buffer (:c:data:`rl_line_buffer` 757db96d56Sopenharmony_ci in the underlying library). 767db96d56Sopenharmony_ci 777db96d56Sopenharmony_ci 787db96d56Sopenharmony_ci.. function:: insert_text(string) 797db96d56Sopenharmony_ci 807db96d56Sopenharmony_ci Insert text into the line buffer at the cursor position. This calls 817db96d56Sopenharmony_ci :c:func:`rl_insert_text` in the underlying library, but ignores 827db96d56Sopenharmony_ci the return value. 837db96d56Sopenharmony_ci 847db96d56Sopenharmony_ci 857db96d56Sopenharmony_ci.. function:: redisplay() 867db96d56Sopenharmony_ci 877db96d56Sopenharmony_ci Change what's displayed on the screen to reflect the current contents of the 887db96d56Sopenharmony_ci line buffer. This calls :c:func:`rl_redisplay` in the underlying library. 897db96d56Sopenharmony_ci 907db96d56Sopenharmony_ci 917db96d56Sopenharmony_ciHistory file 927db96d56Sopenharmony_ci------------ 937db96d56Sopenharmony_ci 947db96d56Sopenharmony_ciThe following functions operate on a history file: 957db96d56Sopenharmony_ci 967db96d56Sopenharmony_ci 977db96d56Sopenharmony_ci.. function:: read_history_file([filename]) 987db96d56Sopenharmony_ci 997db96d56Sopenharmony_ci Load a readline history file, and append it to the history list. 1007db96d56Sopenharmony_ci The default filename is :file:`~/.history`. This calls 1017db96d56Sopenharmony_ci :c:func:`read_history` in the underlying library. 1027db96d56Sopenharmony_ci 1037db96d56Sopenharmony_ci 1047db96d56Sopenharmony_ci.. function:: write_history_file([filename]) 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci Save the history list to a readline history file, overwriting any 1077db96d56Sopenharmony_ci existing file. The default filename is :file:`~/.history`. This calls 1087db96d56Sopenharmony_ci :c:func:`write_history` in the underlying library. 1097db96d56Sopenharmony_ci 1107db96d56Sopenharmony_ci 1117db96d56Sopenharmony_ci.. function:: append_history_file(nelements[, filename]) 1127db96d56Sopenharmony_ci 1137db96d56Sopenharmony_ci Append the last *nelements* items of history to a file. The default filename is 1147db96d56Sopenharmony_ci :file:`~/.history`. The file must already exist. This calls 1157db96d56Sopenharmony_ci :c:func:`append_history` in the underlying library. This function 1167db96d56Sopenharmony_ci only exists if Python was compiled for a version of the library 1177db96d56Sopenharmony_ci that supports it. 1187db96d56Sopenharmony_ci 1197db96d56Sopenharmony_ci .. versionadded:: 3.5 1207db96d56Sopenharmony_ci 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci.. function:: get_history_length() 1237db96d56Sopenharmony_ci set_history_length(length) 1247db96d56Sopenharmony_ci 1257db96d56Sopenharmony_ci Set or return the desired number of lines to save in the history file. 1267db96d56Sopenharmony_ci The :func:`write_history_file` function uses this value to truncate 1277db96d56Sopenharmony_ci the history file, by calling :c:func:`history_truncate_file` in 1287db96d56Sopenharmony_ci the underlying library. Negative values imply 1297db96d56Sopenharmony_ci unlimited history file size. 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci 1327db96d56Sopenharmony_ciHistory list 1337db96d56Sopenharmony_ci------------ 1347db96d56Sopenharmony_ci 1357db96d56Sopenharmony_ciThe following functions operate on a global history list: 1367db96d56Sopenharmony_ci 1377db96d56Sopenharmony_ci 1387db96d56Sopenharmony_ci.. function:: clear_history() 1397db96d56Sopenharmony_ci 1407db96d56Sopenharmony_ci Clear the current history. This calls :c:func:`clear_history` in the 1417db96d56Sopenharmony_ci underlying library. The Python function only exists if Python was 1427db96d56Sopenharmony_ci compiled for a version of the library that supports it. 1437db96d56Sopenharmony_ci 1447db96d56Sopenharmony_ci 1457db96d56Sopenharmony_ci.. function:: get_current_history_length() 1467db96d56Sopenharmony_ci 1477db96d56Sopenharmony_ci Return the number of items currently in the history. (This is different from 1487db96d56Sopenharmony_ci :func:`get_history_length`, which returns the maximum number of lines that will 1497db96d56Sopenharmony_ci be written to a history file.) 1507db96d56Sopenharmony_ci 1517db96d56Sopenharmony_ci 1527db96d56Sopenharmony_ci.. function:: get_history_item(index) 1537db96d56Sopenharmony_ci 1547db96d56Sopenharmony_ci Return the current contents of history item at *index*. The item index 1557db96d56Sopenharmony_ci is one-based. This calls :c:func:`history_get` in the underlying library. 1567db96d56Sopenharmony_ci 1577db96d56Sopenharmony_ci 1587db96d56Sopenharmony_ci.. function:: remove_history_item(pos) 1597db96d56Sopenharmony_ci 1607db96d56Sopenharmony_ci Remove history item specified by its position from the history. 1617db96d56Sopenharmony_ci The position is zero-based. This calls :c:func:`remove_history` in 1627db96d56Sopenharmony_ci the underlying library. 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci 1657db96d56Sopenharmony_ci.. function:: replace_history_item(pos, line) 1667db96d56Sopenharmony_ci 1677db96d56Sopenharmony_ci Replace history item specified by its position with *line*. 1687db96d56Sopenharmony_ci The position is zero-based. This calls :c:func:`replace_history_entry` 1697db96d56Sopenharmony_ci in the underlying library. 1707db96d56Sopenharmony_ci 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci.. function:: add_history(line) 1737db96d56Sopenharmony_ci 1747db96d56Sopenharmony_ci Append *line* to the history buffer, as if it was the last line typed. 1757db96d56Sopenharmony_ci This calls :c:func:`add_history` in the underlying library. 1767db96d56Sopenharmony_ci 1777db96d56Sopenharmony_ci 1787db96d56Sopenharmony_ci.. function:: set_auto_history(enabled) 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci Enable or disable automatic calls to :c:func:`add_history` when reading 1817db96d56Sopenharmony_ci input via readline. The *enabled* argument should be a Boolean value 1827db96d56Sopenharmony_ci that when true, enables auto history, and that when false, disables 1837db96d56Sopenharmony_ci auto history. 1847db96d56Sopenharmony_ci 1857db96d56Sopenharmony_ci .. versionadded:: 3.6 1867db96d56Sopenharmony_ci 1877db96d56Sopenharmony_ci .. impl-detail:: 1887db96d56Sopenharmony_ci Auto history is enabled by default, and changes to this do not persist 1897db96d56Sopenharmony_ci across multiple sessions. 1907db96d56Sopenharmony_ci 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ciStartup hooks 1937db96d56Sopenharmony_ci------------- 1947db96d56Sopenharmony_ci 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_ci.. function:: set_startup_hook([function]) 1977db96d56Sopenharmony_ci 1987db96d56Sopenharmony_ci Set or remove the function invoked by the :c:data:`rl_startup_hook` 1997db96d56Sopenharmony_ci callback of the underlying library. If *function* is specified, it will 2007db96d56Sopenharmony_ci be used as the new hook function; if omitted or ``None``, any function 2017db96d56Sopenharmony_ci already installed is removed. The hook is called with no 2027db96d56Sopenharmony_ci arguments just before readline prints the first prompt. 2037db96d56Sopenharmony_ci 2047db96d56Sopenharmony_ci 2057db96d56Sopenharmony_ci.. function:: set_pre_input_hook([function]) 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_ci Set or remove the function invoked by the :c:data:`rl_pre_input_hook` 2087db96d56Sopenharmony_ci callback of the underlying library. If *function* is specified, it will 2097db96d56Sopenharmony_ci be used as the new hook function; if omitted or ``None``, any 2107db96d56Sopenharmony_ci function already installed is removed. The hook is called 2117db96d56Sopenharmony_ci with no arguments after the first prompt has been printed and just before 2127db96d56Sopenharmony_ci readline starts reading input characters. This function only exists 2137db96d56Sopenharmony_ci if Python was compiled for a version of the library that supports it. 2147db96d56Sopenharmony_ci 2157db96d56Sopenharmony_ci 2167db96d56Sopenharmony_ciCompletion 2177db96d56Sopenharmony_ci---------- 2187db96d56Sopenharmony_ci 2197db96d56Sopenharmony_ciThe following functions relate to implementing a custom word completion 2207db96d56Sopenharmony_cifunction. This is typically operated by the Tab key, and can suggest and 2217db96d56Sopenharmony_ciautomatically complete a word being typed. By default, Readline is set up 2227db96d56Sopenharmony_cito be used by :mod:`rlcompleter` to complete Python identifiers for 2237db96d56Sopenharmony_cithe interactive interpreter. If the :mod:`readline` module is to be used 2247db96d56Sopenharmony_ciwith a custom completer, a different set of word delimiters should be set. 2257db96d56Sopenharmony_ci 2267db96d56Sopenharmony_ci 2277db96d56Sopenharmony_ci.. function:: set_completer([function]) 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_ci Set or remove the completer function. If *function* is specified, it will be 2307db96d56Sopenharmony_ci used as the new completer function; if omitted or ``None``, any completer 2317db96d56Sopenharmony_ci function already installed is removed. The completer function is called as 2327db96d56Sopenharmony_ci ``function(text, state)``, for *state* in ``0``, ``1``, ``2``, ..., until it 2337db96d56Sopenharmony_ci returns a non-string value. It should return the next possible completion 2347db96d56Sopenharmony_ci starting with *text*. 2357db96d56Sopenharmony_ci 2367db96d56Sopenharmony_ci The installed completer function is invoked by the *entry_func* callback 2377db96d56Sopenharmony_ci passed to :c:func:`rl_completion_matches` in the underlying library. 2387db96d56Sopenharmony_ci The *text* string comes from the first parameter to the 2397db96d56Sopenharmony_ci :c:data:`rl_attempted_completion_function` callback of the 2407db96d56Sopenharmony_ci underlying library. 2417db96d56Sopenharmony_ci 2427db96d56Sopenharmony_ci 2437db96d56Sopenharmony_ci.. function:: get_completer() 2447db96d56Sopenharmony_ci 2457db96d56Sopenharmony_ci Get the completer function, or ``None`` if no completer function has been set. 2467db96d56Sopenharmony_ci 2477db96d56Sopenharmony_ci 2487db96d56Sopenharmony_ci.. function:: get_completion_type() 2497db96d56Sopenharmony_ci 2507db96d56Sopenharmony_ci Get the type of completion being attempted. This returns the 2517db96d56Sopenharmony_ci :c:data:`rl_completion_type` variable in the underlying library as 2527db96d56Sopenharmony_ci an integer. 2537db96d56Sopenharmony_ci 2547db96d56Sopenharmony_ci 2557db96d56Sopenharmony_ci.. function:: get_begidx() 2567db96d56Sopenharmony_ci get_endidx() 2577db96d56Sopenharmony_ci 2587db96d56Sopenharmony_ci Get the beginning or ending index of the completion scope. 2597db96d56Sopenharmony_ci These indexes are the *start* and *end* arguments passed to the 2607db96d56Sopenharmony_ci :c:data:`rl_attempted_completion_function` callback of the 2617db96d56Sopenharmony_ci underlying library. The values may be different in the same 2627db96d56Sopenharmony_ci input editing scenario based on the underlying C readline implementation. 2637db96d56Sopenharmony_ci Ex: libedit is known to behave differently than libreadline. 2647db96d56Sopenharmony_ci 2657db96d56Sopenharmony_ci 2667db96d56Sopenharmony_ci.. function:: set_completer_delims(string) 2677db96d56Sopenharmony_ci get_completer_delims() 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ci Set or get the word delimiters for completion. These determine the 2707db96d56Sopenharmony_ci start of the word to be considered for completion (the completion scope). 2717db96d56Sopenharmony_ci These functions access the :c:data:`rl_completer_word_break_characters` 2727db96d56Sopenharmony_ci variable in the underlying library. 2737db96d56Sopenharmony_ci 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ci.. function:: set_completion_display_matches_hook([function]) 2767db96d56Sopenharmony_ci 2777db96d56Sopenharmony_ci Set or remove the completion display function. If *function* is 2787db96d56Sopenharmony_ci specified, it will be used as the new completion display function; 2797db96d56Sopenharmony_ci if omitted or ``None``, any completion display function already 2807db96d56Sopenharmony_ci installed is removed. This sets or clears the 2817db96d56Sopenharmony_ci :c:data:`rl_completion_display_matches_hook` callback in the 2827db96d56Sopenharmony_ci underlying library. The completion display function is called as 2837db96d56Sopenharmony_ci ``function(substitution, [matches], longest_match_length)`` once 2847db96d56Sopenharmony_ci each time matches need to be displayed. 2857db96d56Sopenharmony_ci 2867db96d56Sopenharmony_ci 2877db96d56Sopenharmony_ci.. _readline-example: 2887db96d56Sopenharmony_ci 2897db96d56Sopenharmony_ciExample 2907db96d56Sopenharmony_ci------- 2917db96d56Sopenharmony_ci 2927db96d56Sopenharmony_ciThe following example demonstrates how to use the :mod:`readline` module's 2937db96d56Sopenharmony_cihistory reading and writing functions to automatically load and save a history 2947db96d56Sopenharmony_cifile named :file:`.python_history` from the user's home directory. The code 2957db96d56Sopenharmony_cibelow would normally be executed automatically during interactive sessions 2967db96d56Sopenharmony_cifrom the user's :envvar:`PYTHONSTARTUP` file. :: 2977db96d56Sopenharmony_ci 2987db96d56Sopenharmony_ci import atexit 2997db96d56Sopenharmony_ci import os 3007db96d56Sopenharmony_ci import readline 3017db96d56Sopenharmony_ci 3027db96d56Sopenharmony_ci histfile = os.path.join(os.path.expanduser("~"), ".python_history") 3037db96d56Sopenharmony_ci try: 3047db96d56Sopenharmony_ci readline.read_history_file(histfile) 3057db96d56Sopenharmony_ci # default history len is -1 (infinite), which may grow unruly 3067db96d56Sopenharmony_ci readline.set_history_length(1000) 3077db96d56Sopenharmony_ci except FileNotFoundError: 3087db96d56Sopenharmony_ci pass 3097db96d56Sopenharmony_ci 3107db96d56Sopenharmony_ci atexit.register(readline.write_history_file, histfile) 3117db96d56Sopenharmony_ci 3127db96d56Sopenharmony_ciThis code is actually automatically run when Python is run in 3137db96d56Sopenharmony_ci:ref:`interactive mode <tut-interactive>` (see :ref:`rlcompleter-config`). 3147db96d56Sopenharmony_ci 3157db96d56Sopenharmony_ciThe following example achieves the same goal but supports concurrent interactive 3167db96d56Sopenharmony_cisessions, by only appending the new history. :: 3177db96d56Sopenharmony_ci 3187db96d56Sopenharmony_ci import atexit 3197db96d56Sopenharmony_ci import os 3207db96d56Sopenharmony_ci import readline 3217db96d56Sopenharmony_ci histfile = os.path.join(os.path.expanduser("~"), ".python_history") 3227db96d56Sopenharmony_ci 3237db96d56Sopenharmony_ci try: 3247db96d56Sopenharmony_ci readline.read_history_file(histfile) 3257db96d56Sopenharmony_ci h_len = readline.get_current_history_length() 3267db96d56Sopenharmony_ci except FileNotFoundError: 3277db96d56Sopenharmony_ci open(histfile, 'wb').close() 3287db96d56Sopenharmony_ci h_len = 0 3297db96d56Sopenharmony_ci 3307db96d56Sopenharmony_ci def save(prev_h_len, histfile): 3317db96d56Sopenharmony_ci new_h_len = readline.get_current_history_length() 3327db96d56Sopenharmony_ci readline.set_history_length(1000) 3337db96d56Sopenharmony_ci readline.append_history_file(new_h_len - prev_h_len, histfile) 3347db96d56Sopenharmony_ci atexit.register(save, h_len, histfile) 3357db96d56Sopenharmony_ci 3367db96d56Sopenharmony_ciThe following example extends the :class:`code.InteractiveConsole` class to 3377db96d56Sopenharmony_cisupport history save/restore. :: 3387db96d56Sopenharmony_ci 3397db96d56Sopenharmony_ci import atexit 3407db96d56Sopenharmony_ci import code 3417db96d56Sopenharmony_ci import os 3427db96d56Sopenharmony_ci import readline 3437db96d56Sopenharmony_ci 3447db96d56Sopenharmony_ci class HistoryConsole(code.InteractiveConsole): 3457db96d56Sopenharmony_ci def __init__(self, locals=None, filename="<console>", 3467db96d56Sopenharmony_ci histfile=os.path.expanduser("~/.console-history")): 3477db96d56Sopenharmony_ci code.InteractiveConsole.__init__(self, locals, filename) 3487db96d56Sopenharmony_ci self.init_history(histfile) 3497db96d56Sopenharmony_ci 3507db96d56Sopenharmony_ci def init_history(self, histfile): 3517db96d56Sopenharmony_ci readline.parse_and_bind("tab: complete") 3527db96d56Sopenharmony_ci if hasattr(readline, "read_history_file"): 3537db96d56Sopenharmony_ci try: 3547db96d56Sopenharmony_ci readline.read_history_file(histfile) 3557db96d56Sopenharmony_ci except FileNotFoundError: 3567db96d56Sopenharmony_ci pass 3577db96d56Sopenharmony_ci atexit.register(self.save_history, histfile) 3587db96d56Sopenharmony_ci 3597db96d56Sopenharmony_ci def save_history(self, histfile): 3607db96d56Sopenharmony_ci readline.set_history_length(1000) 3617db96d56Sopenharmony_ci readline.write_history_file(histfile) 362