vim run perl script with word under cursor as argument
is it possible to send the word under the cursor to a perl script by typing a s开发者_运维问答hortcut?
how do i do that?
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>")
.
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:
:help :nnoremap
:help :map-special-keys
:help !
My test script:
#!/usr/bin/env perl -w
print "You selected '$ARGV[0]'.\n";
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.
精彩评论