Hmm looks like I am also getting this when I try it out, seems like it doesn’t like how the string is injected into the function. I tried it by binding to a command and got the same error as you did, so all I did was wrap the line where I was defining the function with the double square brackets.
However, there is one more error you might end up, which I got as a
Trailing Character error, turns out you can’t just add an expression after calling commands, it’s best to
execute the command you want after calling
keeppatterns. Here is what I ended up doing:
cmd = string.format('keepjumps keeppatterns execute %q', cmd)
local original_cursor = vim.fn.winsaveview()
vim.cmd([[command! Preserve lua preserve('%s/\\s\\+$//ge')]])
Only line I modified is the first line of the function, I’ve added
execute %q, the
%q in this case is used by
string.format() which just adds quotes to the variable. So in that case the full
cmd string should be:
keepjumps keeppaterns execute "%s/\s\+$//ge"
Basically, you want to execute the last function, this can take any vim Ex command so it should be fine but idk your use case for this besides trying to execute the expression of