views:

34

answers:

3

is it possible to send the word under the cursor to a perl script by typing a shortcut?

how do i do that?

+1  A: 

Hello! You can do the following:

nnoremap <f5> :!perl my_script.pl <c-r><c-w><enter>

In your vimrc, this lines maps the F5 key to this combination of characters. CTRL-R CTRL-W inserts current word in a command line.

Benoit
-1 for not escaping the argument.
ZyX
+1  A: 

Given a perl script ${HOME}/test.pl, you could use:

:nnoremap <F2> :!${HOME}/test.pl <C-R><C-W><CR>

Then pressing F2 would send the word under the cursor to your script.

As per my answer to your previous question, CTRL-R CTRL-W represents the word under the cursor.

See the following help topics in Vim to get you started with writing keyboard shortcuts:

My test script:

#!/usr/bin/env perl -w
print "You selected '$ARGV[0]'.\n";
Johnsyweb
<c-r><c-f> gave me the pathfile. thanks.
Hermann Ingjaldsson
and what represents the whole line under the cursor?
Hermann Ingjaldsson
ZyX's answer better explains how to do this. I'm leaving my answer up here as it has some useful links as to how to map keys (and you've asked a few questions about this now).
Johnsyweb
+2  A: 

Above solutions are not safe if word under cursor contains special characters. You should use

nnoremap <F2> :execute "!/path/to/script.pl ".shellescape(expand("<cword>"), 1)<CR>

instead.

For the whole line, replace expand("<cword>") with getline('.'). For the filename under cursor use expand("<cfile>").

ZyX