1

I'm writing Python code using Vim inside Terminal (typing command "vim" to start up Vim). I've been trying to find a way to execute the code through the mac terminal in the same window.

I'm trying to use :!python % but I get the following error message: E499: Empty file name for '%' or '#', only works with ":p:h"

Anyone have any suggestions?

3
  • Are you trying to execute the file, or trying to run a few lines of code? Commented Oct 12, 2013 at 5:15
  • You can also open a new tab in the "same window" but it is easier to just have two side-by-side. Commented Oct 12, 2013 at 5:20
  • Do you need a terminal text editor? I'm pretty sure there must be good graphical (windowed) vi variants. Or is Aquamacs just one more reason that emacs blows vi away? :P (Seriously, in Aquamacs I frequently run Python in a terminal in another window in the frame or in another frame, and it's much nicer than trying to emacs inside iTerm.) Commented Oct 12, 2013 at 8:06

5 Answers 5

2

You can't execute a file if that file doesn't exist.

Write the file with :w filename.py (further writes only need :w) and execute your script with :!python %.

Learning programming and Vim at the same time is not a very good idea: Vim is a complex beast and trying to handle both learning curves won't be easy. As much as I love Vim, I'd suggest you use another text editor, at least in the beginning, like Sublime Text or TextMate.

In short, focus on programming first by using a simple and intuitive editor and learn Vim once you are comfortable enough in your craft.

Or don't, Vim is the greatest text editor but you can definitely be a successful programmer without it.

Sign up to request clarification or add additional context in comments.

4 Comments

I second the idea of learning Vim later, though I certainly suggest that you do eventually learn it.
Someone is so in love with Vim that its making him/her blind.
Not quite sure how suggesting someone should eventually learn a powerful tool qualifies as blind love. It's not like I said they can't be a good developer without it.
@CodyPoll, I think that we agree on this topic. My comment wasn't directed at you but at the person who downvoted this answer.
1

You can use splits to have both vim and a bash prompt in the same terminal window.

I would highly recommend switching from the default Terminal app to iTerm2. It's a terminal with many nice features, including 256 colours, tmux integration, and vertical splits.

Vertical splits are much nicer for looking at code and output together in the same window than the horizontal splits available in Terminal.

screenshot

You can also map shortcut keys to quickly switch between the splits.

3 Comments

Thanks for the tip, I just downloaded it!
I'm new to programming, and just find it inconvenient that I can't find a decent text editor with a built in terminal output window.
Vim is great, especially if you're just starting out and have time to learn it. I do all of my work in it and I'm glad I spent the time with it when I first started. You can find lots of plugins to extend vim's capabilities, but often there's another program that will do a better job of what you're trying to accomplish. Rather than shoehorn it into the editor, just switch to your shell split and run the program. Part of the unix philosophy is a program only doing one thing, and doing it well.
0

You can execute command line arguments inside vim by starting the argument with a "!" from the command mode. Also, in command mode, "%" means the current file. Thus, you can execute the current file that you are editing like this:

:!python %

I should probably also add, as another option, that you can split the terminal pane in OS X by pressing Command+d. Then you can run commands in the bottom half, and edit in the top half

Comments

0

You can use "quickrun" plugin. This plugin run a command and show its result quickly.

install this one, then type <Leader>r(default \r) to run program.

or

Use tmux.This tool is a terminal multiplexer.can split window in the same terminal.

Comments

-1

in vim type :w yourfilenamehere.py and press enter

3 Comments

That doesn't run Python, just saves the current file.
yes he needs to save it first in order to run it right?
yes but the answer from romainl already explains that better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.