Skip to main content

vimbuffer

Edit files and strings in temporary vim (or some other console editor) buffers.

Installation

Requires:

  • python3.6+
pip3 install vimbuffer

Usage

There's just the one function, buffer:

vimbuffer.buffer(string: Union[str, NoneType] = None,
                 file: Union[str, NoneType] = None,
                 editor: Union[str, NoneType] = None,
                 fallbacks: Union[List[str], NoneType] = None,
                 name_prefix: Union[str, NoneType] = None) -> str
    Provide one of:
        string: A string to edit in a vimbuffer
        file: A file to edit in a vimbuffer
    If neither is provided, uses an empty string
    editor: editor to override the passed fallbacks/environment variable
    fallbacks: A list of fallbacks for alternate editors (e.g. ['vim', 'vi', 'nano'])
    name_prefix: string prefix for the filename when opening in an editor

    If string is provided, opens the file in an editor, lets the user edit it,
    and returns the string.
    If a file is, it reads the file, lets the user modify the contents, and writes
    back to the file. It also returns the edited file contents.

The editor can be overwritten by specifying environment variables, see below for resolution order.

Examples
import vimbuffer

# edit a string
prompt_string = "Edit this and put what you want here!"
edited_text = vimbuffer.buffer(string=prompt_string)  # opens vim

# edit a file
vimbuffer.buffer(file=os.path.expanduser("~/.bashrc"), name_prefix="bashrc-")

This uses tempfile to create temporary files on the system, and launches vim against them. If the environment variable $EDITOR is set to a graphical text editor, the process (which in this case would just launch the graphical editor) would end before the user had a chance to edit it. I recommend using terminal text editors instead.

You can specify a list of fallback editors; one you'd like to use instead of $EDITOR:

project_description="""
## <my-project>

By <your-name>
"""

edited_desc = vimbuffer.buffer(project_description, fallbacks=["nvim", "vim", "vi", "nano"])

Alternatively, if you want to leave your $EDITOR as a graphical text editor, you can set the $VIMBUFFER_EDITOR environment variable, which trumps all other choices. Specifically, the resolution order is:

  • $VIMBUFFER_EDITOR
  • editor
  • fallbacks(s) passed as keyword arguments in python
  • $EDITOR
  • vim
  • vi

The name_prefix exists as a kwarg since temporary files have names that are randomly generated. By passing a prefix, the name becomes something like /tmp/bashrc-sd43Jds, so it may give a hint as to which file/what you're editing

Tests

pytest

Metadata

Release files for vimbuffer 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vimbuffer 0.1.4
File Size Uploaded
vimbuffer-0.1.4.tar.gz 4.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for vimbuffer 0.1.4
File Interpreter ABI Platform
vimbuffer-0.1.4-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / vimbuffer-0.1.4.tar.gz

Download URL vimbuffer-0.1.4.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
e145222c8a4f3ba9fda4a14a3006b85562276cfd5348fc6560b6f3a51b01bac3
BLAKE2b-256 checksum
How to use checksums
033153014716c79940e96af8cf463ed2cffc0635c0a058f0a7d6d94694dfa194
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.2

Release files / vimbuffer-0.1.4-py3-none-any.whl

Download URL vimbuffer-0.1.4-py3-none-any.whl
Size 4.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
67aa32516bcc39fe446fa92202238df0f623b1daec1a391a4a0c22071810afe0
BLAKE2b-256 checksum
How to use checksums
54cb16124848ef67c37e218333a8fdc2e9e6ef21d2c8e2201301d887751d2bd9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/54.1.2 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.2

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page