Skip to main content

Like webbrowser, but for the text editor.

Project description

TextEditor

Programmatically open the system's editor from your Python program (like webbrowser but for text editors).

Unlike other libraries, TextEditor makes an effort to find the text editor the users really prefer, specially for those that doesn't know what an EDITOR environment variable is.

Temporal file

import texteditor

text = texteditor.open("This is the starting content")

Opens a temporary file with some content to edit, and returns the new content when the user closes the editor.

Existing (or new) file

text = texteditor.open(filename="README.md")

# Warning: By doing the following, you will overwrite the existing content:
# text = texteditor.open(
#   text="This will replace the file content",
#   filename="README.md"
# )

You can also edit an existing text file or one you want to create.

Installation

Using pip of course!

python -m pip install texteditor

Usage

texteditor.open(text=None, filename=None, extension="txt", encoding=None)

Opens filename or a new temporary file in the default editor.

  • text: The starting content for the edited file. This will also be used instead of the original contents of filename if one is also defined.

  • filename: Edit this file instead of a new temporary one.

  • extension: When editing a new temporary file, this will help the editor recognize the intended filetype, so syntax highlighting and custom settings for that filetype can be used. Examples: txt, md, ini. Ignored if filename is used.

  • encoding: To encode the content and decode the result, texteditor.open() uses the default encoding for the platform, but you can use an encoding argument to specify any text encoding supported by Python.

How it Works

texteditor.open() first looks for the $EDITOR environment variable. If set, it uses the value as-is, including any command-line argument, without fallbacks.

If no $EDITOR is set, it will try to guess.

On MacOS, it calls the system default for editing that file extension.

In other operating systems, the function will search through a very short list of known editors, and use the first one that founds.

You might notice that vim and Emacs are not in that short list, that's because:

  1. If you are using it, you know what the EDITOR variable is, and you probably has set it already.
  2. If you aren't using it, finding yourself in their UI for the first time is going to be super confusing. In fact "How to exit vim" is a common Stack Overflow question. Having to google how to set an EDITOR variable is a less scary alternative.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

texteditor-1.3.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

texteditor-1.3-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file texteditor-1.3.tar.gz.

File metadata

  • Download URL: texteditor-1.3.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for texteditor-1.3.tar.gz
Algorithm Hash digest
SHA256 abb3149a96f25aadb876420737a7a178179e980a8becefa44ac4e5f52090e155
MD5 1cb87c5c29a44cb4521c4e479600b323
BLAKE2b-256 35e3b344cb78f3e8163d44b99e80b5d8b189c8ca614e21409f27ccbc9e87ba52

See more details on using hashes here.

File details

Details for the file texteditor-1.3-py3-none-any.whl.

File metadata

  • Download URL: texteditor-1.3-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.0

File hashes

Hashes for texteditor-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 359a3239714bf8bbe7dab10d707ca379d88236e6daaaf8359869d43f9fefcb77
MD5 756caa7e1aa4f4de4cbff2ce8b777054
BLAKE2b-256 7211cad5a293acf87ef528062a7e3ca6fff6aabaf7c9fa2e5c904a57829e4302

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page