Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 19 down vote accepted

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

:! python %
share|improve this answer
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 '11 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!

share|improve this answer

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.

share|improve this answer

Some pointers

:h 'makeprg
:h :make
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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