aboutsummaryrefslogtreecommitdiffstats
path: root/plugin/50-cmdline.lua
blob: 374bfe58515f72f7894c8a8c80027016aadaa204 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
--[[ 50-cmdline.lua — command-line appearance and mode helpers.

Hides the command line until it is used, and adds Readline-style kill-line plus 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
]]

----------------------------------------------------------------------------------------------------
-- Options -----------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------

vim.opt.showmode = false

----------------------------------------------------------------------------------------------------
-- 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" })

local help = require("help")
help.register("cmdline", {
	maps = {
		{ "<C-k>", "Kill from cursor to end of line", "c" },
		{ "<C-p>", "Previous history entry", "c" },
		{ "<C-n>", "Next history entry", "c" },
	},
})