How do i make a keymap to compile and run files in init.lua on neovim?

2.3k views Asked by At

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.

2

There are 2 answers

0
lcheylus On

You must declare your CodeRunner function before creating autocommand :

local CodeRunner

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

CodeRunner = function()
  <Code of function>
0
Neville On

For cpp I use this:

vim.api.nvim_create_autocmd("FileType", { pattern = "cpp", 
    callback = function()
        vim.api.nvim_buf_set_keymap(0,"n","<C-c>",":split<CR>:te g++ -std=c++14 -Wshadow -Wall -o %:t:r % -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG && ./%:t:r<CR>i",opts)
    end})

or

vim.api.nvim_create_autocmd("FileType", { pattern = "cpp", 
    command = "nnoremap <buffer> <C-c> :split<CR>:te g++ -std=c++14 -Wshadow -Wall -o %:t:r % -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG && ./%:t:r<CR>i"})