简体   繁体   English

vim以光标下的单词作为参数运行perl脚本

[英]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 shortcut? 是否可以通过键入快捷方式将光标下的单词发送到perl脚本?

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('.') . 对于整行,将expand("<cword>")替换为getline('.') For the filename under cursor use expand("<cfile>") . 对于光标下的文件名,请使用expand("<cfile>")

Given a perl script ${HOME}/test.pl , you could use: 给定一个perl脚本${HOME}/test.pl ,您可以使用:

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

Then pressing F2 would send the word under the cursor to your script. 然后按F2键会将光标下的单词发送到您的脚本。

As per my answer to your previous question , CTRL - R CTRL - W represents the word under the cursor. 根据我对上一个问题的回答CTRL - R CTRL - W表示光标下方的单词。

See the following help topics in Vim to get you started with writing keyboard shortcuts: 请参阅Vim中的以下帮助主题,以开始编写键盘快捷键:

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. 在您的vimrc中,此行将F5键映射到此字符组合。 CTRL-R CTRL-W inserts current word in a command line. CTRL-R CTRL-W在命令行中插入当前单词。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM