I tried making a keymap for nvim in the init.lua file, I tried making a remap that runs a function but still gives me
E15: Invalid expression: <80>hexec CodeRunner()^M.
the remap works but doesn't run my function as I wanted. I need help writing it.
if vim.fn.has "nvim-0.7" then
vim.api.nvim_create_autocmd("FileType", {
pattern = "*",
callback = function()
vim.schedule(CodeRunner)
end,
})
else
vim.cmd "autocmd FileType * lua CodeRunner()"
end
function CodeRunner()
local bufnr = vim.api.nvim_get_current_buf()
local ft = vim.api.nvim_buf_get_option(bufnr, "filetype")
local fname = vim.fn.expand "%:p:t"
local keymap_c = {}
if ft == "python" then
keymap_c = {
name = "Code",
r = { "<cmd>update<CR><cmd>exec '!python3' shellescape(@%, 1)<cr>", "Run" },
m = { "<cmd>TermExec cmd='nodemon -e py %'<cr>", "Monitor" },
}
elseif ft == "lua" then
keymap_c = {
name = "Code",
r = { "<cmd>luafile %<cr>", "Run" },
}
elseif ft == "rust" then
keymap_c = {
name = "Code",
r = { "<cmd>Cargo run<cr>", "Run" },
D = { "<cmd>RustDebuggables<cr>", "Debuggables" },
h = { "<cmd>RustHoverActions<cr>", "Hover Actions" },
R = { "<cmd>RustRunnables<cr>", "Runnables" },
}
elseif ft == "go" then
keymap_c = {
name = "Code",
r = { "<cmd>GoRun<cr>", "Run" },
}
elseif ft == "typescript" or ft == "typescriptreact" or ft == "javascript" or ft == "javascriptreact" then
keymap_c = {
name = "Code",
o = { "<cmd>TSLspOrganize<cr>", "Organize" },
r = { "<cmd>TSLspRenameFile<cr>", "Rename File" },
i = { "<cmd>TSLspImportAll<cr>", "Import All" },
R = { "<cmd>lua require('config.test').javascript_runner()<cr>", "Choose Test Runner" },
s = { "<cmd>2TermExec cmd='yarn start'<cr>", "Yarn Start" },
t = { "<cmd>2TermExec cmd='yarn test'<cr>", "Yarn Test" },
}
end
if fname == "package.json" then
keymap_c.v = { "<cmd>lua require('package-info').show()<cr>", "Show Version" }
keymap_c.c = { "<cmd>lua require('package-info').change_version()<cr>", "Change Version" }
keymap_c.s = { "<cmd>2TermExec cmd='yarn start'<cr>", "Yarn Start" }
keymap_c.t = { "<cmd>2TermExec cmd='yarn test'<cr>", "Yarn Test" }
end
if next(keymap_c) ~= nil then
whichkey.register(
{ c = keymap_c },
{ mode = "n", silent = true, noremap = true, buffer = bufnr, prefix = "<leader>" }
)
end
end
end
I think I should use a reference to file not sure if that's the solution, I am a beginner at neovim.
You must declare your
CodeRunner
function before creating autocommand :