Skip to content

🖋 editor - Open the default text editor 🖋

You can specify a command line that runs the editor, but usually you leave it empty - in that case, editor uses the the command line from the environment variable VISUAL, or if that's empty, the environment variable EDITOR, or if that's empty, either Notepad on Windows or vi elsewhere.

Example 1: Using a temporary file

If no filename is provided, a temporary file gets edited, and its contents returned.

import editor

MESSAGE = 'Insert comments below this line\n\n'
comments = editor(text=MESSAGE)
# Pops up the default editor with a tempfile, containing MESSAGE

Example 2: Using a named file

If a filename is provided, then it gets edited!

import os

FILE = 'file.txt'
assert not os.path.exists(FILE)

comments = editor(text=MESSAGE, filename=FILE)
# Pops up an editor for new FILE containing MESSAGE, user edits

assert os.path.exists(FILE)

# You can edit an existing file too, and select your own editor.
comments2 = editor(filename=FILE, editor='emacs')

API Documentation

default_editor()

Return the default text editor.

The default text editor is the contents of the environment variable EDITOR, it it's non-empty, otherwise if the platform is Windows, it's 'notepad', otherwise 'vim'.

Source code in editor/editor.py
103
104
105
106
107
108
109
110
111
112
113
114
def default_editor():
    """
    Return the default text editor.

    The default text editor is the contents of the environment variable
    `EDITOR`, it it's non-empty, otherwise if the platform is Windows, it's
    `'notepad'`, otherwise `'vim'`.
    """
    return os.environ.get('VISUAL') or (
        os.environ.get('EDITOR')
        or EDITORS.get(platform.system(), DEFAULT_EDITOR)
    )

editor(text=None, filename=None, editor=None, **kwargs)

Open a text editor, block while the user edits, then return the results

Parameters:

Name Type Description Default
text Optional[str]

A string which is written to the file before the editor is opened. If None, the file is left unchanged.

None
filename Union[None, Path, str]

The name of the file to edit. If None, a temporary file is used.

None
editor Optional[str]

A string containing the command used to invoke the text editor. If None, use editor.default_editor().

None
kwargs Mapping

Arguments passed on to subprocess.call()

{}
Source code in editor/editor.py
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
@xmod
def editor(
    text: Optional[str] = None,
    filename: Union[None, Path, str] = None,
    editor: Optional[str] = None,
    **kwargs: Mapping
):
    """
    Open a text editor, block while the user edits, then return the results

    Args:

      text: A string which is written to the file before the editor is opened.
          If `None`, the file is left unchanged.

      filename: The name of the file to edit.
          If `None`, a temporary file is used.

      editor: A string containing the command used to invoke the text editor.
         If `None`, use `editor.default_editor()`.

      kwargs: Arguments passed on to `subprocess.call()`
"""
    editor = editor or default_editor()
    is_temp = not filename
    if is_temp:
        fd, filename = tempfile.mkstemp()
        os.close(fd)

    try:
        path = Path(filename)
        if text is not None:
            path.write_text(text)

        cmd = '{} {}'.format(editor, filename)
        runs.call(cmd, **kwargs)
        return path.read_text()

    finally:
        if is_temp:
            try:
                path.unlink()
            except Exception:
                traceback.print_exc()

About this project