From 8f8880851ea7c829622303600448115e5c5a600c Mon Sep 17 00:00:00 2001 From: Francis Date: Sun, 27 Apr 2025 13:06:23 +0100 Subject: [PATCH] Added Moving Lines Up and Down in Vim without Using the Clipboard https://www.youtube.com/watch?v=gNyNm5DsQ88 --- init.lua | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/init.lua b/init.lua index 776c6873..876ae985 100644 --- a/init.lua +++ b/init.lua @@ -102,7 +102,7 @@ vim.g.have_nerd_font = false vim.opt.number = true -- You can also add relative line numbers, to help with jumping. -- Experiment for yourself to see if you like it! --- vim.opt.relativenumber = true +vim.opt.relativenumber = true -- Enable mouse mode, can be useful for resizing splits for example! vim.opt.mouse = 'a' @@ -180,10 +180,10 @@ vim.keymap.set('n', 'q', vim.diagnostic.setloclist, { desc = 'Open diagn vim.keymap.set('t', '', '', { desc = 'Exit terminal mode' }) -- TIP: Disable arrow keys in normal mode --- vim.keymap.set('n', '', 'echo "Use h to move!!"') --- vim.keymap.set('n', '', 'echo "Use l to move!!"') --- vim.keymap.set('n', '', 'echo "Use k to move!!"') --- vim.keymap.set('n', '', 'echo "Use j to move!!"') +vim.keymap.set('n', '', 'echo "Use h to move!!"') +vim.keymap.set('n', '', 'echo "Use l to move!!"') +vim.keymap.set('n', '', 'echo "Use k to move!!"') +vim.keymap.set('n', '', 'echo "Use j to move!!"') -- Keybinds to make split navigation easier. -- Use CTRL+ to switch between windows @@ -200,6 +200,34 @@ vim.keymap.set('n', '', '', { desc = 'Move focus to the upper win -- vim.keymap.set("n", "", "J", { desc = "Move window to the lower" }) -- vim.keymap.set("n", "", "K", { desc = "Move window to the upper" }) +-- NOTE: Francis Custom block mover: +-- Alt J / K in visual mode to more a block up/down +-- Try to move lines up/down in normal and visual modes + +-- Helper function to safely map keys +local function map(mode, lhs, rhs, opts) + local options = { noremap = true, silent = true } + if opts then + options = vim.tbl_extend('force', options, opts) + end + vim.keymap.set(mode, lhs, rhs, options) +end + +-- Helper function to safely map keys +local function map(mode, lhs, rhs, opts) + local options = { noremap = true, silent = true } + if opts then + options = vim.tbl_extend('force', options, opts) + end + vim.keymap.set(mode, lhs, rhs, options) +end + +-- Move lines with Cmd+j / Cmd+k +map('n', '', ':m .+1==', { desc = 'Move line down (Cmd)' }) +map('n', '', ':m .-2==', { desc = 'Move line up (Cmd)' }) +map('v', '', ":m '>+1gv=gv", { desc = 'Move selection down (Cmd)' }) +map('v', '', ":m '<-2gv=gv", { desc = 'Move selection up (Cmd)' }) + -- [[ Basic Autocommands ]] -- See `:help lua-guide-autocommands` @@ -886,7 +914,8 @@ require('lazy').setup({ -- Load the colorscheme here. -- Like many other themes, this one has different styles, and you could load -- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'. - vim.cmd.colorscheme 'tokyonight-night' + -- vim.cmd.colorscheme 'tokyonight-night' + vim.cmd.colorscheme 'default' end, },