Silas33
Silas33

Reputation: 41

How to create a command in vim that accesses the current filename?

I would like to create an individual vim command that does the following:

  1. save the current file
  2. run the current file with python

Currently I have to do it like this:

:w

:! python3 {filename}

Instead, I would like to do this:

:pyRun

or something similar

Upvotes: 2

Views: 356

Answers (1)

rkta
rkta

Reputation: 4589

You can chain commands with |. So

:w | ! python3 %

will save the buffer and run it with python3.

See :help :bar and :help c_% for more information.

You can create a command for this like:

:command PyRun execute 'w <bar> ! python3 %'

Note that custom commands in vim have to start with an uppercase letter.

Upvotes: 1

Related Questions