add funtion to comment out lines

This commit is contained in:
Stian Tøsse 2025-12-06 08:12:17 +01:00
parent 3f5a91e792
commit 8965a3f320
1 changed files with 33 additions and 1 deletions

View File

@ -48,8 +48,40 @@ vim.opt.scrolloff = 10
vim.opt.confirm = true vim.opt.confirm = true
-- NOTE: My personal key bining -- NOTE: My personal key bining
vim.keymap.set('n', '<leader>l', "yoconsole.log('<Esc>pa', <Esc>pa);<Esc>", { desc = 'log selected value' })
vim.keymap.set('v', '<leader>l', "yoconsole.log('<Esc>pa', <Esc>pa);<Esc>", { desc = 'log selected value' }) vim.keymap.set('v', '<leader>l', "yoconsole.log('<Esc>pa', <Esc>pa);<Esc>", { desc = 'log selected value' })
vim.keymap.set('n', '<leader>e', ':e.<CR>', { 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', '<leader>c', comment_line, { desc = 'Toggle line comment(s)' })
-- NOTE: My personal key bining END -- NOTE: My personal key bining END
-- Clear highlights on search when pressing <Esc> in normal mode -- Clear highlights on search when pressing <Esc> in normal mode