14

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.

Eric Wilson
  • 4,622
  • 9
  • 32
  • 43

4 Answers4

23

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

:! python %
Mat
  • 51,578
  • 10
  • 158
  • 140
Deepak Mittal
  • 1,544
  • 13
  • 18
  • 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 04 '11 at 06:24
0

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!

Zenon
  • 101
  • 1
0

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.

Keith
  • 7,828
  • 1
  • 27
  • 29
0

Some pointers

:h 'makeprg
:h :make
Mat
  • 51,578
  • 10
  • 158
  • 140