Or: How To Pipe The Current Vim Buffer Through Unix Commands

In this post I will show you how to run a shell command from within Vim, and immediately reload that file.

The Problem

I write a Go file in (Neo)Vim. I want to use the command gofmt to format my file.
Running gofmt will change the contents of my file, so I’ll need to reload my Vim buffer.

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.

What Doesn’t Work

Let’s run gofmt on the current file and then reload the buffer with :e (see :help edit).

:!gofmt % | e


  1. Execute gofmt on the current file (%)
  2. Use a pipe to redirect the output of that command to Vim’s edit


/bin/bash: e command not found


Any “|” in {cmd} is passed to the shell, you cannot use it to append a Vim command. See |:bar|.

(See :help :!)

What Works

You can use execute as a work-around:

:execute '!gofmt %' | edit

What’s Even Better

Use the ex % operator to filter all lines in your current buffer to an external shell command:


Further Reading