How can I get <cword> in command mode using lua

nnoremap <expr> <Leader>s* ':<C-u>%s/\<' . expand('<cword>') . '\>//g<Left><Left>'

How this keymap is converted to lua

I have solved it myself:

keymap("n", "<Leader>sg", [[':<C-u>%s/\<' . expand('<cword>') . '\>//g<Left><Left>']], { noremap = true, silent = false, expr = true })

I’m glad you found a solution! The solution you used keeps the relevant logic in vimscript. You can alternatively get <cword> in a lua expression with vim.fn.expand("<cword>").

You can call any vimscript function, including expand, using the vim.fn prefix.

1 Like