Skip to main content

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

Project description

vimbuffer

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

Installation

Requires:

  • python3+
pip3 install vimbuffer

Usage

There's just the one function, buffer:

vimbuffer.buffer(string=None, file=None, editor=None, fallbacks=[], name_prefix=None)
    Provide one of: 
        string: A string to edit in a vimbuffer
        filepath: A file to edit in a vimbuffer
    If neither is provided, uses an empty string
    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 and editor, lets the user edit it,
    and returns the string.
    If a file is, it reads the file, lets the user modify it, 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
  • 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

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

vimbuffer-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

File details

Details for the file vimbuffer-0.1.0.tar.gz.

File metadata

  • Download URL: vimbuffer-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/42.0.2 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

File hashes

Hashes for vimbuffer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cdf470cf7736236f0a3528833791b533ba17ed46c0dd429d24980c3f4691f0ec
MD5 ad31d57aea73c890e004e582a36a773f
BLAKE2b-256 720d7cb93cacc38c14ca7ac7371078264ebcad7d0489d058f5b6408c45bad197

See more details on using hashes here.

Supported by

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