简体   繁体   中英

Using Python and Vim within the Mac Terminal

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?

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.

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 .

截图

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

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

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.

在vim中输入:w yourfilenamehere.py并按回车

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM