From 2533d7dc2ee03f644ad486f5af856553a467cfe2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Lukas=20H=C3=A4gele?= Date: Sun, 29 Mar 2026 12:12:30 +0200 Subject: [PATCH] collect vsplit code in its plugin file --- lua/config/autocommands.lua | 40 ------------------------------------- lua/config/keymap.lua | 4 ---- lua/snippets/vsplit.lua | 39 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 44 deletions(-) diff --git a/lua/config/autocommands.lua b/lua/config/autocommands.lua index 2aa54c6..cb19852 100644 --- a/lua/config/autocommands.lua +++ b/lua/config/autocommands.lua @@ -1,43 +1,3 @@ -local vsplit = require("snippets.vsplit") - --- startup init -vim.api.nvim_create_autocmd( "VimEnter", { - desc = "run explore & create vsplit", - callback = function() - if vim.bo.filetype ~= "gitcommit" then - -- start netrw - vim.cmd.Explore() - -- split window on startup - vsplit.split() - end - end -}) - --- force new window to the left pane -vim.api.nvim_create_autocmd( "FileType", { - desc = "force new window to the left pane", - group = vim.api.nvim_create_augroup('enforce-single-vsplit', { clear = true }), - pattern = { "man", "help", "qf" }, - callback = function() - vim.schedule(function() - local new_win = vim.api.nvim_get_current_win() - local new_buf = vim.api.nvim_get_current_buf() - if not ((new_win == vsplit.left_id) or (new_win == vsplit.right_id)) then - -- logging - vim.notify("new_win:" ..new_win .." new_buf:" ..new_buf .." vsplit.left_id:" ..vsplit.left_id) - -- move new buffer to left pane - vim.api.nvim_win_set_buf(vsplit.left_id, new_buf) - -- close new window - vim.api.nvim_win_close(new_win, false) - -- switch focus - if vim.bo.filetype == "qf" then - vim.api.nvim_set_current_win(vsplit.right_id) - end - end - end) - end -}) - -- remove trailing whitespace on save vim.api.nvim_create_autocmd({ "BufWritePre" }, { pattern = { "*" }, diff --git a/lua/config/keymap.lua b/lua/config/keymap.lua index ab8a00c..8aaa3d6 100644 --- a/lua/config/keymap.lua +++ b/lua/config/keymap.lua @@ -5,10 +5,6 @@ vim.keymap.set('i', 'jj', '') vim.keymap.set("n", "#", "Vgc", { desc = "select line and toggle comment", remap = true }) vim.keymap.set("v", "#", "gc", { desc = "toggle comment of selected line", remap = true }) --- window management -local vsplit = require("snippets.vsplit") -vim.keymap.set("n", "s", vsplit.split, { desc = "split vertically and move to new window" }) - -- move highlighted lines vim.keymap.set("v", "J", ":m '>+1gv=gv") vim.keymap.set("v", "K", ":m '<-2gv=gv") diff --git a/lua/snippets/vsplit.lua b/lua/snippets/vsplit.lua index 8b33ef4..50dcde4 100644 --- a/lua/snippets/vsplit.lua +++ b/lua/snippets/vsplit.lua @@ -16,4 +16,43 @@ M.split = function() M.right_id = vim.api.nvim_open_win(0, true, win_config) end +-- startup init +vim.api.nvim_create_autocmd( "VimEnter", { + desc = "run explore & create vsplit", + callback = function() + if vim.bo.filetype ~= "gitcommit" then + -- start netrw + vim.cmd.Explore() + -- split window on startup + M.split() + end + end +}) + +-- force new window to the left pane +vim.api.nvim_create_autocmd( "FileType", { + desc = "force new window to the left pane", + group = vim.api.nvim_create_augroup('enforce-single-vsplit', { clear = true }), + pattern = { "man", "help", "qf" }, + callback = function() + vim.schedule(function() + local new_win = vim.api.nvim_get_current_win() + local new_buf = vim.api.nvim_get_current_buf() + if not ((new_win == M.left_id) or (new_win == M.right_id)) then + -- move new buffer to left pane + vim.api.nvim_win_set_buf(M.left_id, new_buf) + -- close new window + vim.api.nvim_win_close(new_win, false) + -- switch focus + if vim.bo.filetype == "qf" then + vim.api.nvim_set_current_win(M.right_id) + end + end + end) + end +}) + +-- keymaps +vim.keymap.set("n", "s", M.split, { desc = "split vertically and move to new window" }) + return M -- 2.39.5