Open the default text editor

1.7.0 · active · verified Thu Apr 09

The 'editor' library provides a programmatic interface to open the system's default text editor from within Python scripts. It allows editing new content, existing files, or temporary files, and returns the modified content. Currently at version 1.7.0, the library is actively maintained with frequent patch releases, primarily for dependency updates and minor improvements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `editor.edit()` to open the default system editor. It shows two common use cases: editing initial content in a temporary file (which is returned and then deleted), and editing an existing file (changes are saved in place). Note that the `contents` argument requires a `bytes` object on Python 3. Both `editor.edit()` and `editor.editor()` functions are available and provide similar functionality for opening the editor.

import editor
import os

# Example 1: Edit new content in a temporary file
# The content must be a bytes object for Python 3.
initial_content = b"# Enter your commit message here\n\n"
commit_message = editor.edit(contents=initial_content)

print(f"User entered: {commit_message.decode('utf-8')}")

# Example 2: Edit an existing file (or create a new one)
# Ensure a dummy file exists for the example
file_path = "my_document.txt"
with open(file_path, "w") as f:
    f.write("Hello, World!\nThis is a test file.\n")

edited_content = editor.edit(file=file_path)

print(f"Content of {file_path} after editing: {edited_content.decode('utf-8')}")

# Clean up the dummy file
os.remove(file_path)

view raw JSON →