How to execute an external command in Vim script?

Afshin Mehrabani picture Afshin Mehrabani · Jul 4, 2013 · Viewed 7.6k times · Source

I want to execute an external command in Vim script but I don't how can I do this. For example I need to change a file permission using chmod command (Please assume application has enough permission to run the command)

Is there any way to achieve this?

Answer

Ingo Karkat picture Ingo Karkat · Jul 4, 2013

If you want users to see the output (or interact with the command) :! is the right command. For silent execution (as I suppose would be desired with your chmod), using system() is preferable. Especially on Windows, this avoids the popup of a command prompt that must be dismissed manually.

:call system('chmod +x ' . shellescape(fname))