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 searchxxxx
, also call the function. - press
*
to highlight occurrences, then also execute a function.
I want to know how to call a function after a normal or Ex command.
Examples:
/xxxx
to search xxxx
, also call the function.*
to highlight occurrences, then also execute a function.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.