Converting conditional vimscript mapping into lua (neovim) (submit <CR> in lua)

Viewed 133

I'm trying to convert the following conditional mapping into lua code

:nnoremap <expr> <CR> ((&buftype is# "quickfix") ? "<CR>" : ":\:nohlsearch<cr>\n")

This mappigng maps Enter to a :nohlsearch command, but only if we are not in the quickfix buffer. There, you need the Enter for selecting lines.

The lua code that I came with is:

vim.keymap.set("n", "<CR>", function()
if vim.bo.buftype == "quickfix" then
  print("<CR>")
  vim.api.nvim_input("<CR>")
else
  print("nohlsearch")
  vim.cmd(":nohlsearch")
end
end, {desc = "call :nohlsearch to disable highlighting (but only if in file editing)"} )

The branching, the vim.cmd, and the print() parts work as expected, but the vim.api.nvim_input("<CR>") part doesn't work (in quickfix I can't use Enter to select a line). It's not equivalent to the <CR> in the vimscript mapping.

Also, there has to be a more idiomatic way to write this mapping.

I started this conversion because I need to make the condition more complex.

1 Answers

You need to check the doc of vim.keymap.set more thoroughly (see :h vim.keymap.set). You are doing it wrong and missing some parameters. Here is what is working (tested in nvim 0.7.2):

vim.keymap.set('n', '<CR>', function()
  if vim.o.buftype == 'quickfix' then
    return "<CR>"
  else
    return ":nohlsearch<CR>"
  end
end, {expr = true, replace_keycodes = true})

You can just return those key maps in the lua code, but you are missing the {expr = true, replace_keycodes = true} part.

If you haven't check nvim lua guide, read it through and make sure you understand all of it. You will understand nvim better than most users.

Related