Shell Commands


Vim has built-in support for calling shell commands.

The simplest form of calling shell commands is:

:!command [args]

where command is the shell command to call, and rags are optional arguments to be passed to the command. For example, the following returns the current working directory:

:!pwd

You can also write a shell script and call it:

test.sh:

pwd

then:

:!bash test.sh

similarly,

chmod u+x test.sh
!./test.sh

A second form of calling shell commands leverages the :exe[cute] command, which has the call signature:

:exe[cute] {expression}

which evaluates {expression} into a string, which is then executed. As a simple example, we can call pwd using :exe[cute] like this:

:exe "!pwd"

which produces the same result as above. Here is an example that evaluates the fnamemodify and getcwd functions to generate the parent directory, which is passed as an argument to ls:

:exe "!ls" fnamemodify(getcwd(), ":h")

when expression contains multiple arguments, as above, they are concatenated together with a space between them. This space can be removed by separating the arguments with the ".." operator.