Skip to content

Escape pasted text #257

@carbon-steel

Description

@carbon-steel

I tried adding the following code to my after/ftplugin/spectre_panel.lua to escape special characters when I paste:

vim.keymap.set("n", "p", function()
	local text = vim.fn.getreg() -- uses v:register by default
	text = string.gsub(text, [[%\]], [[\\]])
	text = string.gsub(text, "%.", [[\.]])
	text = string.gsub(text, "%(", [[\(]])
	text = string.gsub(text, "%)", [[\)]])
	text = string.gsub(text, "%[", [[\[]])
	text = string.gsub(text, "%]", [[\[]])
	text = string.gsub(text, [[%|]], [[\|]])
	vim.api.nvim_paste(text, true, -1)
end)

The idea is that if I yank "foo.py" and paste it into the spectre panel, it should paste "foo.py" so that it looks for the exact string instead of the regex.

However, the keymap conflicted with

n  p           *@p<Cmd>lua require('spectre').on_search_change()<CR> 

so it didn't work.

Would it be possible to integrate escaping special characters into the plugin itself? Or is there another way to achieve what I'm trying to do?

Metadata

Metadata

Assignees

No one assigned

    Labels

    wontfixThis will not be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions