A Neovim plugin providing integration with the uv Python package manager, offering a smooth workflow for Python development in Neovim.
- Run Python code directly from Neovim
- Execute selected code snippets with context preservation
- Run specific functions with automatic module imports
- Manage Python packages with uv commands
- Automatically activate virtual environments
- Integration with UI pickers (like Telescope and Snacks.nvim)
uvnvim.mp4
Using lazy.nvim:
return {
"benomahony/uv.nvim",
-- Optional filetype to lazy load when you open a python file
-- ft = { python }
-- Optional dependency, but recommended:
-- dependencies = {
-- "folke/snacks.nvim"
-- or
-- "nvim-telescope/telescope.nvim"
-- },
opts = {
picker_integration = true,
},
}Using packer.nvim:
use {
'benomahony/uv.nvim',
-- Optional filetype to lazy load when you open a python file
-- ft = { python }
-- Optional dependency, but recommended:
-- requires = {
-- "folke/snacks.nvim"
-- or
-- "nvim-telescope/telescope.nvim"
-- },
config = function()
require('uv').setup()
end
}You can customize any part of the configuration to fit your workflow.
- Neovim 0.7.0 or later
- uv installed on your system
- For UI picker integration, a compatible UI picker (like Snacks.nvim or Telescope.nvim)
uv.nvim provides several commands:
:UVInit- Initialize a new uv project:UVRunFile- Run the current Python file:UVRunSelection- Run the selected Python code:UVRunFunction- Run a specific function from the current file
All keymaps use the <leader>x prefix by default:
<leader>x- Show uv commands menu (requires UI picker integration)<leader>xr- Run current file<leader>xs- Run selected code (in visual mode)<leader>xf- Run a specific function<leader>xe- Environment management<leader>xi- Initialize uv project<leader>xa- Add a package<leader>xd- Remove a package<leader>xc- Sync packages
The plugin intelligently handles running selected code by:
- Preserving imports from the file
- Including global variables for context
- Auto-detecting code type (function, expression, etc.)
- Adding appropriate wrappers when needed
Example:
# In your file
import numpy as np
data = np.array([1, 2, 3])
# Select this function and run with <leader>xs
def process_data(arr):
return arr.mean()The plugin will automatically:
- Include the import
- Include the global variable
data - Add a call to the function if not present
When using :UVRunFunction or <leader>xf, the plugin:
- Scans the current file for all function definitions
- Shows a picker to select which function to run
- Creates a proper module import context for the function
- Captures and displays return values
This plugin integrates with Snacks.nvim for UI components:
- Command picker
- Environment management
- Function selection
For advanced usage or integration with other plugins:
local uv = require('uv')
-- Run a command with uv
uv.run_command('uv add pandas')
-- Activate a virtual environment
uv.activate_venv('/path/to/venv')
-- Run the current file
uv.run_file()
-- Run selected code
uv.run_python_selection()
-- Run a specific function
uv.run_python_function()require('uv').setup({
keymaps = {
prefix = "<leader>u", -- Change prefix to <leader>u
-- Disable specific keymaps
venv = false,
init = false,
}
})require('uv').setup({
execution = {
run_command = "python -m", -- Use standard Python instead of uv
}
})require('uv').setup({
keymaps = false -- Disable all keymaps
})- Command execution errors: Make sure uv is installed and in your PATH.
- Virtual environment not activating: Check if the
.venvdirectory exists in your project root. - Output not showing: Check notification settings in your Neovim configuration.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
Here's the default configuration with all available options:
require('uv').setup({
-- Auto-activate virtual environments when found
auto_activate_venv = true,
notify_activate_venv = true,
-- Auto commands for directory changes
auto_commands = true,
-- Integration with snacks picker
picker_integration = true,
-- Keymaps to register (set to false to disable)
keymaps = {
prefix = "<leader>x", -- Main prefix for uv commands
commands = true, -- Show uv commands menu (<leader>x)
run_file = true, -- Run current file (<leader>xr)
run_selection = true, -- Run selected code (<leader>xs)
run_function = true, -- Run function (<leader>xf)
venv = true, -- Environment management (<leader>xe)
init = true, -- Initialize uv project (<leader>xi)
add = true, -- Add a package (<leader>xa)
remove = true, -- Remove a package (<leader>xd)
sync = true, -- Sync packages (<leader>xc)
sync_all = true, -- Sync all packages, extras and groups (<leader>xC)
},
-- Execution options
execution = {
-- Python run command template
run_command = "uv run python",
-- Show output in notifications
notify_output = true,
-- Notification timeout in ms
notification_timeout = 10000,
},
})