fireq1
fireq1

Reputation: 21

Substitution command in neovim remaps not working

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.

Upvotes: 1

Views: 537

Answers (1)

fireq1
fireq1

Reputation: 21

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

Upvotes: 1

Related Questions