local state = { floating = { buf = -1, win = -1 } } function create_floating_window(opts) -- Set default options if not provided opts = opts or {} opts.width = opts.width or math.floor(vim.o.columns * 0.8) -- Default 80% of screen width opts.height = opts.height or math.floor(vim.o.lines * 0.8) -- Default 80% of screen height -- Create buffer local buf = nil if vim.api.nvim_buf_is_valid(opts.buf) then buf = opts.buf else buf = vim.api.nvim_create_buf(false, true) -- no file, scratch buffer end -- Calculate center position local row = math.floor((vim.o.lines - opts.height) / 2) local col = math.floor((vim.o.columns - opts.width) / 2) -- Floating window options local win_opts = { relative = "editor", width = opts.width, height = opts.height, col = col, row = row, style = "minimal", border = "rounded", -- You can use "none", "single", "double", or "rounded" } -- Create the floating window local win = vim.api.nvim_open_win(buf, true, win_opts) return { buf = buf, win = win } end -- Emable usage: vim.api.nvim_create_user_command("FloatTerminal", function() if not vim.api.nvim_win_is_valid(state.floating.win) then state.floating = create_floating_window({ buf = state.floating.buf }) if vim.bo[state.floating.buf].buftype ~= "terminal" then vim.cmd.term() end else vim.api.nvim_win_hide(state.floating.win) end end, {})