0

How can I do in nvim if the file type is js do this:

vim.api.nvim_set_keymap('n', '<A-1>', [[:w | split term://node % <CR>]], { noremap = true, silent = true})

else if file type is lua do this:

vim.api.nvim_set_keymap("n", "<A-1>", [[:w | split term://lua %<CR>]], { noremap = true, silent = true })
2
  • Welcome to Vim!. Do you still have something open in your question? How can we help you further. Otherwise maybe could you accept one of the answers using the v button next to the arrow voting buttons. It allow the question to rest :-) Feb 20 at 12:35
  • Related 1, 2, 3
    – D. Ben Knoble
    Feb 21 at 16:54

1 Answer 1

1

The standard way to do this is to define buffer mapping in the filetype configuration.

I would have a ~/.config/nvim/after/ftplugin/js.lua file with the following content:

vim.api.nvim_set_keymap('n', '<A-1>', [[:w | split term://node % ]], { noremap = true, silent = true, buffer = true})

And a ~/.config/nvim/after/ftplugin/lua.lua file with the following content:

vim.api.nvim_set_keymap("n", "<A-1>", [[:w | split term://lua %<CR>]], { noremap = true, silent = true , buffer = true})

These files are executed when the filetype is set to their corresponding value. The buffer option makes the mapping only applying to the corresponding buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.