Skip to content

Commit d25c5e8

Browse files
committed
feat(keymaps): v/split toggle
1 parent d33c67a commit d25c5e8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

nvim/lua/keymaps.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,28 @@ vim.keymap.set('n', '<leader>wn', '<cmd>vnew<CR>', { desc = 'Window vsplit with
4949
vim.keymap.set('n', '<leader>wfh', '<cmd>set winfixheight<CR>', { desc = 'Window fix height' })
5050
vim.keymap.set('n', '<leader>wfw', '<cmd>set winfixwidth<CR>', { desc = 'Window fix width' })
5151

52+
local function toggle_split()
53+
local buf = vim.api.nvim_get_current_buf()
54+
local pos = vim.api.nvim_win_get_cursor(0)
55+
local height_pct = vim.api.nvim_win_get_height(0) / vim.o.lines
56+
local width_pct = vim.api.nvim_win_get_width(0) / vim.o.columns
57+
58+
vim.api.nvim_win_close(0, false)
59+
60+
-- Decide which split to use based on dimensions of current window
61+
if height_pct > width_pct then
62+
vim.cmd('split')
63+
else
64+
vim.cmd('vsplit')
65+
end
66+
67+
-- Restore buffer and cursor
68+
vim.api.nvim_set_current_buf(buf)
69+
vim.api.nvim_win_set_cursor(0, pos)
70+
end
71+
72+
vim.keymap.set('n', '<leader>w<space>', toggle_split, { noremap = true, silent = true })
73+
5274
-- blackhole single x
5375
vim.keymap.set('n', 'x', '"_x')
5476

0 commit comments

Comments
 (0)