2012-08-29 25 views
1
function! ReName() 
    let old_name = expand("<cword>") 
    let new_name = input("new name: ",old_name) 
    let cmd = "ref.sh ".expand(old_name).expand(" ").expand(new_name) 
    :call system(cmd) 
endfunction 

ref.sh是一个bash文件,背景是如何调用系统()以2个参数的VIM

#! /bin/bash 
find . -name '*.[ch]' | xargs sed -i s/$1/$2/g 

但现在,当我在VIM使用重命名功能,这是行不通的。

回答

2

嗯,你能指望它做的,什么/哪里是错误?

首先,您忽略从system()的呼叫输出。如果存在输出,要么使用:echo代替:call,将其分配给一个变量,或者它:return。否则,请检查v:shell_error变量以了解命令的退出状态。


一些更多的批评:通过printf()

let cmd = "ref.sh ".old_name." ".new_name 

或组装命令:代替

let cmd = "ref.sh ".expand(old_name).expand(" ").expand(new_name) 

离开了多余expand()

let cmd = printf("ref.sh %s %s", old_name, new_name) 

您的功能将仅适用于特定,乖巧的参数工作。在Vim中使用shellescape(),并在shell脚本中正确引用。

+0

试试这个办法,但仍无法执行该脚本! – user1632780

0

我同意英戈Karkat。其他的方式可以是替代

:call system(cmd) 

,它是不正确与vimrc内结肠,

exe "!" . cmd 

假设你cmd有没有特殊字符,如空格,换行OS类似的东西。

+0

你可以使用':call';但只有在键入(进入命令行模式)时才需要“:”,在脚本中可选。 –

+0

@IngoKarkat:谢谢。编辑修复它。 – Birei

+0

它的over.The原因是,对于bash.thank你的路径! – user1632780