I want to remap the m key for executing a function after a mark is set. I tried with this function but it doesn't work. The mark function is not running correctly, and hello world is never printed. How can I map this correctly? I am not sure how can I wait for m to complete its action before running other action.

vim.api.nvim_set_keymap("n", "m", "m<cmd>echo 'hello world'<cr>", {noremap = true})

1 Answer 1


You probably want to use :h getchar() to let the user input the name of the mark they want to set, otherwise your mapped m command will be executed without a mark name and that doesn't work.

Here is an example in vimscript using a function: You remap m to call the MarkAndDo function. This function reads the input of the user with getchar() and uses execute to run the command m + mark name then you can do whatever you want in the function.

function! MarkAndDo()
    execute "normal! m" . nr2char(getchar())
    echo "hello world"

nnoremap <silent> m :call MarkAndDo()<CR>

Adapting this solution to the nvim_set_keymap() api is straight forwards:

vim.api.nvim_set_keymap("n", "m", ":call MarkAndDo()<CR>", {noremap= true})

If you want to keep it a one liner that should be possible too but I think that it's better to use a function to keep your code readable.

Your Answer

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

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