From 8965a3f3201cfc87155656b5dba9ddbdefbbe5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stian=20T=C3=B8sse?= Date: Sat, 6 Dec 2025 08:12:17 +0100 Subject: [PATCH] add funtion to comment out lines --- init.lua | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/init.lua b/init.lua index b819f724..23f26ead 100644 --- a/init.lua +++ b/init.lua @@ -48,8 +48,40 @@ vim.opt.scrolloff = 10 vim.opt.confirm = true -- NOTE: My personal key bining -vim.keymap.set('n', 'l', "yoconsole.log('pa', pa);", { desc = 'log selected value' }) vim.keymap.set('v', 'l', "yoconsole.log('pa', pa);", { desc = 'log selected value' }) +vim.keymap.set('n', 'e', ':e.', { desc = 'Open file three' }) + +-- Function to comment out line(s) +local function comment_line() + local count = vim.v.count + 1 + local start_line = vim.fn.line '.' -- Get current line number + + -- Check only the first line to determine action + local first_line = vim.fn.getline(start_line) + local is_commented = first_line:match '^%s*//' + + for i = 0, count - 1 do + local line_num = start_line + i + local line = vim.fn.getline(line_num) + local new_line + + if is_commented then + -- Uncomment: remove leading whitespace, //, and optional space after // + new_line = line:gsub('^%s*//%s?', '') + else + -- Comment: add // at the very beginning of the line + new_line = '// ' .. line + end + + vim.fn.setline(line_num, new_line) + end + + -- Move cursor down by count lines + vim.cmd('normal! ' .. count .. 'j') +end + +vim.keymap.set('n', 'c', comment_line, { desc = 'Toggle line comment(s)' }) + -- NOTE: My personal key bining END -- Clear highlights on search when pressing in normal mode