Vim using shell, how can I stop job like C-z in Unix? - vim

I have a quick question about programming using Vim. I sometimes make a silly mistake in my program.
For example, the Python code below has an infinite loop (say foo.py . . . be careful when executing it!)
x = 1
while x == 1:
x = 1
You won't see any result, but find a fan inside your computer is becoming louder, and you need to stop executing this Python program. On a Unix shell, you can do it by pressing Ctrl-z. Or, on Emacs shell-mode, just pressing Ctrl-c Ctrl-z.
I know how to switch to shell-mode in Vim: :sh (then type python foo.py) or :!python foo.py. But I don't know how to stop a job on a shell from Vim without killing Vim itself. Does anyone know this?

Why can't you use <C-C>? By default, it sends an INTerrupt signal unless program is able to catch <C-C>. Vim handles <C-C> like any other <C-...> key, but python does not do this by default.
By the way, <C-Z> in unix shells suspends program while in the case mentioned above you need to kill it which is done using <C-C>. Suspend=pause executing (though most programs are not able to/do not need to handle SIGTSTP/SIGCONT properly).

pkill <process name>
or
ps -ef, find the process then kill -9 <process id> (this is safer)

How do you determine that vim is "killed"? The :sh command starts a shell which looks like your normal shell. When the shell exits (e.g. after Ctrl-D) you return to Vim.

Related

How to use xdotool to open a new tab, switch to it and run commands in it

I am trying to write a bash script to automate running some commands. However some of these commands should be running in their own terminal tab.
So I use the following in my bash script to open a new tab:
xdotool key ctrl+shift+t
this does the job, but the next commands in my bash script are still executed in the previous terminal tab.
How can I make the new opened terminal tab active and run the next commands in this tab?
What Terminal Emulator are you using? It strongly depends on this.
In general, you could write the commands you want to execute in a shell script and tell your terminal emulator to execute the script once it has started.
Example with xterm:
echo '#!/bin/bash' > /tmp/thescript
echo 'ls -la' >> /tmp/thescript
chmod +x /tmp/thescript
xterm -hold -e /tmp/thescript
EDIT: I just saw that u asked for a way to achieve this with xdotool. So this answer might be invalid. Please tell me if so - then i'll delete it.
How are you using xdotool? It can be done with a chain, for example:
$ xdotool key "ctrl+shift+t"; xdotool type "ls"; xdotool key Return
If all you want is to run the commands in the background / in parallel, without synchronously waiting for each command to complete before the next begins, terminate them with an ampersand & to instruct the shell to do so.
Alternatively, you can execute the commands in their own subshells by surrounding each with parentheses ( ). If they are long running processes or you do not wish to pollute the original shell with their output, you can fork them off and capture their output to file with something like (setsid command 1>/path/to/log &).
If separate tabs is necessary requirement, you can use xdotool to key the switch-to-the-next-tab binding or similar, and then key the commands you must run in that tab.
Instead of sorting out that mess yourself, you could use a script from this answer by Jacob Vlijm, which wraps a windowed approach that uses xdotool and wmctrl to 'send' commands to different terminal windows. The script is written in python 3 but it can easily be rewritten for a shell environment of choice.
A more direct approach involves use of a TIOCSTI ioctl to inject characters into another terminal. According to the tty_ioctl manual page:
NAME
ioctl_tty - ioctls for terminals and serial lines
...
DESCRIPTION
The ioctl(2) call for terminals and serial ports accepts many possible
command arguments.
...
Faking input
TIOCSTI const char *argp
Insert the given byte in the input queue
...
Here are c and perl wrappers, and an example in python as referenced by this answer.

Vim interrupted after shell command

I don't get why when I type something like :
:! bash -ic 'p4 diff %:p'
My Vim get interrupted and I get
[3]+ Stopped vim ~/.vimrc
as a consequence I have to put that job in foreground to see the effect of the command.
Do you know how can I solve this annoying issue ?
You are telling Vim to run an interactive command so it has no choice but to suspend itself to allow you to do what you want.
It's hard to be sure without more information about your setup but this command:
:!p4 diff %:p
should be enough to run p4 diff on the the current file without side-effects.

Run emacs as separate process from terminal

When I run the emacs text editor the process does not return so I cannot use the terminal which is obviously the default behavior.
I cannot find the switch or command in the man but I know it is something very simple.
How can I run emacs as a separate process so I can continue to use the terminal without opening a second one?
You can start any program in the background by appending an ampersand to the command, emacs &.
There's a whole framework for working with backgrounded processes, see for example man jobs, disown, fg, bg, and try Ctrl-Z on a running process, which will suspend it and give you the terminal, allowing you to resume that process either in the foreground or background at your pleasure. Normally when your shell closes, all those programs will end, disown allows you to tell a program to keep running after you end your session.
The emacs --help command is giving you a tip:
--batch do not do interactive display; implies -q
So run emacs --batch (or maybe emacs --executesomecommand ).
If you have a desktop (or some X11 display) and want emacs to open an X11 windows and give you back a shell prompt, run it in the background (e.g. emacs &) as many commented.
And I find very useful to start programs (or shells) within emacs, e.g. with Emacs commands: M-x shell, or M-x compile (for make etc...), or M-x gdb for a debugger.
You usually start one single emacs at the beginning of a working day. You could use emacsclient (or set your EDITOR environment variable to it) for other editions using the same emacs.
I'd like to add that Windows does have an equivalent to *nix's & for starting programs in a separate process:
start /b emacs Main.hs

What if we close the terminal before finishing the command?

Let me explain better. What is gonna happen if I run a command in Linux and before it's done and you could enter another command I close the terminal. Would it still do the command or not?
Generally, you must expect that closing your terminal will hangup your command. But fear not! Linux has a solution for that too!
To ensure that your command completes, use the nohup argument first. Simply place it before whatever you are trying to do:
nohup ./some_program
nohup ./do_a_thing -frx -file input_file.txt
nohup grep "something" giant_list_of_files/* > temp_file.txt
The nohup command stands for "no hangup" and it will ensure that the command you execute continues to run, even if you close your terminal.
It depends on the process and your environment (job control shell options, VNC, etc). But typically, no. The process will get a "hangup" signal (message) from the operating system, and upon receiving that, will quit.
The nohup command, for example, arranges for processes to ignore the hangup signal from the OS. There are many ways to achieve the same result.
I would say it will abort att the status you are in just before the session close.
If you want to be sure to complete the job, you will need to use the nohup command.
http://en.wikipedia.org/wiki/Nohup
Read about nohups and daemons (-d)...
A good link is [link]What's the difference between nohup and a daemon?
Worth look at screen command, Screen command offers the ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time.

How to check if it's still in Vim shell mode

In vim, type :sh will switch to shell, and exit can exit the shell and get back to vim. Is there any command to check if it's in vim shell mode? So that I won't accidentally vim to open the same file again. I want to avoid below scenario:
vim myfile > :sh > exit > vim myfile // get warning of another vim instance is editing the same file
These are the normal scenario:
vim myfile > :sh > exit // keep editing
vim myfile > :wq > vim myfile // keep editing
In addition to #a3nm answer, what you can do is
use pstree -h: it will output process tree with current branch highligted and all you need to check is whether there is vim in the highlight.
Another possibility is ps t: it will show all processes using the current terminal and should show vim in a list when you are inside :sh. ps -oargs t may be more useful in case you want to know arguments you ran vim with.
These methods are more reliable because VIMRUNTIME, VIM and MYVIMRC environment variables may be overrided by you in order to do some customizations (BTW, they are defined by vim for use in vimscripts, not by :sh). They also work for other processes that allow you to run a subshell, but do not define any environment variables.
I would also suggest to consider using <C-z> in normal mode or :suspend/:stop in Ex because these use shell vim was launched from instead of creating new. This behavior gives you access to history of commands you typed before running vim and also makes you able to write more complex and time-consuming shell configuration without needing to wait every time.
In case you use <C-z> both methods still work, but first method won’t highlight vim because it will be at the same level (has the same parent) as pstree itself, likely just below or above pstree in graph. This also enables third method: jobs shell builtin.
In order to restore from <C-z> you should use fg (a single % in zsh and bash also works) which is less to type then exit (but more then <C-d>).
The :sh command in vim seems to define the VIMRUNTIME, VIM and MYVIMRC environment variables, so you can just check if they are defined. To do so, you can run echo $VIM, for instance, which should return an empty line in a normal shell and something like /usr/share/vim in a shell run from vim.

Resources