Contents

Vim Bang Bang

Contents

Let’s say we edit a file and we are in the 2nd line and want to do the math:

1
2
3
This is some text
1+2
30*2 + 4

Hitting !! in normal mode puts you in command-line mode ready to type a shell command: :.!bc. The input of the shell command will be the line where the cursor is (1+2). It will send it to bc and by hitting Enter it will replace the line with the result:

1
2
3
This is some text
3
30*2 + 4

The . means the line where the cursor is. You can pass a range if you want, let’s calculate both lines:

1
2
3
4
5
This is some text
1+2
30*2 + 4

:2,3!bc

and the result:

1
2
3
This is some text
3
64

Here is the vim documentation for the !! command:

1
2
!!{filter}		Filter [count] lines through the external program
			    {filter}.

You can run any shell command, convert this one line json input

1
2
3
{"name":"foo","last_name":"bar","age":30}

:.!python -m json.tool

to this

1
2
3
4
5
{
    "name": "foo",
    "last_name": "bar",
    "age": 30
}