Some Cool Vim Shortcuts

It’s sometimes more fun to find one or two slightly useful shortcuts and spam them any chance you can as opposed to reading a cheatsheet. Also, writing it here makes it easier for me to remember to use them again. Here are a couple of nice movements that I’ve found:

Not Really Shortcuts But Also Quite Useful

Lua code for the C-[hjkl] mapping:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
--- if using Lua 5.2 or above, unpack is deprecated, but also table.unpack doesn't seem to work correctly; my solution to this is to alias unpack
unpack = table.unpack or unpack

-- wrapper: vim.keymap.set RHS needs to be a function, but need to pass in args, so wrap() exists
local wrap = function(func, ...)
	local args = { ... }
	return function()
		func(unpack(args))
	end
end

-- function: move the window in direction shown or create new split (adapted to lua from: https://pastebin.com/ya9hWSbY)
local win_move = function(key)
	local cur_win = vim.fn.winnr()
	vim.cmd.wincmd(key)
	if cur_win == vim.fn.winnr() then
		if key == "j" or key == "k" then
			vim.cmd.wincmd("s")
		else
			vim.cmd.wincmd("v")
		end
		vim.cmd.wincmd(key)
	end
end

-- set the keymaps
vim.keymap.set("n", "<C-h>", wrap(win_move, "h"), { noremap = true, silent = true })
vim.keymap.set("n", "<C-j>", wrap(win_move, "j"), { noremap = true, silent = true })
vim.keymap.set("n", "<C-k>", wrap(win_move, "k"), { noremap = true, silent = true })
vim.keymap.set("n", "<C-l>", wrap(win_move, "l"), { noremap = true, silent = true })