neovim 中的替换命令重新映射不起作用。

huangapple go评论66阅读模式
英文:

Substitution command in neovim remaps not working

问题

我只想将 <leader>pu 映射为在文件中删除所有换行符(表示为 ^M)的操作。

我可以执行以下命令:

:%s/\r//g

这个命令有效。

然而,在我的 remap.lua 文件中尝试使用 vim.keymap.set 时会出现错误。

尝试的方法包括:

vim.keymap.set('n', '<leader>pu', vim.cmd('%s/\r//g'))

报错信息是 "Pattern not found:"

使用方括号会报错 "Pattern not found:\r"

vim.keymap.set('n', '<leader>pu', vim.cmd.substitute{ range = '%', args = {'\r',''} })

报错信息是 "Invalid command arg: expected non-whitespace"

使用不同的字符,如 'r',可以源代码,但是将命令应用于源代码文件而不是映射它。

我有其他使用 vim.cmd 的映射,它们在 remap 文件中运行正常。

也许这是一个技能问题,但我需要一些帮助。

英文:

I simply want to map < leader> pu to erase all occurrences of the carriage return character
(represented as ^M) in a file.

I can execute the command:

:%s/\r//g

and it works.

Any attempts to use vim.keymap.set in my remap.lua file, however, error out when I try to source it.

Attempts include:

vim.keymap.set(&#39;n&#39;, &#39;&lt;leader&gt;pu&#39;, vim.cmd(&#39;%s/\r//g&#39;))

Errors with "Pattern not found:"
using square brackets errors with "Pattern not found:\r"

vim.keymap.set(&#39;n&#39;, &#39;&lt;leader&gt;pu&#39;, vim.cmd.substitute{ range = &#39;%&#39;, args = {&#39;\r&#39;,&#39;&#39;} })

Errors with " Invalid command arg: expected non-whitespace"

An attempt with a different character, 'r', sources, but applies the command inside the sourced file instead of mapping it.

vim.keymap.set(&#39;n&#39;, &#39;&lt;leader&gt;pu&#39;, vim.cmd(&#39;%s/r//g&#39;))

I have other mappings using vim.cmd that work fine, and do not apply in the remap file.

Maybe this is a skill issue, but I need some assistance.

答案1

得分: 1

vim.api.nvim_set_keymap('n', '<leader>pu', ':%s/\\r//<CR>', {})
英文:
vim.api.nvim_set_keymap(&#39;n&#39;, &#39;&lt;leader&gt;pu&#39;, &#39;:%s/\\r//&lt;CR&gt;&#39;, {})

credit here:
https://vi.stackexchange.com/questions/42622/substitution-command-in-neovim-remaps-not-working

huangapple
  • 本文由 发表于 2023年7月11日 08:15:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658015.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定