How to execute an external command in a Vim script?

I want to execute an external command in a Vim script, but I cannot do it. For example, I need to change the file resolution using the chmod (suppose the application has enough permissions to run the command)

Is there any way to achieve this?

+6
source share
3 answers

If you want users to see the result (or interact with the team) :! - this is the right team. For silent execution (I believe it would be desirable with your chmod ), it is preferable to use system() . Especially on Windows, this prevents the command prompt from popping up, which must be manually rejected.

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

You can use the command :! vim. For example, in echo 'Hello, World!' from inside vim (and therefore from inside vim script, too), type

 :! echo 'Hello, World\!' 

in vim. Or, in a vim script, you can only put

 ! echo 'Hello, World\!' 

The reason you need to \ before! because vim does special processing! characters in argument a! command. If you ran a command that does not include any! symbol, then you do not need to avoid it.

If you want to talk more about this in detail, you can enter

 :help :! 

in vim as @FDinoff said.

+6
source

My preferred way is to press Ctrl-z or run :sus to pause the vim process, then you can do whatever you want in the shell.

When you're done, run fg (foreground) to resume your vim session.

More information can be found here .

+2
source

Source: https://habr.com/ru/post/948662/


All Articles