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.
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.
text = texteditor.open(filename='README.md')
# text = texteditor.open("This will be used instead of the file content", filename='README.md')
You can also edit an existing text file. If the file cannot be opened, an OSError
is raised.
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 iffilename
is used. -
encoding: To encode the content and decode the result,
texteditor.open()
uses the default encoding for the platform, but you can use anencoding
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:
- If you are using it, you know what the
EDITOR
variable 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
File details
Details for the file texteditor-1.0.6.tar.gz
.
File metadata
- Download URL: texteditor-1.0.6.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6faa3478d0ffd879f55504ebac60403a1bd94c761d061444e11a6cb87f6a8689 |
|
MD5 | b3efa76d714ee6798061bac7230ea55b |
|
BLAKE2b-256 | 2663888b2b42362ad904b62285e81f77273deb3b9a8657d57ac594c50b595cf5 |
File details
Details for the file texteditor-1.0.6-py3-none-any.whl
.
File metadata
- Download URL: texteditor-1.0.6-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f4f1bec42e22f971b77caebef82d33d5758dc75c0a0b3185a33aad7423a7f9e |
|
MD5 | e865f10e921c0539af354ce1014a6cb1 |
|
BLAKE2b-256 | 18f6e8b918ab3c02d355fd23b0590c497ddba796945fe812fed7646a6a7576e1 |