image.nvim

repository·master·Indexed 24 days ago

https://github.com/3rd/image.nvim

A Neovim plugin that enables image rendering within the editor. It supports rendering backends including Kitty's Graphics Protocol, Sixel, and Überzug++, and requires ImageMagick for image processing via either the magick_cli or magick_rock processors. The plugin provides integrations for various filetypes, a dedicated image API for loading and manipulating images from files or URLs, and support for Tmux >= 3.3.

Tokens
2.4K
Snippets
8
Records
10
Agent score
35%

What's inside image.nvim

  1. Choose a rendering backend

    master

    You must select one of the following rendering backends for image.nvim to work:

    • kitty (recommended): Requires Kitty >= 28.0 or a terminal implementing Kitty's Graphics Protocol (e.g., WezTerm, though performance may vary). Offers best performance, native clipping, and caching.
    • ueberzug: Requires Überzug++. Works with any terminal emulator but has significantly lower performance.
    • sixel: Works with Sixel-supporting terminals (e.g., XTerm, WezTerm, foot). Requires ImageMagick with Sixel support. Performance is lower, but can be improved using only_render_image_at_cursor=true and only_render_image_at_cursor_mode="popup".
  2. Install image.nvim using Lazy.nvim

    master

    Depending on which ImageMagick processor you chose, use the corresponding configuration for Lazy.nvim.

    Using magick_cli (Default)

    Use this if you installed ImageMagick via your system package manager.

    Using magick_rock (FFI)

    Use this for better performance. Requires a LuaRocks setup.

    For Lazy >= v11.*:

    require("lazy").setup({
        rocks = {
            hererocks = true,  -- recommended if you do not have global installation of Lua 5.1.
        },
        spec = {
            {
                "3rd/image.nvim",
                opts = {}
            },
        },
    })

    For Lazy < v11.x (using luarocks.nvim):

    {
        "vhyrro/luarocks.nvim",
        priority = 1001,
        opts = {
            rocks = { "magick" },
        },
    },
    {
        "3rd/image.nvim",
        dependencies = { "luarocks.nvim" },
        opts = {}
    }
    {
        "3rd/image.nvim",
        build = false,
        opts = {
            processor = "magick_cli",
        }
    }
  3. Quick start for image.nvim

    master

    To get the best experience with image.nvim, follow these steps in order:

    1. Install the Kitty terminal.
    2. Install ImageMagick.
    3. Install the image.nvim plugin and configure it.
    4. (Optional) Configure Tmux if you use it.
    5. Check the 'How to ...?' section in the documentation.
    nvim --clean -c ":luafile minimal-setup.lua"
  4. Configure Tmux for image.nvim

    master

    To enable first-class support for Tmux, ensure you are using Tmux >= 3.3 and add the following settings to your tmux.conf:

    set -g allow-passthrough on
    set -g visual-activity off
    set-option -g focus-events on
    set -g allow-passthrough on
    set -g visual-activity off
    set-option -g focus-events on
  5. Install ImageMagick for image processing

    master

    ImageMagick is required to convert, scale, and crop images. You can use either the magick_cli processor (default) or the magick_rock processor (FFI bindings).

    Installation by OS

    Arch

    sudo pacman -S imagemagick

    Ubuntu/Debian

    • For magick_cli: sudo apt install imagemagick
    • For magick_rock: sudo apt install libmagickwand-dev

    Fedora

    • For magick_cli: sudo dnf install ImageMagick
    • For magick_rock: sudo dnf install ImageMagick-devel

    macOS

    • Homebrew: brew install imagemagick (Note: You may need to add $(brew --prefix)/lib to DYLD_FALLBACK_LIBRARY_PATH in your shell profile).
    • MacPorts: sudo port install imagemagick (Note: You must add /opt/local/lib to DYLD_FALLBACK_LIBRARY_PATH).

    NixOS

    Install the imagemagick package. For magick_rock, you also need luajitPackages.magick.

  6. Configure image.nvim via setup()

    master

    Use require("image").setup() to configure the plugin's behavior. Key configuration areas include:

    • backend: The rendering engine. Options are "kitty" (recommended), "ueberzug", or "sixel".
    • processor: The image processing engine. Options are "magick_cli" or "magick_rock".
    • integrations: A table of per-filetype settings (e.g., markdown, asciidoc, neorg, rst, typst, html, css).
    • hijack_file_patterns: A list of file extensions (e.g., "*.png", "*.jpg") that, when opened, will render the file as an image.
    • max_height_window_percentage: Controls the maximum height of rendered images relative to the window.
    require("image").setup({
      backend = "kitty", -- or "ueberzug" or "sixel"
      processor = "magick_cli", -- or "magick_rock"
      integrations = {
        markdown = {
          enabled = true,
          clear_in_insert_mode = false,
          download_remote_images = true,
          only_render_image_at_cursor = false,
          only_render_image_at_cursor_mode = "popup", -- or "inline"
          floating_windows = false,
          filetypes = { "markdown", "vimwiki" },
        },
        -- ... other integrations
      },
      max_height_window_percentage = 50,
      hijack_file_patterns = { "*.png", "*.jpg", "*.jpeg", "*.gif", "*.webp", "*.avif" },
    })
  7. Render images only under the cursor

    master

    To reduce visual clutter, you can configure integrations to only render an image when the cursor is positioned on it. This is controlled via only_render_image_at_cursor and only_render_image_at_cursor_mode (which can be "popup" or "inline").

    require("image").setup({
      integrations = {
        markdown = {
            only_render_image_at_cursor = true,
            only_render_image_at_cursor_mode = "popup",
        }
    })
  8. Use the image API to load and manipulate images

    master

    The image module provides methods to load images from local files or URLs and manipulate their properties.

    Loading Images

    • api.from_file(path, options): Loads an image from an absolute path.
    • api.from_url(url, options, callback): Loads an image from a URL. The callback receives the image object.

    Image Options

    • id: Optional string ID (defaults to random).
    • window: Optional window ID to bind the image to.
    • buffer: Optional buffer ID to bind the image to.
    • with_virtual_padding: If true, pads vertically with extmarks.
    • overlap: Optional positive integer for lines covered by the image.
    • inline: If true, binds image to an extmark (required if with_virtual_padding is true).
    • x, y, width, height: Geometry settings.

    Image Methods

    • image:render(): Renders the image.
    • image:render(geometry): Updates geometry and renders.
    • image:clear(): Clears the image.
    • image:move(x, y): Moves the image.
    • image:brightness(value): Changes brightness.
    • image:saturation(value): Changes saturation.
    • image:hue(value): Changes hue.
    local api = require("image")
    
    -- from a file
    local image = api.from_file("/path/to/image.png", {
      id = "my_image_id",
      with_virtual_padding = true,
      inline = true,
      x = 1,
      y = 1,
      width = 10,
      height = 10
    })
    
    -- from a URL
    api.from_url("https://gist.ro/s/remote.png", {
        -- options
    }, function(img)
        -- callback
    end)
    
    image:render()
    image:move(1, 1)
    image:brightness(0.5)
    image:clear()
  9. Customize image path resolution per integration

    master

    You can override how images are located by providing a resolve_image_path function within a specific integration's configuration. This is useful for specialized setups like Obsidian.

    Arguments passed to the function:

    • document_path: The path to the file containing the image.
    • image_path: The potentially relative path to the image (e.g., "![](image.png)" in Markdown).
    • fallback: A function to call to get the default behavior.
    require('image').setup({
      integrations = {
        markdown = {
          resolve_image_path = function(document_path, image_path, fallback)
            -- document_path is the path to the file that contains the image
            -- image_path is the potentially relative path to the image.
    
            return fallback(document_path, image_path)
          end,
        }
      }
    })
  10. Enable or disable the plugin on demand

    master

    Use the following functions to control the plugin's active state:

    • require("image").enable(): Enables the plugin.
    • require("image").disable(): Disables the plugin.
    • require("image").is_enabled(): Returns a boolean indicating if the plugin is active.
    require("image").enable()
    require("image").disable()
    print(require("image").is_enabled()) -- returns true/false