Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to do dynamic custom background highlighting #959

Open
oscarmorrison opened this issue Dec 19, 2024 · 0 comments
Open

How to do dynamic custom background highlighting #959

oscarmorrison opened this issue Dec 19, 2024 · 0 comments

Comments

@oscarmorrison
Copy link

oscarmorrison commented Dec 19, 2024

@lukas-reineke would love to be able to add this to the wiki? or maybe add an examples section to the repo.
Spent a long time working this out:

Dynamic Background Stripes with indent-blankline.nvim

This guide explains how to set up alternating background stripes for indentation levels in Neovim using indent-blankline.nvim. The configuration dynamically adapts the background colors based on the active colorscheme, ensuring seamless integration with both dark and light themes.


Features

  • Dynamic Highlight Groups: Automatically adjusts colors for both dark and light themes.
  • Alternating Background Stripes: Provides subtle visual cues for nested indentation levels using background colors.
  • No Characters: Relies on background highlights instead of displaying visible characters, maintaining a clean and minimal aesthetic.
  • Dynamic Colorscheme Support: Automatically updates the colors when switching between themes.

Configuration

Lua Code

(lazyvim in a /plugins/indent-blank.lua

return {
  {
    "lukas-reineke/indent-blankline.nvim",
    main = "ibl",
    event = { "BufReadPre", "BufNewFile" },
    config = function()
      -- Function to dynamically calculate colors based on the background
      local function setup_highlight_groups()
        local background = vim.o.background -- 'dark' or 'light'
        if background == "dark" then
          -- Dark theme colors
          vim.api.nvim_set_hl(0, "IndentStripe1", { bg = "#1B2B34" }) -- Original dark background
          vim.api.nvim_set_hl(0, "IndentStripe2", { bg = "#232E38" }) -- Slightly lighter
        else
          -- Light theme colors
          vim.api.nvim_set_hl(0, "IndentStripe1", { bg = "#FDF6E3" }) -- Light theme background
          vim.api.nvim_set_hl(0, "IndentStripe2", { bg = "#F6EFD9" }) -- Slightly darker
        end
      end

      -- Set up highlight groups for the current colorscheme
      setup_highlight_groups()

      -- Autocmd to update highlight groups when the colorscheme changes
      vim.api.nvim_create_autocmd("ColorScheme", {
        callback = setup_highlight_groups,
      })

      -- Configure indent-blankline.nvim
      require("ibl").setup({
        indent = {
          highlight = { "IndentStripe1", "IndentStripe2" }, -- Alternating subtle backgrounds
          char = "", -- No character, only background
        },
        whitespace = {
          highlight = { "IndentStripe1", "IndentStripe2" },
          remove_blankline_trail = false,
        },
        scope = { enabled = false },
      })
    end,
  },
}

Dynamic Background Colors

The configuration dynamically detects the current theme using vim.o.background:

  • Dark Themes:
    • IndentStripe1: #1B2B34 (dark base color)
    • IndentStripe2: #232E38 (slightly lighter)
  • Light Themes:
    • IndentStripe1: #FDF6E3 (light base color)
    • IndentStripe2: #F6EFD9 (slightly darker)

image

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant