Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute an external command in Vim script?

Tags:

vim

vim-plugin

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?

like image 760
Afshin Mehrabani Avatar asked Jul 03 '13 22:07

Afshin Mehrabani


People also ask

How do I run a command in Vim?

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.

How do you chain command in Vim?

You can execute more than one command by placing a | between two commands.

How do I open the console in Vim?

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.


1 Answers

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))
like image 95
Ingo Karkat Avatar answered Oct 13 '22 00:10

Ingo Karkat