Vim在光标下发送一个Word到外部命令

时间:2016-03-21 17:49:57

标签: vim external text-editor

我正在尝试将一个Word发送到外部命令。我做了一些挖掘,但没有一个有效的解决方案。我学会了使用<cword>,但我似乎无法将当前的单词传递给外部命令。

这是我的命令:

nmap <silent> <F8> :!start  test.exe s/\(<c-r>=expand("<cword>")<cr>\)/

它所做的就是获取光标下的当前单词并将其传递给test.exe。请有人帮忙。

更新

这是我想要实现的目标。我在代码中有函数名。

a = 0
b = 1
c = add_function(a,b)

我想使用光标下的单词将add_function传递给我的自定义可执行文件。这样它就会启动test.exe,并传递以下内容:

open, 'add_function'

enter image description here

我尝试了上面的vim命令,但确实有效。

2 个答案:

答案 0 :(得分:4)

如果我删除了我在cmdline中的<silent>,我不确定映射的问题是

:!start  test.exe s/\(WORD\)/

哪个可能是你想要的?它没有发送,因为您没有在末尾添加<Cr>(并且由于<silent>而未在命令行中显示)...

nnoremap <silent> <F8> :!start  test.exe s/\(<c-r>=expand("<cword>")<cr>\)/<CR>

如果这不是您想要的,并且如果添加s/\(..\)/不是故意的,那么让我们重新尝试通过回显Vim中的当前单词来获得最简单的工作映射: / p>

nnoremap <F8> :echo shellescape(expand('<cword>'))<Cr>

然后展开它以运行外部echo命令:

nnoremap <F8> :execute ':!echo ' . shellescape(expand('<cword>'))<Cr>

然后展开它以运行start text.exe

nnoremap <F8> :execute ':!start  test.exe' . shellescape(expand('<cword>'))<Cr>

最后添加<silent>

nnoremap <silent> <F8> :execute ':!start  test.exe' . shellescape(expand('<cword>'))<Cr>

答案 1 :(得分:0)

再添加一个“cr”来运行外部程序

nmap <silent><F8> :!start  test.exe s/\(<c-r>=expand("<cword>")<cr><cr>\)/

结束将其重写为

会更好
nnoremap <silent><F8> :!start  test.exe %s/\(<c-r>=expand("<cword>")<cr><cr>\)/