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 the 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
filenameif 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 iffilenameis used. -
encoding: To encode the content and decode the result,
texteditor.open()uses the default encoding for the platform, but you can use anencodingargument 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 tries to guess.
To do so, the function search through a very short list of the most popular editors, and use the first one that founds.
On MacOS, as fallback, it calls the system default for editing that file extension.
You might notice that vim and Emacs are not in the short list of editors, that's because:
- If you are using either of them, you know what the
EDITORvariable is, and you probably has set it already. - 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file texteditor-1.4.1.tar.gz.
File metadata
- Download URL: texteditor-1.4.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab53fb8e2df2212eace989913210b1b10991a26718c4ee8f70928d81276095ae
|
|
| MD5 |
75aa5e805da576443b659ebe0f8d94df
|
|
| BLAKE2b-256 |
af24c0b5387db51f984a1e7c17a46fff198142e8c51c74b9e54a050d90ec7d86
|
File details
Details for the file texteditor-1.4.1-py3-none-any.whl.
File metadata
- Download URL: texteditor-1.4.1-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e19e587e795cac056b7470969d8dc187af73f2cf23eae279904fdecbe38692b3
|
|
| MD5 |
a3230f5dae1c2cf20b6cf7991610956f
|
|
| BLAKE2b-256 |
fc0e163918a10c65be59dc9e887599a6a6f8e0bc9f389f7f1aab95d60ceb7510
|