Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call system() with 2 arguments in vim

Tags:

vim

system

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 is a bash file, context is

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

but now, when i use ReName function in vim, it does not work.

like image 992
user1632780 Avatar asked Sep 11 '25 23:09

user1632780


1 Answers

Well, what do you expect it to do, and what / where is the error?

First of all, you ignore the output from the call to system(). If there is output, either use :echo instead of :call, assign it to a variable, or :return it. Otherwise, check the v:shell_error variable for the exit status of the command.


Some more critiques: Instead of

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

leave out the superfluous expand():

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

or assemble the command via printf():

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

Your function will only work for certain, well-behaved arguments. Use shellescape() in Vim, and proper quoting in your shell script.

like image 133
Ingo Karkat Avatar answered Sep 13 '25 13:09

Ingo Karkat