img-clip.nvim

repository·main·Indexed 21 days ago

https://github.com/hakonharnes/img-clip.nvim

A Neovim plugin for embedding images into markup languages like LaTeX, Markdown, or Typst. It supports pasting images from the system clipboard, dragging and dropping files, and programmatic embedding via a Lua API. Features include dynamic configuration, image processing via shell commands (e.g., ImageMagick), project-specific settings via .img-clip.lua, and integrations with Telescope.nvim, Oil.nvim, and Snacks.nvim.

Tokens
5.1K
Snippets
18
Records
21
Agent score
26%

What's inside img-clip.nvim

  1. Override options for specific files, directories, or custom triggers

    main

    The plugin supports granular overrides. Options are evaluated in the following order of precedence (highest to lowest):

    1. Custom options: Based on a trigger function.
    2. File specific options: Based on absolute path or filename.
    3. Directory specific options: Based on absolute path.
    4. Filetype specific options: Based on the buffer's filetype.
    5. Default options: The global configuration.

    Options can be nested. For example, you can define filetype-specific settings inside a directory-specific block.

    -- file specific options
    files = {
      ["/path/to/specific/file.md"] = {
        template = "Custom template for this file",
      },
      ["README.md"] = {
        template = "Custom template for README.md",
      },
    },
    
    -- directory specific options
    dirs = {
      ["/path/to/project"] = {
        template = "Project specific template",
    
        filetypes = {
          markdown = {
            template = "markdown template"
          }
        },
    
        files = {
          ["readme.md"] = {
            dir_path = "images"
          },
        },
      },
    },
    
    -- custom options
    custom = {
      {
        trigger = function()
          return vim.fn.strftime("%A") == "Monday"
        end,
        template = "Template for Mondays only",
      },
    }
  2. Use drag and drop to embed images

    main

    You can drag images from a web browser or file explorer directly into your terminal to automatically embed them in Neovim while in normal mode.

    To enable drag and drop in insert mode, set the drag_and_drop.insert_mode option to true in your configuration.

    Terminal Requirements

    For drag and drop to function, your terminal emulator must:

    1. Paste the file path or URL of the image when dropped.
    2. Support bracketed paste mode so Neovim can distinguish pasted text from typed text.

    Terminal Compatibility Summary

    • Kitty: Supports Files and URLs on X11 and Wayland/macOS.
    • Alacritty: Supports Files on X11 and macOS.
    • Foot: Supports Files and URLs on Wayland.
    • Windows Terminal: Supports Files and URLs on Windows.
    • iTerm.app / Terminal.app: Support Files and URLs on macOS.
    • Konsole: Supports Files and URLs on X11.

    Troubleshooting

    • Windows: If drag and drop fails, try changing your default shell to powershell or pwsh (see :h shell-powershell).
    • macOS: Image URLs are only supported when using Safari.
  3. Use project-specific settings with .img-clip.lua

    main

    To apply settings to a specific project, create a .img-clip.lua file in the project root. The plugin automatically loads the closest such file found in the current file's parent directories. The file must return a Lua table containing the options.

    return {
      default = {
        template = "default template"
      },
    
      filetypes = {
        markdown = {
          template = "markdown template"
        }
      },
    }
  4. Check requirements for img-clip.nvim

    main

    Depending on your operating system, you must have specific clipboard utilities installed for the plugin to function:

    • Linux (X11): xclip
    • Linux (Wayland): wl-clipboard
    • MacOS: pngpaste
    • Windows: No additional requirements

    After installation, run :checkhealth img-clip within Neovim to verify that your environment is correctly configured.

  5. Process images with process_cmd

    main

    The process_cmd option allows you to specify a shell command to process the image before saving or embedding it as base64. The command must read image data from standard input (stdin) and write the processed data to standard output (stdout).

    Examples using ImageMagick:

    • Compress to 85% quality: convert - -quality 85 -
    • Resize to 50%: convert - -resize 50% -
    • Convert to grayscale: convert - -colorspace Gray -
    process_cmd = "convert - -quality 85 -" -- compress the image with 85% quality
    process_cmd = "convert - -resize 50% -" -- resize the image to 50% of its original size
    process_cmd = "convert - -colorspace Gray -" -- convert the image to grayscale
  6. Enable drag and drop for images

    main

    The drag and drop feature allows you to drag images from a web browser or file explorer into the terminal to automatically embed them. By default, this works in Normal mode. To enable it in Insert mode, set the drag_and_drop.insert_mode option to true in your configuration.

    Terminal Requirements

    For drag and drop to function, your terminal emulator must:

    1. Paste the file path or URL of the image when dropped.
    2. Support bracketed paste mode so Neovim can distinguish pasted text from typed text.

    Terminal Compatibility Summary

    TerminalX11 (File/URL)Wayland (File/URL)MacOS (File/URL)Windows (File/URL)
    KittyYes/YesYes/YesYes/YesN/A
    KonsoleYes/YesN/TN/AN/A
    AlacrittyYes/NoNo/NoYes/NoYes/No
    WeztermNo/NoN/TYes/NoYes/No
    FootN/AYes/YesN/AN/A
    Terminal.appN/AN/AYes/YesN/A
    iTerm.appN/AN/AYes/YesN/A
    Windows TerminalN/AN/AN/AYes/Yes

    Note: MacOS URLs only work in Safari. If having issues on Windows, try using powershell or pwsh as your default shell.

    -- Example configuration to enable drag and drop in insert mode
    require("img-clip").setup({
      drag_and_drop = {
        insert_mode = true,
      },
    })
  7. Install img-clip.nvim using lazy.nvim

    main

    To install img-clip.nvim using lazy.nvim, add the following configuration to your plugin list. It is recommended to load the plugin on VeryLazy and map a key (like <leader>p) to the PasteImage command for quick access.

    return {
      "HakonHarnes/img-clip.nvim",
      event = "VeryLazy",
      opts = {
        -- add options here
        -- or leave it empty to use the default settings
      },
      keys = {
        -- suggested keymap
        { "<leader>p", "<cmd>PasteImage<cr>", desc = "Paste image from system clipboard" },
      },
    }
  8. Verify requirements for img-clip.nvim

    main

    Depending on your operating system, you must have specific clipboard utilities installed for the plugin to function correctly:

    • Linux (X11): xclip
    • Linux (Wayland): wl-clipboard
    • macOS: pngpaste
    • Windows: No additional requirements

    After installation, run :checkhealth img-clip within Neovim to verify that your environment meets these requirements.

  9. Use .img-clip.lua for project-specific settings

    main

    You can create a .img-clip.lua file in your project root to define project-specific settings. The plugin automatically loads the closest such file found in the current file's parent directories. The file must return a Lua table containing options (similar to opts in lazy.nvim).

    return {
      default = {
        template = "default template"
      },
    
      filetypes = {
        markdown = {
          template = "markdown template"
        }
      },
    }
  10. Configure options for specific filetypes

    main

    You can override default settings for specific filetypes using the filetypes table. The key should be the filetype string (as returned by :help filetype).

    Example: Using absolute paths specifically for LaTeX (tex):

    filetypes = {
      tex = {
        use_absolute_path = true
      }
    }
  11. Configure filetype-specific options

    main

    You can override default settings for specific filetypes using the filetypes table. The key should be the filetype string (as returned by :help filetype).

    Example: Setting absolute paths for LaTeX (tex) files:

    filetypes = {
      tex = {
        use_absolute_path = true
      }
    }
  12. Configure img-clip.nvim via Setup

    main

    The plugin is highly configurable using a Lua table. You can provide static values or dynamic functions for most options. Configuration can be applied globally (default), per filetype, per file, per directory, or via custom triggers.

    {
      default = {
        -- file and directory options
        dir_path = "assets",
        extension = "png",
        file_name = "%Y-%m-%d-%H-%M-%S",
        use_absolute_path = false,
        relative_to_current_file = false,
    
        -- logging options
        verbose = true,
    
        -- template options
        template = "$FILE_PATH",
        url_encode_path = false,
        relative_template_path = true,
        use_cursor_in_template = true,
        insert_mode_after_paste = true,
        insert_template_after_cursor = true,
    
        -- prompt options
        prompt_for_file_name = true,
        show_dir_path_in_prompt = false,
    
        -- base64 options
        max_base64_size = 10,
        embed_image_as_base64 = false,
    
        -- image options
        process_cmd = "",
        copy_images = false,
        download_images = true,
        formats = { "jpeg", "jpg", "png" },
    
        -- drag and drop options
        drag_and_drop = {
          enabled = true,
          insert_mode = false,
        },
      },
    
      filetypes = {
        markdown = {
          url_encode_path = true,
          template = "![$CURSOR]($FILE_PATH)",
          download_images = false,
        },
        -- ... other filetypes
      },
    
      files = {},
      dirs = {},
      custom = {},
    }