英文:
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('n', '<leader>pu', vim.cmd('%s/\r//g'))
Errors with "Pattern not found:"
using square brackets errors with "Pattern not found:\r"
vim.keymap.set('n', '<leader>pu', vim.cmd.substitute{ range = '%', args = {'\r',''} })
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('n', '<leader>pu', vim.cmd('%s/r//g'))
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('n', '<leader>pu', ':%s/\\r//<CR>', {})
credit here:
https://vi.stackexchange.com/questions/42622/substitution-command-in-neovim-remaps-not-working
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论