External Commands
vimrc can work with external commands in a very elegant way. You can call the commands in various means. These are the current best practices.
Use the Bang (!)
Use the Bang (!)
For simple and easy 1-5 words command, you can easily use the bang. Before the bang, please use the colon to indicate that it is a vim command. Otherwise, it is very confusing between vim commands and shell commands. Here is an example:
:!clear
Use System()
Use System()
For long and complicated (e.g. variable substitution) commands, you use system()
. You can capture the output and error code easily as well. If you need to print the output, you can use a simple echo. Here is an example:
let cmd = 'echo "Hello "' . vimVariable
let output = system(cmd)
echo output
NOTE:
- As a good practice, formulate the string command in a separate line before feeding it into the
system()
call. - Sometimes, when the external command output is too long to the point it requires you to press ENTER a couple of times, you can press
q
to exit the prompt and get back to editor.
That's all about interacting with external commands from vimrc.