Vlad the Impala
Vlad the Impala

Reputation: 15872

call vim function with current word

suppose I have a line like this: hello foo bar

and my cursor is on foo. I want to map a key that will call a function with foo without me having to first select it visually. How would I do this in vim?

Upvotes: 4

Views: 1219

Answers (3)

Jason
Jason

Reputation: 3917

Another solution...

nnoremap <leader>f yw:call fun(@")<cr>

Better solution...

nnoremap <leader>f :call fun(<c-r><c-w>)<cr>

Upvotes: 1

Kent
Kent

Reputation: 195059

you are looking for function expand(expression)

here your expression would be '<cword>'

If you love to put it as function argument, you could :yourFunction(expand('<cword>'))

:h expand( for more details.

Upvotes: 4

bgoldst
bgoldst

Reputation: 35314

Perhaps something like this:

nnoremap \print :echo printf('word under cursor: %s',expand('<cword>'))<CR>

Upvotes: 3

Related Questions