Keymapping to search string pattern in LSP workspace

Hi,
the goal is to have a handy search feature similar to VSCode, without need to manual specify current workspace.

Current approach:

:lua vim.keymap.set('n', '<leader>f', 
  ":vimgrep //j " .. vim.lsp.buf.list_workspace_folders()[1] .. '/**')

Result: :vimgrep //j path/to/root/**.

Having cursor position auto-placed at the missing pattern lead me to

:lua vim.keymap.set('n', '<leader>f', 
  ":vimgrep //j " .. vim.lsp.buf.list_workspace_folders()[1] .. '/** 
  <c-b><right><right><right><right><right><right><right><right><right>')

Result: :vimgrep /<CURSOR_POS>/j path/to/root/**.

Next stop, I tried to persist said keybinding in init.lua as is. But realized that workspace expression is evaluated eagerly and cannot work.

Where I am struggling now:

vim.keymap.set('n','<leader>f', function() return ":vimgrep ..." end)

This just returns the string. These alternatives aren’t suited either:

  • print() echoes expression non-interactively
  • vim.cmd() executes string immediately

1.) Instead I’d like to have a “prefilled” ex-command template in command line as shown above (command being not directly executed). How might I do this?
2.) Is there a way to adjust cursor position more elegantly than <c-b><right>...?
3.) I thought to exclude directories from .gitignore by using git ls-files instead of vim.lsp.buf.list_workspace_folders()[1]. How could I pass the path results of this external command to vimgrep?

Thanks for help !

Following works for me:

vim.keymap.set('n','<leader>f', function()
  local pattern = vim.fn.input("Search pattern: ")
  local cmd = [[vimgrep /]].. pattern .. [[/j `git ls-files --full-name :/ \| sed "s;^;$(git rev-parse --show-toplevel)/;"` | copen]]
  return vim.cmd(cmd)
end,opts)

EDIT: Ensured absolute path file patterns, so that vimgrep has no issues when cwd is somewhere inside the project.
EDIT 2: The only requirement for this method is an initialized Git repository. It doesn’t need Language Server Protocol features. .gitignore will be respected.