Skip to content

Commit 8f88808

Browse files
author
Francis
committed
Added Moving Lines Up and Down in Vim without Using the Clipboard
https://www.youtube.com/watch?v=gNyNm5DsQ88
1 parent a15b0ce commit 8f88808

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

init.lua

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ vim.g.have_nerd_font = false
102102
vim.opt.number = true
103103
-- You can also add relative line numbers, to help with jumping.
104104
-- Experiment for yourself to see if you like it!
105-
-- vim.opt.relativenumber = true
105+
vim.opt.relativenumber = true
106106

107107
-- Enable mouse mode, can be useful for resizing splits for example!
108108
vim.opt.mouse = 'a'
@@ -180,10 +180,10 @@ vim.keymap.set('n', '<leader>q', vim.diagnostic.setloclist, { desc = 'Open diagn
180180
vim.keymap.set('t', '<Esc><Esc>', '<C-\\><C-n>', { desc = 'Exit terminal mode' })
181181

182182
-- TIP: Disable arrow keys in normal mode
183-
-- vim.keymap.set('n', '<left>', '<cmd>echo "Use h to move!!"<CR>')
184-
-- vim.keymap.set('n', '<right>', '<cmd>echo "Use l to move!!"<CR>')
185-
-- vim.keymap.set('n', '<up>', '<cmd>echo "Use k to move!!"<CR>')
186-
-- vim.keymap.set('n', '<down>', '<cmd>echo "Use j to move!!"<CR>')
183+
vim.keymap.set('n', '<left>', '<cmd>echo "Use h to move!!"<CR>')
184+
vim.keymap.set('n', '<right>', '<cmd>echo "Use l to move!!"<CR>')
185+
vim.keymap.set('n', '<up>', '<cmd>echo "Use k to move!!"<CR>')
186+
vim.keymap.set('n', '<down>', '<cmd>echo "Use j to move!!"<CR>')
187187

188188
-- Keybinds to make split navigation easier.
189189
-- Use CTRL+<hjkl> to switch between windows
@@ -200,6 +200,34 @@ vim.keymap.set('n', '<C-k>', '<C-w><C-k>', { desc = 'Move focus to the upper win
200200
-- vim.keymap.set("n", "<C-S-j>", "<C-w>J", { desc = "Move window to the lower" })
201201
-- vim.keymap.set("n", "<C-S-k>", "<C-w>K", { desc = "Move window to the upper" })
202202

203+
-- NOTE: Francis Custom block mover:
204+
-- Alt J / K in visual mode to more a block up/down
205+
-- Try to move lines up/down in normal and visual modes
206+
207+
-- Helper function to safely map keys
208+
local function map(mode, lhs, rhs, opts)
209+
local options = { noremap = true, silent = true }
210+
if opts then
211+
options = vim.tbl_extend('force', options, opts)
212+
end
213+
vim.keymap.set(mode, lhs, rhs, options)
214+
end
215+
216+
-- Helper function to safely map keys
217+
local function map(mode, lhs, rhs, opts)
218+
local options = { noremap = true, silent = true }
219+
if opts then
220+
options = vim.tbl_extend('force', options, opts)
221+
end
222+
vim.keymap.set(mode, lhs, rhs, options)
223+
end
224+
225+
-- Move lines with Cmd+j / Cmd+k
226+
map('n', '<D-j>', ':m .+1<CR>==', { desc = 'Move line down (Cmd)' })
227+
map('n', '<D-k>', ':m .-2<CR>==', { desc = 'Move line up (Cmd)' })
228+
map('v', '<D-j>', ":m '>+1<CR>gv=gv", { desc = 'Move selection down (Cmd)' })
229+
map('v', '<D-k>', ":m '<-2<CR>gv=gv", { desc = 'Move selection up (Cmd)' })
230+
203231
-- [[ Basic Autocommands ]]
204232
-- See `:help lua-guide-autocommands`
205233

@@ -886,7 +914,8 @@ require('lazy').setup({
886914
-- Load the colorscheme here.
887915
-- Like many other themes, this one has different styles, and you could load
888916
-- any other, such as 'tokyonight-storm', 'tokyonight-moon', or 'tokyonight-day'.
889-
vim.cmd.colorscheme 'tokyonight-night'
917+
-- vim.cmd.colorscheme 'tokyonight-night'
918+
vim.cmd.colorscheme 'default'
890919
end,
891920
},
892921

0 commit comments

Comments
 (0)