gpt4 book ai didi

How to use vim.keymap.set with vim.v.count in Neovim(如何在Neovim中使用vim.keymap.set和vim.v.count)

转载 作者:bug小助手 更新时间:2023-10-28 22:29:27 34 4
gpt4 key购买 nike



I'm trying to add a count to one of my key-map bindings. However, the following does not work as expected:

我正在尝试向我的一个键映射绑定添加计数。然而,以下情况并未如预期那样工作:


vim.keymap.set("n", "<leader>o", vim.v.count .. 'o<Esc>')

If I use the key combination <leader>5o only one new line is added and the editor is in insert mode. Only after I hit the <Esc> key myself 4 extra lines appear and the editor switches to normal mode.

如果我使用组合键 50,则只会添加一行,并且编辑器处于插入模式。只有在我自己按了 键之后,才会出现额外的4行,并且编辑器会切换到正常模式。


How can one correctly use vim.v.count with vim.keymap.set?

如何正确使用vim.v.count和vim.keymap.set?


If I use vim.keymap.set("n", "<leader>o", 5 .. 'o<Esc>') it works as expected. Five new lines are added and the editor stays in normal mode.

如果使用vim.keymap.set(“n”,“ o”,5.‘o ’)它按预期工作。添加了五个新行,并且编辑器保持在正常模式。


I've also tried to wrap the commands into a function vim.keymap.set("n", "<leader>o", function() ... end), but it didn't change the behavior.

我还尝试将命令包装到一个函数vim.keymap.set(“n”,“ o”,Function()...结束),但这并没有改变行为。


更多回答

vi.stackexchange.com

Vi.stackexchange.com

What is the intended use case? Are you simply looking to insert n blank lines, or do you want to insert n lines with the same content? Lastly, what line do you want to end up on after the insert is complete, the initial line you enter the key mapping on? Or, the final inserted line?

目标使用案例是什么?您是要简单地插入n个空行,还是要插入具有相同内容的n行?最后,在插入完成后,您希望在哪一行结束,在第一行上输入键映射?或者,最后插入的行?

I want to insert n blank lines. I'm not that concerned about where the cursor ends up after the insert.

我想插入n个空行。我不太关心光标在INSERT之后的位置。

优秀答案推荐

You need to use a map-expression (see h: map-expression):

您需要使用映射表达式(请参阅h:map-表达式):


-- Insert 'n' lines below current line staying in normal mode (e.g. use 5<leader>o)
vim.keymap.set("n", "<leader>o", function()
return "m`" .. vim.v.count .. "o<Esc>``"
end, { expr = true })

-- Insert 'n' lines above current line staying in normal mode (e.g. use 5<leader>O)
vim.keymap.set("n", "<leader>O", function()
return "m`" .. vim.v.count .. "O<Esc>``"
end, { expr = true })

-- oo and OO are another good LHS mapping,
-- they roll off the fingers a bit better than <leader>o and <leader>O I think

This forces the right-hand side of the mapping to evaluate first which correctly captures the input count. Now you can type some count, followed by <leader>o or <leader>O, and it will insert the input number of lines either below or above the current line, respectively.

这会强制映射的右侧首先计算正确捕获输入计数的值。现在,您可以输入一些计数,后跟 o或 O,它将分别在当前行的下方或上方插入输入的行数。


Note: a mark has to be set with "m`" prior to the rest of the command otherwise strange behaviour occurs. I also added "``" to jump back to this mark so the cursor doesn't move when adding new blank lines. If you want it to jump to the last of the inserted blank lines, simply remove the "``".

注意:标记必须在命令的其余部分之前设置为“m`”,否则会出现奇怪的行为。我还添加了“``”以跳回此标记,以便在添加新的空行时光标不会移动。如果希望跳到插入的空行的最后一行,只需删除“``”即可。


更多回答

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com