├── stylua.toml ├── plugin └── transparent.vim ├── README.md └── lua └── transparent.lua /stylua.toml: -------------------------------------------------------------------------------- 1 | column_width = 100 2 | indent_type = "Spaces" 3 | quote_style = "AutoPreferDouble" 4 | indent_width = 2 5 | -------------------------------------------------------------------------------- /plugin/transparent.vim: -------------------------------------------------------------------------------- 1 | if exists('g:loaded_transparent') | finish | endif 2 | 3 | let g:loaded_transparent = 1 4 | 5 | augroup transparent 6 | autocmd! 7 | autocmd VimEnter,ColorScheme * lua require('transparent').clear_bg() 8 | command -bar -nargs=0 TransparentEnable lua require('transparent').toggle_transparent(true) 9 | command -bar -nargs=0 TransparentDisable lua require('transparent').toggle_transparent(false) 10 | command -bar -nargs=0 TransparentToggle lua require('transparent').toggle_transparent() 11 | augroup END 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nvim-transparent 2 | 3 | Remove all background colors to make nvim transparent. 4 | 5 | ![screenshot](https://user-images.githubusercontent.com/47070852/124546661-9353ce80-de5d-11eb-81ba-f8282e034d9f.gif) 6 | 7 | --- 8 | 9 | ## Usage 10 | 11 | Example config 12 | 13 | ```lua 14 | require("transparent").setup({ 15 | enable = true, -- boolean: enable transparent 16 | extra_groups = { -- table/string: additional groups that should be cleared 17 | -- In particular, when you set it to 'all', that means all available groups 18 | 19 | -- example of akinsho/nvim-bufferline.lua 20 | "BufferLineTabClose", 21 | "BufferlineBufferSelected", 22 | "BufferLineFill", 23 | "BufferLineBackground", 24 | "BufferLineSeparator", 25 | "BufferLineIndicatorSelected", 26 | }, 27 | exclude = {}, -- table: groups you don't want to clear 28 | }) 29 | ``` 30 | 31 | You can also set the `groups` option to override the default groups. 32 | 33 | The default groups: 34 | `Normal` `NormalNC` `Comment` `Constant` `Special` `Identifier` `Statement` `PreProc` `Type` `Underlined` 35 | `Todo` `String` `Function` `Conditional` `Repeat` `Operator` `Structure` `LineNr` `NonText` `SignColumn` `CursorLineNr`. 36 | 37 | --- 38 | 39 | The global variable `g:transparent_enabled` has greater priority to option `enable`. 40 | Some plugins or themes support setting transparency, and you can use this 41 | variable as a flag. eg: `vim.g.tokyonight_transparent = vim.g.transparent_enabled` 42 | 43 | **Disable by default** 44 | 45 | ```vim 46 | let g:transparent_enabled = v:false 47 | ``` 48 | 49 | ## Commands 50 | 51 | ``` 52 | :TransparentEnable 53 | :TransparentDisable 54 | :TransparentToggle 55 | ``` 56 | 57 | ## Aknowledgement 58 | 59 | [vim-transparent](https://github.com/Kjwon15/vim-transparent) 60 | -------------------------------------------------------------------------------- /lua/transparent.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | local config = { 4 | enable = false, 5 | groups = { 6 | "Normal", 7 | "NormalNC", 8 | "Comment", 9 | "Constant", 10 | "Special", 11 | "Identifier", 12 | "Statement", 13 | "PreProc", 14 | "Type", 15 | "Underlined", 16 | "Todo", 17 | "String", 18 | "Function", 19 | "Conditional", 20 | "Repeat", 21 | "Operator", 22 | "Structure", 23 | "LineNr", 24 | "NonText", 25 | "SignColumn", 26 | "CursorLineNr", 27 | "EndOfBuffer", 28 | }, 29 | extra_groups = {}, 30 | exclude = {}, 31 | } 32 | 33 | local clear_group_bg = function(group, highlights) 34 | if not (group or highlights) then 35 | return 36 | end 37 | 38 | if group and vim.fn.highlight_exists(group) == 0 then 39 | return 40 | end 41 | 42 | group = group or vim.split(highlights, " ")[1] 43 | highlights = highlights or vim.api.nvim_exec("hi " .. group, true) 44 | 45 | if vim.tbl_contains(config.exclude, group) or highlights:match("links to") then 46 | return 47 | end 48 | 49 | local ok, err = pcall(vim.cmd, string.format("hi %s ctermbg=NONE guibg=NONE", group)) 50 | if not ok then 51 | vim.api.nvim_echo( 52 | { 53 | { 54 | string.format("[transparent]:error occurs when setting highlight `%s`: %s", group, err), 55 | "ErrorMsg", 56 | }, 57 | }, 58 | true, 59 | {} 60 | ) 61 | end 62 | end 63 | 64 | local function _clear_bg() 65 | if vim.g.transparent_enabled ~= true then 66 | return 67 | end 68 | for _, group in ipairs(config.groups) do 69 | clear_group_bg(group) 70 | end 71 | 72 | if type(config.extra_groups) == "string" then 73 | if config.extra_groups == "all" then 74 | local hls = vim.split(vim.api.nvim_exec("highlight", true), "\n") 75 | for _, hl in ipairs(hls) do 76 | clear_group_bg(nil, hl) 77 | end 78 | else 79 | clear_group_bg(config.extra_groups) 80 | end 81 | else 82 | for _, group in ipairs(config.extra_groups) do 83 | clear_group_bg(group) 84 | end 85 | end 86 | end 87 | 88 | function M.clear_bg() 89 | if vim.g.transparent_enabled ~= true then 90 | return 91 | end 92 | -- ? some plugins calculate colors from basic highlights 93 | -- : clear immediately 94 | _clear_bg() 95 | -- ? some plugins use autocommands to redefine highlights 96 | -- : clear again after a while 97 | vim.defer_fn(_clear_bg, 500) 98 | -- again 99 | vim.defer_fn(_clear_bg, 1000) 100 | -- yes, clear 4 times!!! 101 | vim.defer_fn(_clear_bg, 5000) 102 | end 103 | 104 | function M.toggle_transparent(option) 105 | if option == nil then 106 | vim.g.transparent_enabled = not vim.g.transparent_enabled 107 | else 108 | vim.g.transparent_enabled = option 109 | end 110 | if vim.g.colors_name then 111 | vim.cmd("colorscheme " .. vim.g.colors_name) 112 | else 113 | vim.cmd("doautocmd ColorScheme") 114 | end 115 | end 116 | 117 | function M.setup(user_config) 118 | config = vim.tbl_extend("force", config, user_config) 119 | if vim.g.transparent_enabled == nil then 120 | vim.g.transparent_enabled = config.enable 121 | end 122 | end 123 | 124 | return M 125 | --------------------------------------------------------------------------------