I want to filter some text through an external command without replacing the text, but rather have some way of inserting it afterwards or wherever I want.

Say I want to calculate:

2^8 = 

I mark 2^8 in visual mode and type :! bc, this replaces 2^8 with 256. However, I would rather just insert the result of the command after the = sign. Is this possible?

  • As a useful side-note to the answers below, if I wanted to filter part of the line to the bc command I could use the vis plugin (stackoverflow.com/questions/16196826/…), and select the relevant part of the line with V, then enter :B ! bc
    – gauteh
    Jun 15 '17 at 10:08


2^8 =
1+1 = 3
2+4 =
4%8 =


:g/=$/ t. | s/=// | exe '.!bc'  | -j


2^8 = 256
1+1 = 3
2+4 = 6
4%8 = 4

It finds each line ending in =, copies it below, trims off the =, filters it through bc, then joins it to the line above.


  • :h :global -- short form: :g -- executes the following commands on each line that matches.
  • :h :copy -- short synomym: :t -- copy a range of lines elsewhere.
  • :h :substitute -- short form: :s -- search and replace.
  • :h :execute -- short form: :exe -- execute a string as a command, used here to allow another following command since otherwise | would be passed to the shell.
  • :h range! -- filter a range of lines through an external program.
  • :h :join -- short form: :j -- join a range of lines.
  • I did not realize that : ! command works on the whole line and not just on the selection, this explains why things didn't work.
    – gauteh
    Jun 14 '17 at 10:18

What about

:%s/\(.*\)=\s*\zs$/\=system('bc -q <(echo '.shellescape(submatch(1)) . ')')/

This replaces line-endings terminated with = by bc -q <(echo "expression").


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.