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?
You can run commands in Vim by entering the command mode with : . Then you can execute external shell commands by pre-pending an exclamation mark ( ! ). For example, type :! ls , and Vim will run the shell's ls command from within Vim.
You can execute more than one command by placing a | between two commands.
Use ctrl-w N to switch to "terminal-normal mode", which will let you navigate around. It could be useful to then copy content to the clipboard. Then return to regular terminal mode, simply type i just like how you'd enter insert mode from a regular window. ctrl-w : will open command mode like in regular Vim.
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With