aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-cmdline.lua
blob: c37442bfaf21f3bcba1938622dc025ca040041fd (plain)
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
31
32
33
34
35
36
37
--[[ 50-cmdline.lua — command-line mode helpers.

Readline-style kill-line and a non-incremental history search that stays usable while the
completion menu is open.

Keymaps:
  `<C-k>`   kill from the cursor to the end of the command line
  `<C-p>`   previous history entry
  `<C-n>`   next history entry
]]

----------------------------------------------------------------------------------------------------
-- Local functions ---------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

-- Kill from the cursor to the end of the command line, akin to Readline's kill-line
local function kill_line()
	local pos = vim.fn.getcmdpos() -- 1-based byte index of the cursor
	vim.fn.setcmdline(vim.fn.getcmdline():sub(1, pos - 1), pos)
end

-- Non incremental command-line history search that works while the completion menu is active
local function history_prev()
	return vim.fn.pumvisible() == 1 and "<C-e><Up>" or "<Up>"
end

local function history_next()
	return vim.fn.pumvisible() == 1 and "<C-e><Down>" or "<Down>"
end

----------------------------------------------------------------------------------------------------
-- Keymaps -----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

vim.keymap.set("c", "<C-k>", kill_line, { desc = "Kill from cursor to end of line" })
vim.keymap.set("c", "<C-p>", history_prev, { expr = true, desc = "Previous history entry" })
vim.keymap.set("c", "<C-n>", history_next, { expr = true, desc = "Next history entry" })