0

I'm trying to convert my init.vim over to lua.

Also trying out "packer" plugin manager and using styua-nvim plugin to format lua code.

Followed instructions for packer and got it installed and loaded stylua-nvim plugin with pakcer. Loaded stylua with :PackerInstall

However, getting error: attempt to call global 'buf_set_keymap' (a nil value)

init.lua:

vim.cmd([[
source ~/.config/nvim/local_customizations.vim
source ~/.config/nvim/plugins.vim
]])
require('plugins')
HOME = os.getenv("HOME")

<snip>

local opts = { noremap=true, silent=true }
buf_set_keymap("n", "<leader>f", "<cmd>lua require('stylua-nvim').format_file()<CR>", opts)

Also tried:

buf_set_keymap("n", "<leader>f", "require('stylua-nvim').format_file()<CR>", opts)

But get same error.

Original instruction for stylua-nvim give this for config of keymap:

buf_set_keymap("n", "<leader>f", "<cmd>lua require("stylua-nvim").format_file()<CR>', opts)

But this looks to be wrong to me because the quotes are unmatched and throws an error:

Error while creating lua chunk: /Users/stevedondley/.config/nvim/init.lua:29: ')' expected near 'stylua'

Frustrating.

StevieD
  • 6,925
  • 2
  • 25
  • 45
  • You have to prefix with `vim.api` to access the method, it is not a global method. Or you import that method locally and use it without `vim.api` prefix, something like `local xxx = vim.api.xxx`. Have you checked [nvim lua guide](https://github.com/nanotee/nvim-lua-guide#the-vim-namespace)? – jdhao Feb 23 '22 at 02:55

1 Answers1

0

OK, had to do:

vim.api.nvim_buf_set_keymap(bufnr, "n", "<leader>f", '<cmd>lua require("stylua-nvim").format_file()<CR>', opts)

No idea why, but it works.

StevieD
  • 6,925
  • 2
  • 25
  • 45