Super User is a question and answer site for computer enthusiasts and power users. 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

I'm pretty new to VIM and linux in general. So doing this on my own is well beyond my skill level now. I'm familiar with vimrc file but that is it...

Let's say I have one terminal open idling. Then I open another terminal, and edit a python (.py) file with vim. How can I create a permanent hotkey (like f1, for example), that will work on all VIM modes, that will save the python file and execute it in the other terminal window?

share|improve this question
le google: -- oh and running something in another terminal is not trivial, unless you want to start a new terminal. I'd say just forget about it for now... Perhaps someone will have a better suggestion :) welcome to SU! – Ярослав Рахматуллин Aug 31 '13 at 5:27

Executing a command in another existing terminal can be done, but it is far from trivial.

First, you have more than one terminal emulator open. Those terminal emulators are running a shell, probably bash if you're on Linux. With the ps command you can find the process ids (PID) of all the shells running on your machine. The big question now is; which one to send the command to?

You are looking for a shell that belongs to the terminal wou want to show the command on. Running the command echo $$ on the terminal in question should return the PID of the shell running on that terminal. Using the /proc filesystem you can determine the shell's standard input /proc/<PID>/fd/0, which is a pty device.

Next you want to send a command to that terminal. This has to be done using the TIOCSTI ioctl command on the shell's pty. You can find the C source code to do that here. Compile that program as e.g. ttyecho and put it in a directory in your $PATH.

Now the pieces are in place, and you could call the ttyecho command with the name of the shell's pty, and the command python <filename>.

Doing all this in vimscript is left as an exercise for the reader. :-)

share|improve this answer

You can try something like this (same terminal):

map <F2>:w<CR>!C:\Python27\python.exe %<CR><CR>
share|improve this answer
appleLover mentioned he is on Linux – taketwo Aug 31 '13 at 11:39
That's hardly a reason to vote the answer down. The Vim command is identical in a unix environment apart from the binary path. I'm all for pedantry, but you sir take the cake :) – Ярослав Рахматуллин Sep 1 '13 at 17:21
I do not see the point of giving an answer that will definitely not work for the person asking, unless he does proper modifications. That might be good for educational purposes but well... we are not in a classroom. – taketwo Sep 2 '13 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .