0

I want to know how to call a function after a normal or Ex command.

Examples:

  • bind a function to search command. When I input /xxxx to search xxxx, also call the function.
  • press * to highlight occurrences, then also execute a function.
1
1

In case of * (or similar normal mode commands) you just remap it:


func! MyFunc()
    echom "Hello World"
endfunc

nnoremap * *:call MyFunc()<CR>

For the search /xxxx you can redefine <CR> mapping in commandline:

func! SearchCR()
    if getcmdtype() =~ "[/?]"
        echom "Run Something if CR was pressed in /search"
    endif
endfunc

cnoremap <CR> <cmd>call SearchCR()<CR><CR>

This would work only in recent vim (with support of a new <cmd>)

PS, there are no "hooks" in vim to attach to selected normal mode commands, although there are autocommands that could be used to run your code on some events.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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