Pyvim
Pyvim is a pure Python implementation of the Vim text editor, built upon the `prompt_toolkit` library. It offers essential Vim-like features such as syntax highlighting (via Pygments), horizontal and vertical splits, tab pages, and Vi key bindings. The project, currently at version 3.0.3, serves as a demonstration of `prompt_toolkit`'s capabilities and includes integrations for Python code completion (Jedi) and linting (Pyflakes). Its release cadence is irregular, with the last update in May 2022.
Common errors
-
ModuleNotFoundError: No module named 'pyVim'
cause Attempting to import 'pyVim' (with a capital 'V') instead of 'pyvim' (lowercase 'v'). The 'pyVim' module is part of the `pyvmomi` library (VMware vSphere SDK), which is a completely different project.fixEnsure you are using `pip install pyvim` (lowercase) and importing `pyvim` if you are trying to access its internal components (though it's primarily a command-line tool). If you intended to use the VMware SDK, install `pyvmomi` with `pip install pyvmomi`. -
pyvim: command not found
cause The `pyvim` executable is not in your system's PATH, or the installation was incomplete.fixVerify that `pyvim` is installed correctly (`pip show pyvim`). Ensure your Python environment's script directory is included in your system's PATH. On Linux/macOS, this is often `~/.local/bin` or a virtual environment's `bin` directory.
Warnings
- gotcha Performance can significantly degrade when editing large files (thousands of lines) with syntax highlighting enabled due to Python's string handling and Pygments processing.
- gotcha Pyvim's buffer implementation uses simple Python strings, which can be inefficient for extremely large files and may not scale as well as optimized text editor data structures.
- gotcha The documentation on GitHub states compatibility up to Python 3.4. While PyPI's `requires_python` is empty (implying broader Python 3 support), explicit modern Python 3 compatibility (e.g., 3.9+) is not clearly documented by the project author.
- breaking Lack of individual cursor support across split windows can lead to unexpected behavior where the cursor in one window affects other splits, including unintended scrolling.
Install
-
pip install pyvim
Imports
- pyvim (executable)
from pyvim import Editor
Run 'pyvim' directly from the terminal or via subprocess
- pyVim (case-sensitive)
from pyVim import connect
pip install pyvim (for this library), pip install pyvmomi (for VMware SDK)
Quickstart
import subprocess
import os
# Create a dummy file for editing
file_content = "Hello, pyvim!\nThis is a test file.\n"
with open("example.txt", "w") as f:
f.write(file_content)
print("Launching pyvim to edit example.txt. Press ESC, then :wq to save and exit.")
try:
# Launch pyvim as a subprocess
# The input/output will be connected to the current terminal
subprocess.run(["pyvim", "example.txt"])
except FileNotFoundError:
print("Error: 'pyvim' executable not found. Make sure it's installed and in your PATH.")
except Exception as e:
print(f"An error occurred: {e}")
# Optionally, print the content after editing
if os.path.exists("example.txt"):
with open("example.txt", "r") as f:
print("\nContent of example.txt after editing:")
print(f.read())