I'd like to select some text in vim and then execute it in bash. I know of the following procedure:

  1. select text using V or v
  2. :!!

However, this replaces the selection with the output of the command. What if I don't want that?


3 Answers 3


Type the :w command like this:

:w !command

As you type it, it will appear like this:

:'<,'>w !command


:help :w_c
  • 4
    This is correct, but take care not to do :w! command by mistake. If command is a program you have write access to, it'll overwrite the program, and you won't be able to undo the change since that wasn't the buffer that was open.
    – Ray
    Oct 12, 2016 at 19:26

In vim 8.0, you can select a range of lines and run :terminal bash. This will open a terminal running bash with your selection as stdin.

:terminal node, :terminal ruby, :terminal python seem to work nicely as well.

  • In 8.2: E319: Sorry, the command is not available in this version. Maybe one needs to build with special flags.
    – oarfish
    Oct 15, 2020 at 13:32

Every 6-8 months I need this, forget how, and end up here. Here is the answer I'll want in the future:

To run the visually selected text from vim in an escaped shell via register:

  • Copy selection to the register "a by typing "ay.
  • Open vim command mode with !<CTRL-r>a

(Skip named register "a and just use the default 0 if you're feeling lazy)

Visual instructions:

Vim visual selected text

type "ay

command mode, <CTRL-r>a

vim shell escape running bash command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .