
Product
Introducing Webhook Events for Pull Request Scans
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
spawneditor
Advanced tools
A Python module that attempts to provide a common interface for opening an editor at a specified line in a file.
The launched editor will be chosen from, in order:
VISUAL
environment variable, if DISPLAY
is available.EDITOR
environment variable.pip install --user spawneditor
Alternatively clone the Git repository as a Git submodule named spawneditor
.
import spawneditor
# Open an existing file in the default editor.
spawneditor.edit_file("path/to/file.txt")
# A line number may be specified.
spawneditor.edit_file("path/to/file.txt", line_number=123)
# Edits a blank temporary file.
edited_lines = spawneditor.edit_temporary()
# Edits a temporary file with specified content. Unlike `edit_file`,
# `edit_temporary` returns the contents of the edited file.
instructions = [
"Instructions:",
"Enter some text below the line.",
"----",
]
new_contents = spawneditor.edit_temporary(instructions,
line_number=(len(instructions) + 1))
# ... or, if you prefer a single string with newlines:
new_contents = spawneditor.edit_temporary(
sss["Instructions:\nEnter some text below the line.\n----"],
line_number=4,
)
for line in new_contents:
print(line, end="")
spawneditor
know how to invoke the editor at a specified line number?spawneditor
is hard-coded to recognize and invoke some common editors based on
the name of the executed binary.
See the editor_syntax_table
in spawneditor.py
.
Other editors should still work. At worst, the specified file should be opened in the default editor, just not at the specified line.
If your editor isn't supported, I also encourage filing an issue or pull request to add support.
It's tempting to support using, say, a configuration file to allow users to specify how to invoke arbitrary editors. I currently prefer not to because:
spawneditor
support other editors directly.One pathological situation where an override would be necessary is if an editor uses the same executable name as one of the recognized editors and uses a different command-line syntax for specifying the line number. However, that also should be a very rare situation.
One compelling reason might be to allow people to set their default editor to a wrapper script that invokes their actual editor. I'm sympathetic to that, but in the meantime, the wrapper script could be given the same name as the actual editor.
VISUAL
depend on DISPLAY
?Technically VISUAL
should refer to a full-screen editor, not necessarily a
graphical editor. In practice, however, I think that the graphical vs.
text-mode distinction is far more relevant and important than the distinction
between full-screen and line-based editors.
spawneditor.edit_file
immediately return when spawning a multi-document editor (e.g. Visual Studio Code, Sublime Text)?Multi-document editors are typically also single-instance: when the editor is
invoked, a new process will be spawned, but that process then will forward the
request to an existing, primary process. The new, secondary process then will
immediately exit and cause spawneditor.edit_file
to return. Such editors
typically provide a command-line option (e.g. --wait
for Visual Studio Code
and Sublime Text) to keep the secondary process alive until the file is closed
by the primary process. Consult the documentation to your editor.
Yes. The editor
and python-editor
packages are alternatives that also
invoke the default editor. Currently neither one supports opening a file to a
specified line number. Were I aware of them when I wrote spawneditor
, and
possibly I would have tried contributing to them instead. However,
spawneditor
is implemented rather differently than either one, and I'm not
sure that either project would have appreciated drastic changes. There likely
is room for cross-pollination.
Copyright © 2020-2022 James D. Lin.
FAQs
Attempts to open a file at a specified line in the default editor.
We found that spawneditor demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Add real-time Socket webhook events to your workflows to automatically receive pull request scan results and security alerts in real time.
Research
The Socket Threat Research Team uncovered malicious NuGet packages typosquatting the popular Nethereum project to steal wallet keys.
Product
A single platform for static analysis, secrets detection, container scanning, and CVE checks—built on trusted open source tools, ready to run out of the box.