From c2631e3af986a2d65b88bf43c0b44d4e14d44185 Mon Sep 17 00:00:00 2001 From: Timur <74539640+SHTRAMPANTUNC@users.noreply.github.com> Date: Thu, 9 May 2024 17:07:31 +0300 Subject: [PATCH] Added binds for moving lines of code I really like the feature from Vs Code that allows you to use the keys on your keyboard to move pieces of code relative to the rest of the code around, I think it's a handy feature that will help well when editing code --- lua/nvchad/mappings.lua | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lua/nvchad/mappings.lua b/lua/nvchad/mappings.lua index 8730906..a944465 100644 --- a/lua/nvchad/mappings.lua +++ b/lua/nvchad/mappings.lua @@ -51,6 +51,38 @@ map("n", "/", function() require("Comment.api").toggle.linewise.current() end, { desc = "comment toggle" }) +--Move line +local function move_line(op) + return function() + local start = op == "+" and 1 or 2 + local count = vim.v.count + local times = count == 0 and start or (op == "+" and count or count + 1) + local ok, _ = pcall(vim.cmd.move, op .. times) + if ok then + vim.cmd.norm("==") + end + end +end + +local function move_selected(op) + return function() + local restore_autocmd = u.disable_autocmd("toggle_relnum") + + local start = op == "+" and "" or 2 + local count = vim.v.count + local times = count == 0 and start or (op == "+" and count or count + 1) + local mark = op == "+" and "'>" or "'<" + vim.api.nvim_feedkeys(vim.keycode(":move" .. mark .. op .. times .. "gv=gv"), "n", true) + + restore_autocmd() + end +end + +map("n", "", move_line("+"), { silent = true, desc = "Move line to Top" }) +map("n", "", move_line("-"), { silent = true, desc = "Move line to Down" }) +map("x", "", move_selected("+"), { silent = true, desc = "Move Group of line to Top" }) +map("x", "", move_selected("-"), { silent = true, desc = "Move Group of line to Down" }) + map( "v", "/",