In vim, if I'm working on a Python script, I will commonly type:

:! python this_script.py

to execute the script. Is there a shortcut for the name of the current file? If not, can I easily make one? I'm new at vim, and I'm not sure how to google for this.

4 Answers 4


You can just use % for current file. This command should serve your purpose:

:! python %
  • 4
    Thus, don't forget you must quote the % character when using commands like :s ... :s/Foo\%X// ... if you don't, the name of the file will show up there.
    – Mei
    Jun 4, 2011 at 6:24

One other way is to use the really nice package for vim: Efficient python folding : Fold python code nicely and toggle with one keystroke which folds the "defs, classes" but also binds "shift+e" to 'save + execute', so cool and useful!


FYI, if you are also using the embedded Python interpreter in Vim, you can get the name with the vim.current.buffer.name attribute. You man also want to use the :update command to write the file automatically. I have a mapping like for Python buffers:

nmap ;ru :update<CR>:python pyterm(vim.current.buffer.name)

The pyterm is a function that also opens a new Python interpreter in a new terminal window and runs it in interactive mode.


Some pointers

:h 'makeprg
:h :make

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.