is it possible to send the word under the cursor to a perl script by typing a shortcut?
how do i do that?
is it possible to send the word under the cursor to a perl script by typing a shortcut?
how do i do that?
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.
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";
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>")
.